일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 배열
- 프로그래밍
- Kotlin
- c
- Direct2D
- Windows
- Programming
- CS
- 알고리즘
- Tips강좌
- 문법
- doit코틀린프로그래밍
- Visual Studio
- Tips프로그래밍강좌
- c#
- 지식나눔강좌
- 백준
- 연산자
- Javascript
- 김성엽
- Desktop
- 리뷰
- 포인터
- VS ERROR
- c++
- 티스토리
- 함수
- Win32
- tipssoft
- 이지스퍼블리싱
Archives
- Yesterday
- Today
- Total
F.R.I.D.A.Y.
바둑 죽은 돌 개수 구하기 for C 본문
반응형
C++ 코드 주소 : http://pang2h.tistory.com/3?category=699931
/*
바둑판의 크기를 입력받은 후, 흰 돌에 둘러쌓여 죽은 검은 돌의 개수를 구하여라.
※ 흰 돌과 검은 돌은 무작위로 정하며 바둑판은 정사각형이다.
※ 네 면이 모두 흰 돌에 막혀있는 것만 죽은 것으로 인정한다.
※ 인접한 검은 돌을 이어 단 한 개의 검은 돌이라도 흰 돌이 아닌 테두리에 있는 것은 죽은 것으로 보지 않는다.
○ : 흰돌
● : 검은 돌
ex) ● ● ○ ○ ○ ● ○
○ ○ ○ ● ○ ● ●
● ● ○ ○ ● ○ ●
○ ○ ● ● ● ○ ●
● ● ○ ○ ● ● ○
● ○ ○ ○ ○ ○ ●
● ● ● ○ ○ ● ●
죽은 검은 돌 수 : 7
*/
#pragma region code
#pragma warning(disable:4996)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define black 1
#define white 0
#define stoneBlack "●"
#define stoneWhite "○"
void mapPrint(int **map, int size);
void areaCheck(int x, int y, int**map, int**checkMap, int**countMap, int size, int re);
int lineCheck(int**countMap, int size);
int dieCount(int**countMap, int size);
int main(void) {
int size = 0;
int dead = 0;
system("color 70");
//바둑돌 색상 지정
while (!(size >= 7)) {
printf("맵 크기 : ");
scanf("%d", &size);
}
int **map = (int **)malloc(sizeof(int *) * size);
//메인 맵 선언
int **checkMap = (int **)malloc(sizeof(int *) * size);
//체크 확인 맵 선언
int **countMap = (int **)malloc(sizeof(int *) * size);
//개수 확인 맵 선언
for (int i = 0; i < size; i++) {
map[i] = (int *)malloc(sizeof(int) * size);
memset(map[i], 0, sizeof(int) * size);
//메인 맵 데이터 초기화
checkMap[i] = (int *)malloc(sizeof(int) * size);
memset(checkMap[i], 0, sizeof(int) * size);
//체크 확인 맵 데이터 초기화
countMap[i] = (int *)malloc(sizeof(int) * size);
memset(countMap[i], 0, sizeof(int) * size);
}
srand((unsigned int)time(NULL));
//난수 초기화
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
map[x][y] = rand() % 2;
//메인 맵에 바둑돌 데이터 입력
}
}
mapPrint(map, size);
//바둑판 맵 출력
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
if (checkMap[x][y] != 1) {
//이미 체크를 한 맵의 경우 넘어감
areaCheck(x, y, map, checkMap, countMap, size, 0);
//인접한 돌 체크
if (!(lineCheck(countMap, size))) {
//테두리 상에 붙어있는 돌의 경우 제외
dead += dieCount(countMap, size);
//죽은 돌의 개수만큼 추가
}
}
}
}
printf("죽은 검은 돌 개수 : %d\n", dead);
return 0;
}
void mapPrint(int **map, int size) {
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
if (map[x][y] == black) {
printf("%s", stoneBlack);
}
else {
printf("%s", stoneWhite);
}
}
printf("\n");
}
}
void areaCheck(int x, int y, int **map, int **checkMap, int **countMap, int size, int re) {
if (re == 0) {
for (int i = 0; i < size; i++) {
memset(countMap[i], 0, sizeof(int) * size);
}
}
if (checkMap[x][y] != 1 && map[x][y] == black) {
checkMap[x][y] = 1;
countMap[x][y] = 1;
if (x - 1 >= 0) {
areaCheck(x - 1, y, map, checkMap, countMap, size, 1);
}
if (x + 1 < size) {
areaCheck(x + 1, y, map, checkMap, countMap, size, 1);
}
if (y - 1 >= 0) {
areaCheck(x, y - 1, map, checkMap, countMap, size, 1);
}
if (y + 1 < size) {
areaCheck(x, y + 1, map, checkMap, countMap, size, 1);
}
}
return;
}
int lineCheck(int **countMap, int size) {
for (int x = 0; x < size; x++) {
if (countMap[x][0] == 1) {
return 1;
}
if (countMap[x][size - 1] == 1) {
return 1;
}
}
for (int y = 0; y < size; y++) {
if (countMap[0][y] == 1) {
return 1;
}
if (countMap[size - 1][y] == 1) {
return 1;
}
}
return 0;
}
int dieCount(int**countMap, int size) {
int count = 0;
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
if (countMap[x][y] == black) {
count++;
}
}
}
return count;
}
#pragma endregion
Result:
C++에서 작성한 코드를 C로 변환한 것일 뿐이므로 코드 결과는 C++에서의 결과와 같음.
728x90
반응형
'DEV > C C++' 카테고리의 다른 글
Q 공배수 출력 (0) | 2018.04.15 |
---|---|
수식으로 하트그리기 (0) | 2018.04.14 |
전처리 상수 VS 일반 상수 (0) | 2018.04.14 |
값 받아서 그래프 그리기 (0) | 2018.04.13 |
바둑 죽은 돌 개수 구하기 (0) | 2018.04.08 |
Comments