๐Ÿ˜‰/์ฝ”๋”ฉํ…Œ์ŠคํŠธ

[์•Œ๊ณ ๋ฆฌ์ฆ˜] ์™„์ „ํƒ์ƒ‰(BOARDCOVER)

ruhz 2020. 7. 28. 17:15
 

algospot.com :: BOARDCOVER

๊ฒŒ์ž„ํŒ ๋ฎ๊ธฐ ๋ฌธ์ œ ์ •๋ณด ๋ฌธ์ œ H*W ํฌ๊ธฐ์˜ ๊ฒŒ์ž„ํŒ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒŒ์ž„ํŒ์€ ๊ฒ€์€ ์นธ๊ณผ ํฐ ์นธ์œผ๋กœ ๊ตฌ์„ฑ๋œ ๊ฒฉ์ž ๋ชจ์–‘์„ ํ•˜๊ณ  ์žˆ๋Š”๋ฐ ์ด ์ค‘ ๋ชจ๋“  ํฐ ์นธ์„ 3์นธ์งœ๋ฆฌ L์ž ๋ชจ์–‘์˜ ๋ธ”๋ก์œผ๋กœ ๋ฎ๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค. ์ด ๏ฟฝ๏ฟฝ

www.algospot.com

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
queue<int> answer;

int row, col;
int matrix[20][20];

int run() {
	/* ์™ผ์ชฝ ์œ„ ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•˜์—ฌ ๋น„์–ด์žˆ๋Š” ์นธ์„ x,y์— ์ €์žฅ
	 * flag ์ง€์ •ํ•˜์—ฌ, ์ฐพ๋Š” ์ฆ‰์‹œ ๋ฐ˜๋ณต๋ฌธ ํƒˆ์ถœ
	 * x, y๊ฐ€ ๋ฐ˜๋ณต๋ฌธ์„ ๊ทธ๋Œ€๋กœ ํ†ต๊ณผํ•˜๋ฉด ๋ชจ๋‘ ์ฑ„์›Œ์กŒ๋‹ค๋Š” ์˜๋ฏธ >> 1์„ ๋ฐ˜ํ™˜(result์— ๋”ํ•ด์ง)
	 */
	int x = -1;
	int y = -1;
	bool flag = false;	
	for (int i = 0; i < row; i++) {
		for (int j = 0; j < col; j++)
			if (matrix[i][j] == 0) {
				x = i;
				y = j;
				flag = true;
				break;
			}
		if (flag == true)
			break;
	}
	if (x == -1) return 1;
	
	/* ๋ชจ์–‘์— ๋งž๊ฒŒ x, y์˜ ๋ฒ”์œ„ ๋น„์–ด์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๊ฒ€์‚ฌ
	 * ํ˜ธ์ถœ๋œ ์ผ€์ด์Šค ๋ณ„๋กœ ๊ฐ๊ฐ์˜ ์„ฑ๊ณต ํšŸ์ˆ˜๊ฐ€ result์— ๋”ํ•ด์ง
	 * ์‹คํŒจ ์‹œ ๋์—์„œ ๋ถ€ํ„ฐ ๋ฐ˜ํ™˜๋˜๋ฉฐ ์žฌ๊ท€ํ˜ธ์ถœ๋ฌธ ๋’ค์˜ ๋ฌธ์žฅ๋“ค๋กœ ์›์ƒ๋ณต๊ท€
	 */
	bool flag2 = false;
	int result = 0;

	// โ”˜๋ชจ์–‘, โ”” ๋ชจ์–‘, โ”Œ ๋ชจ์–‘,  โ”๋ชจ์–‘ ์ˆœ์„œ๋กœ ๊ฒ€์‚ฌํ•˜๊ณ  ๊ฐ ๊ฒฝ์šฐ ์žฌ๊ท€ ํ˜ธ์ถœํ•˜์—ฌ ํƒ์ƒ‰ 
	if ((y > 0 && x < row - 1) && (matrix[x + 1][y] == 0 && matrix[x + 1][y - 1] == 0)) {
		matrix[x][y] = 1;
		matrix[x + 1][y] = 1;
		matrix[x + 1][y - 1] = 1;
		result += run();
		matrix[x][y] = 0;
		matrix[x + 1][y] = 0;
		matrix[x + 1][y - 1] = 0;
		flag2 = true;
	}
	if ((y < col - 1 && x < row - 1) && (matrix[x + 1][y] == 0 && matrix[x + 1][y + 1] == 0)) {
		matrix[x][y] = 1;
		matrix[x + 1][y] = 1;
		matrix[x + 1][y + 1] = 1;
		result += run();
		matrix[x][y] = 0;
		matrix[x + 1][y] = 0;
		matrix[x + 1][y + 1] = 0;
		flag2 = true;
	}
	if ((y < col - 1 && x < row - 1) && (matrix[x + 1][y] == 0 && matrix[x][y + 1] == 0)) {
		matrix[x][y] = 1;
		matrix[x][y + 1] = 1;
		matrix[x + 1][y] = 1;
		result += run();
		matrix[x][y] = 0;
		matrix[x][y + 1] = 0;
		matrix[x + 1][y] = 0;
		flag2 = true;
	}
	if ((y < col - 1 && x < row - 1) && (matrix[x][y + 1] == 0 && matrix[x + 1][y + 1] == 0)) {
		matrix[x][y] = 1;
		matrix[x][y + 1] = 1;
		matrix[x + 1][y + 1] = 1;
		result += run();
		matrix[x][y] = 0;
		matrix[x][y + 1] = 0;
		matrix[x + 1][y + 1] = 0;
		flag2 = true;
	}
	if (!flag2) return 0;

	return result;
}

int main() {
	int tc;
	char input;
	int count;
	cin >> tc;
	while (tc > 0) {
		cin >> row >> col;
		memset(matrix, -1, sizeof(matrix));
		count = 0;
		// matrix์—๋Š” ํŽธ์˜์ƒ '#'์€ -1, '.'์€ 0์œผ๋กœ ๋ฐ”๊ฟ” ์ €์žฅ
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < col; j++) {
				cin >> input;
				if (input == '#') matrix[i][j] = -1;
				else if (input == '.') {
					matrix[i][j] = 0;
					count++;
				}
				else return -1;
			}
		}
		// ์• ์ดˆ์— ๋นˆ์นธ์ด 3์˜ ๋ฐฐ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ฉด ์ •๋‹ต์€ 0
		if (count % 3 == 0) answer.push(run());
		else answer.push(0);
		tc--;
	}
	while (!answer.empty()) {
		cout << answer.front() << endl;
		answer.pop();
	}

	return 0;
}

๋จผ์ € ์–ด๋–ป๊ฒŒ ํ•˜๋ฉด ๋ณด๋“œ๊ฒŒ์ž„ํŒ์„ ๋ฎ์„ ์ˆ˜ ์žˆ์„ ์ง€ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ง์ ‘ ์„ธ์–ด๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค. ์ค‘๋ณต๋˜์ง€ ์•Š๊ฒŒ ์„ธ์–ด์•ผ ํ•˜๋ฏ€๋กœ, ๊ณ ๋“ฑํ•™๊ต ๋•Œ ๊ฒฝ์šฐ์˜ ์ˆ˜ ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ์˜ '์‚ฌ์ „์  ๋ฐฐ์—ด'๊ณผ ๊ฐ™์ด ๊ธฐ์ค€์ด ์žˆ์–ด์•ผ ํ–ˆ๋‹ค.

 

๋‚˜๋Š” โ”˜๋ชจ์–‘ →โ”” ๋ชจ์–‘ →โ”Œ ๋ชจ์–‘ → โ”๋ชจ์–‘์„ ์šฐ์„ ์ˆœ์œ„๋กœ ๋‘์—ˆ๊ณ , ์œ„ 4๊ฐ€์ง€ ํŒจํ„ด์„ ๊ทธ๋ฆฌ๋Š”๋ฐ ์‹คํŒจ ํ•œ๋‹ค๋ฉด ์ด์ „ ๋‹จ๊ณ„๋กœ ๋Œ์•„์™€ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๋ณด๊ณ  ๋‹ค๋ฅธ ์„ ํƒ์ง€๋ฅผ ๊ณจ๋ผ์•ผํ•จ์„ ์•Œ๊ฒŒ๋˜์—ˆ๋‹ค.

 

โ€‹์ด๋Ÿฌํ•œ ์˜์‹์˜ ํ๋ฆ„์„ ๋”ฐ๋ผ ์žฌ๊ท€ํ•˜์—ฌ ์™„์ „ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค. ์กฐ๊ฐ์˜ ๋ชจ์–‘์„ 3์ฐจ์› ๋ฐฐ์—ด์— ์ €์žฅํ•˜์—ฌ ํ’€์ดํ–ˆ๋‹ค๋ฉด ์ฝ”๋“œ๊ฐ€ ๋” ์งง์•„์กŒ์„ ์ˆ˜๋„ ์žˆ์—ˆ๊ฒ ๋‹ค ์ƒ๊ฐํ–ˆ๋‹ค.

 

 

 

ruhz3/CodingTest

To prepare for coding test. Contribute to ruhz3/CodingTest development by creating an account on GitHub.

github.com