F.R.I.D.A.Y.

바둑 죽은 돌 개수 구하기 본문

DEV/C C++

바둑 죽은 돌 개수 구하기

F.R.I.D.A.Y. 2018. 4. 8. 19:53
반응형

/*

	바둑판의 크기를 입력받은 후, 흰 돌에 둘러쌓여 죽은 검은 돌의 개수를 구하여라.
	※ 흰 돌과 검은 돌은 무작위로 정하며 바둑판은 정사각형이다.
	※ 네 면이 모두 흰 돌에 막혀있는 것만 죽은 것으로 인정한다.
	※ 인접한 검은 돌을 이어 단 한 개의 검은 돌이라도 흰 돌이 아닌 테두리에 있는 것은 죽은 것으로 보지 않는다.
	
	○ : 흰돌
	● : 검은 돌

	ex)	● ● ○ ○ ○ ● ○
		○ ○ ○ ● ○ ● ● 
		● ● ○ ○ ● ○ ● 
		○ ○ ● ● ● ○ ● 
		● ● ○ ○ ● ● ○ 
		● ○ ○ ○ ○ ○ ● 
		● ● ● ○ ○ ● ● 
		죽은 검은 돌 수 : 7

*/

#pragma region code

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

#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 = 0);
int lineCheck(int**countMap, int size);
int dieCount(int**countMap, int size);


int main(void) {
	int size = 0;
	int dead = 0;

	while (!(size >= 7)) {
		cout << "맵 크기 : ";
		cin >> size;

	}


	int **map = new int *[size];
	//메인 맵 데이터

	int **checkMap = new int *[size];
	//체크 확인 맵

	int **countMap = new int *[size];
	//개수 체크 맵

	for (int i = 0; i < size; i++) {
		map[i] = new int[size];
		memset(map[i], 0, sizeof(int) * size);

		checkMap[i] = new int[size];
		memset(checkMap[i], 0, sizeof(int) * size);

		countMap[i] = new 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);

				}
			
			}


		}
	}
	cout << "죽은 검은 돌 수 : " << dead << endl << endl;

	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) {
				cout << stoneBlack;

			}
			else {
				cout << stoneWhite;

			}
		}
		cout << endl;

	}

	return;
}

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



#define stoneBlack "●"
#define stoneWhite "○"


여기서 다음과 같이 쌍따옴표로 "" 문자열 처리를 해준 이유는 ○● 가 아스키 코드에 없는 문자이기 때문이다. 영어와 숫자, 일부 문자는 1바이트로 표현이 가능하지만, 그 외의 것들은 2바이트 등으로 처리를 해주기 때문에 위와같이 쌍따옴표로 문자열 처리를 해준다. 출력할 때도 %s로 출력해주어야 하고, 그렇지 않으면 프로그램에서 오류가 난다.

728x90
반응형

'DEV > C C++' 카테고리의 다른 글

Q 공배수 출력  (0) 2018.04.15
수식으로 하트그리기  (0) 2018.04.14
전처리 상수 VS 일반 상수  (0) 2018.04.14
바둑 죽은 돌 개수 구하기 for C  (0) 2018.04.14
값 받아서 그래프 그리기  (0) 2018.04.13
Comments