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

[์•Œ๊ณ ๋ฆฌ์ฆ˜] ์—ฐ๊ตฌ์†Œ (๋ฐฑ์ค€, 14502)

ruhz 2020. 11. 20. 15:36

๋ฌธ์ œ

 

14502๋ฒˆ: ์—ฐ๊ตฌ์†Œ

์ธ์ฒด์— ์น˜๋ช…์ ์ธ ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ์—ฐ๊ตฌํ•˜๋˜ ์—ฐ๊ตฌ์†Œ์—์„œ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์œ ์ถœ๋˜์—ˆ๋‹ค. ๋‹คํ–‰ํžˆ ๋ฐ”์ด๋Ÿฌ์Šค๋Š” ์•„์ง ํผ์ง€์ง€ ์•Š์•˜๊ณ , ๋ฐ”์ด๋Ÿฌ์Šค์˜ ํ™•์‚ฐ์„ ๋ง‰๊ธฐ ์œ„ํ•ด์„œ ์—ฐ๊ตฌ์†Œ์— ๋ฒฝ์„ ์„ธ์šฐ๋ ค๊ณ  ํ•œ๋‹ค. ์—ฐ๊ตฌ์†Œ๋Š” ํฌ

www.acmicpc.net

 

 

ํ’€์ด

 

ruhz3/CodingTest

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

github.com

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

int row, col;
int map[8][8];
bool check[8][8];

/*๊ฐ์—ผ*/
void infection(int R, int C) {
	if (R > 0 && !check[R - 1][C] && map[R - 1][C] == 0) {
		check[R - 1][C] = true;
		infection(R - 1, C);
	}
	if (R < row && !check[R + 1][C] && map[R + 1][C] == 0) {
		check[R + 1][C] = true;
		infection(R + 1, C);
	}
	if (C > 0 && !check[R][C - 1] && map[R][C - 1] == 0) {
		check[R][C - 1] = true;
		infection(R, C - 1);
	}
	if (C < col && !check[R][C + 1] && map[R][C + 1] == 0) {
		check[R][C + 1] = true;
		infection(R, C + 1);
	}
}

/*๋ฒฝ ์„ธ์šฐ๊ธฐ*/
int setWall(int x, int y, int count) {
	int result = 0;
	// ๋ฒฝ์„ 3๊ฐœ ๋ชจ๋‘ ์„ธ์šฐ๋ฉด,
	if (count == 3) {
		// ๋ฐ”์ด๋Ÿฌ์Šค ์ฐพ์•„์„œ ๊ฐ์—ผํ•จ์ˆ˜ ํ˜ธ์ถœํ•˜๊ณ 
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < col; j++) {
				if (map[i][j] == 2) infection(i, j);
			}
		}
		// ์•ˆ์ „์ง€์—ญ ๊ฒ€์‚ฌํ•ด ๋„“์ด ๋ฐ˜ํ™˜
		for (int i = 0; i < row; i++)
			for (int j = 0; j < col; j++)
				if (map[i][j] == 0 && !check[i][j]) result++;
		memset(check, false, sizeof(check));
		return result;
	}

	// ๋ฒฝ์„ ์„ธ์šฐ๋Š” ๊ฒฝ์šฐ ์žฌ๊ท€ํ˜ธ์ถœ
	int R, C;
	for (int i = col * x + y; i < row * col; i++) {
		R = i / col; C = i % col;
		if (map[R][C] == 0) {
			map[R][C] = 1;
			result = max(result, setWall(R, C, count + 1));
			map[R][C] = 0;
		}
	}
	return result;
}

int main() {
	cin >> row >> col;
	memset(map, 1, sizeof(map));
	memset(check, false, sizeof(check));
	for (int i = 0; i < row; i++) {
		for (int j = 0; j < col; j++) {
			cin >> map[i][j];
		}
	}
	cout << setWall(0, 0, 0) << endl;
	return 0;
}

์ฒ˜์Œ์— ํŒ€์›๊ณผ ์—ญํ• ์„ ๋‚˜๋ˆ ํ’€๋‹ค๊ฐ€, ๊ตฌํ˜„์„ ์„œ๋กœ ๋‹ค๋ฅด๊ฒŒ ํ•˜์—ฌ ํ˜ผ์„ ์ด ์กฐ๊ธˆ ์žˆ์—ˆ๋‹ค. ์šฐ์™•์ขŒ์™•ํ•˜๋‹ค ๋‚ด๊ฐ€ ๋งก์€ ๋ถ€๋ถ„์—์„œ ๋นต๊พธ๊ฐ€ ๋‚˜์„œ ์ข€ ๋ฏธ์•ˆํ–ˆ๋‹ค. ์—ญํ• ์„ ๋‚˜๋ˆ„๋Š” ๊ฒฝ์šฐ ํŒ€์›๊ณผ ๋ช…ํ™•ํ•˜๊ฒŒ ๊ตฌ์ƒ์„ ํ•˜๊ณ  ์ฝ”๋”ฉ์— ์ฐฉ์ˆ˜ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค๋Š” ๊ตํ›ˆ์„ ์–ป์—ˆ๋‹ค. ์ง‘์— ์™€์„œ ๊ตฌํ˜„ํ•˜๋ ค ํ–ˆ๋˜ ๋ฐฉํ–ฅ๋Œ€๋กœ ๊ตฌํ˜„ํ•ด ๋ณด์•˜๋‹ค.