๐Ÿ™‡โ€โ™€๏ธ[Gold IV] ๊ฐ์‹œ - 15683

๋ฌธ์ œ ๋งํฌ

์„ฑ๋Šฅ ์š”์•ฝ

๋ฉ”๋ชจ๋ฆฌ: 2024 KB, ์‹œ๊ฐ„: 24 ms

๋ถ„๋ฅ˜

๋ฐฑํŠธ๋ž˜ํ‚น, ๋ธŒ๋ฃจํŠธํฌ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜, ๊ตฌํ˜„, ์‹œ๋ฎฌ๋ ˆ์ด์…˜

์ œ์ถœ ์ผ์ž

2024๋…„ 2์›” 9์ผ 23:08:06

๋ฌธ์ œ ์„ค๋ช…

์Šคํƒ€ํŠธ๋งํฌ์˜ ์‚ฌ๋ฌด์‹ค์€ 1ร—1ํฌ๊ธฐ์˜ ์ •์‚ฌ๊ฐํ˜•์œผ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ๋Š” Nร—M ํฌ๊ธฐ์˜ ์ง์‚ฌ๊ฐํ˜•์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ์‚ฌ๋ฌด์‹ค์—๋Š” ์ด K๊ฐœ์˜ CCTV๊ฐ€ ์„ค์น˜๋˜์–ด์ ธ ์žˆ๋Š”๋ฐ, CCTV๋Š” 5๊ฐ€์ง€ ์ข…๋ฅ˜๊ฐ€ ์žˆ๋‹ค. ๊ฐ CCTV๊ฐ€ ๊ฐ์‹œํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

1๋ฒˆ 2๋ฒˆ 3๋ฒˆ 4๋ฒˆ 5๋ฒˆ

1๋ฒˆ CCTV๋Š” ํ•œ ์ชฝ ๋ฐฉํ–ฅ๋งŒ ๊ฐ์‹œํ•  ์ˆ˜ ์žˆ๋‹ค. 2๋ฒˆ๊ณผ 3๋ฒˆ์€ ๋‘ ๋ฐฉํ–ฅ์„ ๊ฐ์‹œํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, 2๋ฒˆ์€ ๊ฐ์‹œํ•˜๋Š” ๋ฐฉํ–ฅ์ด ์„œ๋กœ ๋ฐ˜๋Œ€๋ฐฉํ–ฅ์ด์–ด์•ผ ํ•˜๊ณ , 3๋ฒˆ์€ ์ง๊ฐ ๋ฐฉํ–ฅ์ด์–ด์•ผ ํ•œ๋‹ค. 4๋ฒˆ์€ ์„ธ ๋ฐฉํ–ฅ, 5๋ฒˆ์€ ๋„ค ๋ฐฉํ–ฅ์„ ๊ฐ์‹œํ•  ์ˆ˜ ์žˆ๋‹ค.

CCTV๋Š” ๊ฐ์‹œํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉํ–ฅ์— ์žˆ๋Š” ์นธ ์ „์ฒด๋ฅผ ๊ฐ์‹œํ•  ์ˆ˜ ์žˆ๋‹ค. ์‚ฌ๋ฌด์‹ค์—๋Š” ๋ฒฝ์ด ์žˆ๋Š”๋ฐ, CCTV๋Š” ๋ฒฝ์„ ํ†ต๊ณผํ•  ์ˆ˜ ์—†๋‹ค. CCTV๊ฐ€ ๊ฐ์‹œํ•  ์ˆ˜ ์—†๋Š” ์˜์—ญ์€ ์‚ฌ๊ฐ์ง€๋Œ€๋ผ๊ณ  ํ•œ๋‹ค.

CCTV๋Š” ํšŒ์ „์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š”๋ฐ, ํšŒ์ „์€ ํ•ญ์ƒ 90๋„ ๋ฐฉํ–ฅ์œผ๋กœ ํ•ด์•ผ ํ•˜๋ฉฐ, ๊ฐ์‹œํ•˜๋ ค๊ณ  ํ•˜๋Š” ๋ฐฉํ–ฅ์ด ๊ฐ€๋กœ ๋˜๋Š” ์„ธ๋กœ ๋ฐฉํ–ฅ์ด์–ด์•ผ ํ•œ๋‹ค.

0 0 0 0 0 0
0 0 0 0 0 0
0 0 1 0 6 0
0 0 0 0 0 0

์ง€๋„์—์„œ 0์€ ๋นˆ ์นธ, 6์€ ๋ฒฝ, 1~5๋Š” CCTV์˜ ๋ฒˆํ˜ธ์ด๋‹ค. ์œ„์˜ ์˜ˆ์‹œ์—์„œ 1๋ฒˆ์˜ ๋ฐฉํ–ฅ์— ๋”ฐ๋ผ ๊ฐ์‹œํ•  ์ˆ˜ ์žˆ๋Š” ์˜์—ญ์„ '#'๋กœ ๋‚˜ํƒ€๋‚ด๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

0 0 0 0 0 0
0 0 0 0 0 0
0 0 1 # 6 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
# # 1 0 6 0
0 0 0 0 0 0
0 0 # 0 0 0
0 0 # 0 0 0
0 0 1 0 6 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 1 0 6 0
0 0 # 0 0 0
โ†’ โ† โ†‘ โ†“

CCTV๋Š” ๋ฒฝ์„ ํ†ต๊ณผํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์—, 1๋ฒˆ์ด โ†’ ๋ฐฉํ–ฅ์„ ๊ฐ์‹œํ•˜๊ณ  ์žˆ์„ ๋•Œ๋Š” 6์˜ ์˜ค๋ฅธ์ชฝ์— ์žˆ๋Š” ์นธ์„ ๊ฐ์‹œํ•  ์ˆ˜ ์—†๋‹ค.

0 0 0 0 0 0
0 2 0 0 0 0
0 0 0 0 6 0
0 6 0 0 2 0
0 0 0 0 0 0
0 0 0 0 0 5

์œ„์˜ ์˜ˆ์‹œ์—์„œ ๊ฐ์‹œํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉํ–ฅ์„ ์•Œ์•„๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

0 0 0 0 0 #
# 2 # # # #
0 0 0 0 6 #
0 6 # # 2 #
0 0 0 0 0 #
# # # # # 5
0 0 0 0 0 #
# 2 # # # #
0 0 0 0 6 #
0 6 0 0 2 #
0 0 0 0 # #
# # # # # 5
0 # 0 0 0 #
0 2 0 0 0 #
0 # 0 0 6 #
0 6 # # 2 #
0 0 0 0 0 #
# # # # # 5
0 # 0 0 0 #
0 2 0 0 0 #
0 # 0 0 6 #
0 6 0 0 2 #
0 0 0 0 # #
# # # # # 5
์™ผ์ชฝ ์ƒ๋‹จ 2: โ†”, ์˜ค๋ฅธ์ชฝ ํ•˜๋‹จ 2: โ†” ์™ผ์ชฝ ์ƒ๋‹จ 2: โ†”, ์˜ค๋ฅธ์ชฝ ํ•˜๋‹จ 2: โ†• ์™ผ์ชฝ ์ƒ๋‹จ 2: โ†•, ์˜ค๋ฅธ์ชฝ ํ•˜๋‹จ 2: โ†” ์™ผ์ชฝ ์ƒ๋‹จ 2: โ†•, ์˜ค๋ฅธ์ชฝ ํ•˜๋‹จ 2: โ†•

CCTV๋Š” CCTV๋ฅผ ํ†ต๊ณผํ•  ์ˆ˜ ์žˆ๋‹ค. ์•„๋ž˜ ์˜ˆ์‹œ๋ฅผ ๋ณด์ž.

0 0 2 0 3
0 6 0 0 0
0 0 6 6 0
0 0 0 0 0

์œ„์™€ ๊ฐ™์€ ๊ฒฝ์šฐ์— 2์˜ ๋ฐฉํ–ฅ์ด โ†• 3์˜ ๋ฐฉํ–ฅ์ด โ†์™€ โ†“์ธ ๊ฒฝ์šฐ ๊ฐ์‹œ๋ฐ›๋Š” ์˜์—ญ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

# # 2 # 3
0 6 # 0 #
0 0 6 6 #
0 0 0 0 #

์‚ฌ๋ฌด์‹ค์˜ ํฌ๊ธฐ์™€ ์ƒํƒœ, ๊ทธ๋ฆฌ๊ณ  CCTV์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, CCTV์˜ ๋ฐฉํ–ฅ์„ ์ ์ ˆํžˆ ์ •ํ•ด์„œ, ์‚ฌ๊ฐ ์ง€๋Œ€์˜ ์ตœ์†Œ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์‚ฌ๋ฌด์‹ค์˜ ์„ธ๋กœ ํฌ๊ธฐ N๊ณผ ๊ฐ€๋กœ ํฌ๊ธฐ M์ด ์ฃผ์–ด์ง„๋‹ค. (1 โ‰ค N, M โ‰ค 8)

๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ์‚ฌ๋ฌด์‹ค ๊ฐ ์นธ์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. 0์€ ๋นˆ ์นธ, 6์€ ๋ฒฝ, 1~5๋Š” CCTV๋ฅผ ๋‚˜ํƒ€๋‚ด๊ณ , ๋ฌธ์ œ์—์„œ ์„ค๋ช…ํ•œ CCTV์˜ ์ข…๋ฅ˜์ด๋‹ค.

CCTV์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜๋Š” 8๊ฐœ๋ฅผ ๋„˜์ง€ ์•Š๋Š”๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ์‚ฌ๊ฐ ์ง€๋Œ€์˜ ์ตœ์†Œ ํฌ๊ธฐ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿš€ํ’€์ด

๋ธŒ๋ฃจํŠธํฌ์Šค๋กœ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ํƒ์ƒ‰ํ•ด์„œ ํ’€์—ˆ๋‹ค.

enum Dir
{
	UP,		// 0
	RIGHT,	// 1
	DOWN,	// 2
	LEFT,	// 3
};

int n, m, cctvCount, ch[20], res = 123456789;
vector<vector<int>> board;
vector<vector<int>> visited;
vector<pair<int, int>> cctvs;

int dy[] = { -1, 0, 1, 0 };
int dx[] = { 0, 1, 0, -1 };

์ด๋ ‡๊ฒŒ ์ „์—ญ ๋ณ€์ˆ˜๋ฅผ ์žก์•˜๊ณ , cctv๋งˆ๋‹ค ๊ฐ์‹œํ•˜๋Š” ๋ฒ”์œ„๊ฐ€ ๋‹ค๋ฅด๋ฏ€๋กœ ๊ฐ๊ฐ์˜ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์คฌ๋‹ค.

void CCTV1(int y, int x, int dir)
{
	int curY = y;
	int curX = x;
	while (true)
	{
		int ny = y + dy[dir];
		int nx = x + dx[dir];

		if (ny < 1 || nx < 1 || ny > n || nx > m)
			break;
		if (board[ny][nx] == 6)
			break;

		y = ny;
		x = nx;

		visited[ny][nx] = 1;
	}
}

void CCTV2(int y, int x, int dir)
{
	int curY = y;
	int curX = x;
	while (true)
	{
		int ny = y + dy[dir];
		int nx = x + dx[dir];

		if (ny < 1 || nx < 1 || ny > n || nx > m)
			break;
		if (board[ny][nx] == 6)
			break;

		y = ny;
		x = nx;

		visited[ny][nx] = 1;
	}

	dir = (dir + 2) % 4;
	y = curY;
	x = curX;
	while (true)
	{
		int ny = y + dy[dir];
		int nx = x + dx[dir];

		if (ny < 1 || nx < 1 || ny > n || nx > m)
			break;
		if (board[ny][nx] == 6)
			break;

		y = ny;
		x = nx;

		visited[ny][nx] = 1;
	}
}

void CCTV3(int y, int x, int dir)
{
	int curY = y;
	int curX = x;
	while (true)
	{
		int ny = y + dy[dir];
		int nx = x + dx[dir];

		if (ny < 1 || nx < 1 || ny > n || nx > m)
			break;
		if (board[ny][nx] == 6)
			break;

		y = ny;
		x = nx;

		visited[ny][nx] = 1;
	}

	y = curY;
	x = curX;
	dir = (dir + 1) % 4;
	while (true)
	{
		int ny = y + dy[dir];
		int nx = x + dx[dir];

		if (ny < 1 || nx < 1 || ny > n || nx > m)
			break;
		if (board[ny][nx] == 6)
			break;

		y = ny;
		x = nx;

		visited[ny][nx] = 1;
	}
}

void CCTV4(int y, int x, int dir)
{
	int curY = y;
	int curX = x;
	while (true)
	{
		int ny = y + dy[dir];
		int nx = x + dx[dir];

		if (ny < 1 || nx < 1 || ny > n || nx > m)
			break;
		if (board[ny][nx] == 6)
			break;

		y = ny;
		x = nx;

		visited[ny][nx] = 1;
	}

	y = curY;
	x = curX;
	dir = (dir + 1) % 4;
	while (true)
	{
		int ny = y + dy[dir];
		int nx = x + dx[dir];

		if (ny < 1 || nx < 1 || ny > n || nx > m)
			break;
		if (board[ny][nx] == 6)
			break;

		y = ny;
		x = nx;

		visited[ny][nx] = 1;
	}

	y = curY;
	x = curX;
	dir = (dir + 2) % 4;
	while (true)
	{
		int ny = y + dy[dir];
		int nx = x + dx[dir];

		if (ny < 1 || nx < 1 || ny > n || nx > m)
			break;
		if (board[ny][nx] == 6)
			break;

		y = ny;
		x = nx;

		visited[ny][nx] = 1;
	}
}

void CCTV5(int y, int x, int dir)
{
	int curY = y;
	int curX = x;
	for (int i = 1; i <= 4; ++i)
	{
		while (true)
		{
			int ny = y + dy[dir];
			int nx = x + dx[dir];

			if (ny < 1 || nx < 1 || ny > n || nx > m)
				break;
			if (board[ny][nx] == 6)
				break;

			y = ny;
			x = nx;

			visited[ny][nx] = 1;
		}

		dir = (dir + i) % 4;
		y = curY;
		x = curX;
	}
}

๋ฌธ์ œ์˜ ์กฐ๊ฑด๋Œ€๋กœ ๊ฐ์‹œ๋ฅผ ํ•˜๊ณ  ๋ฐฉํ–ฅ์„ ์„ค์ •ํ•˜๋ฉด ๊ทธ ๋ฐฉํ–ฅ์„ ๊ธฐ์ค€์œผ๋กœ ๊ฐ์ง€ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.

๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜์™€ ๊ฒฐ๊ณผ ํ™•์ธ์„ ์œ„ํ•ด visited๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ํ•จ์ˆ˜๋„ ๋งŒ๋“ค์—ˆ๋‹ค.

void visitedClear()
{
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			visited[i][j] = 0;
		}
	}
}
int checkResult()
{
	int res = 0;

	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			if (board[i][j] == 1 || board[i][j] == 2 || board[i][j] == 3 || board[i][j] == 4 || board[i][j] == 5 || board[i][j] == 6)
				continue;

			if (visited[i][j] == 0 )
				res++;
		}
	}

	return res;
}

๋ชจ๋“  ์ƒํ™ฉ์„ ํƒ์ƒ‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ค‘๋ณต ๊ฐ€๋Šฅํ•œ ์ˆœ์—ด์„ ๋งŒ๋“ค์—ˆ๋‹ค.

DFS๋ผ๋ฅธ ์ด๋ฆ„์œผ๋กœ ๋งŒ๋“ค์—ˆ๋‹ค.

void DFS(int s, int L)
{
	if (L == cctvCount)
	{
		for (int i = 0; i < L; ++i)
		{
			// ch[i] ๊ฐ€ dir
			// i๋Š” cctv์ธ๋ฑ์Šค

			if (board[cctvs[i].first][cctvs[i].second] == 1)
			{
				CCTV1(cctvs[i].first, cctvs[i].second, ch[i]);
			}
			else if (board[cctvs[i].first][cctvs[i].second] == 2)
			{
				CCTV2(cctvs[i].first, cctvs[i].second, ch[i]);
			}
			else if (board[cctvs[i].first][cctvs[i].second] == 3)
			{
				CCTV3(cctvs[i].first, cctvs[i].second, ch[i]);
			}
			else if (board[cctvs[i].first][cctvs[i].second] == 4)
			{
				CCTV4(cctvs[i].first, cctvs[i].second, ch[i]);
			}
			else if (board[cctvs[i].first][cctvs[i].second] == 5)
			{
				CCTV5(cctvs[i].first, cctvs[i].second, ch[i]);
			}
		}

		res = min(res, checkResult());

		//printVisited();

		visitedClear();

		//cout << ch[i] << " ";
		//cout << endl;
	}
	else
	{
		for (int i = 0; i < 4; ++i)
		{
			ch[L] = i;
			DFS(i + 1, L + 1);
		}
	}
}

์œ„์˜ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ์ตœ์ข…์ ์œผ๋กœ solveํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค.

void solve()
{
	cin >> n >> m;
	board = vector<vector<int>>(n + 1, vector<int>(m + 1));
	visited = vector<vector<int>>(n + 1, vector<int>(m + 1));

	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			cin >> board[i][j];
			if (board[i][j] >= 1 && board[i][j] <= 5)
				cctvs.push_back({ i, j });
		}
	}

	cctvCount = cctvs.size();

	DFS(0, 0);

	cout << res;
	// cctv๊ฐ€ 3๊ฐœ
	// ๊ฒฝ์šฐ์˜ ์ˆ˜
	// 0 0 0
	// 0 0 1
	// 0 0 2
	// 0 0 3
	// 0 1 0
	// 0 1 1
	// 0 1 2
	// 0 1 3
	// 0 2 0
	// ...

	// 4 * 4 * 4
	// 64
	
}

๊ณจ๋“œ4 ๋ฌธ์ œ์˜€๋Š”๋ฐ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์„ ์ฐพ์œผ๋ ค๊ณ  ์‹œ๊ฐ„์„ ๋งŽ์ด ์ผ๋‹ค.

๊ฒฐ๊ตญ์—” ๋ฌด์‹ํ•˜๊ฒŒ ํ‘ธ๋Š” ๋ฐฉ์‹์œผ๋กœ ํ•˜๋‚˜ํ•˜๋‚˜ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ํ’€์—ˆ๋‹ค.

๋ฌธ์ œ ์ œ์ถœ์—์„œ๋Š” ํ•œ๋ฒˆ๋„ ํ‹€๋ฆฌ์ง€ ์•Š๊ณ  ๋ฐ”๋กœ ๋งž์ถฐ์„œ ์ข‹์•˜๋‹ค.

๋ฌธ์ œ ํ•ด๊ฒฐ

๐Ÿš€์ „์ฒด ์ฝ”๋“œ

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include<iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <math.h>

using namespace std;

void CCTV1(int y, int x, int dir);
void CCTV2(int y, int x, int dir);
void CCTV3(int y, int x, int dir);
void CCTV4(int y, int x, int dir);
void CCTV5(int y, int x, int dir);
void visitedClear();
int checkResult();
void solve();

// 1๋ฒˆ : ํ•œ์ชฝ ๋ฐฉํ–ฅ๋งŒ ์ฒดํฌ
// 2๋ฒˆ : ์ •๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ์œผ๋กœ ์ฒดํฌ (ํ•œ ๋ผ์ธ ์ฒดํฌ)
// 3๋ฒˆ : ์ง๊ฐ ๋ฐฉํ–ฅ์œผ๋กœ ์ฒดํฌ
// 4๋ฒˆ : ํ•œ ๋ฐฉํ–ฅ์œผ๋กœ ์ œ์™ธํ•˜๊ณ  ์ฒดํฌ
// 5๋ฒˆ : ๋ชจ๋“  ๋ฐฉํ–ฅ ์ฒดํฌ

// cctv๋Š” ์ตœ๋Œ€ 8๊ฐœ์ด๋‹ค.
// ํ•˜๋‚˜์˜ cctv๋Š” ์ตœ๋Œ€ 4๋ฒˆ ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ์กด์žฌํ•œ๋‹ค. -> ๋ธŒ๋ฃจํŠธํฌ์Šค๋กœ ํ•ด๋„ ๋ ๊ฑฐ๊ฐ™๋‹ค.

// ๋ฒˆํ˜ธ๋งˆ๋‹ค ์ฒดํฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ํ•„์š”ํ•˜๋‹ค.

enum Dir
{
	UP,		// 0
	RIGHT,	// 1
	DOWN,	// 2
	LEFT,	// 3
};

int n, m, cctvCount, ch[20], res = 123456789;
vector<vector<int>> board;
vector<vector<int>> visited;
vector<pair<int, int>> cctvs;

int dy[] = { -1, 0, 1, 0 };
int dx[] = { 0, 1, 0, -1 };

void CCTV1(int y, int x, int dir)
{
	int curY = y;
	int curX = x;
	while (true)
	{
		int ny = y + dy[dir];
		int nx = x + dx[dir];

		if (ny < 1 || nx < 1 || ny > n || nx > m)
			break;
		if (board[ny][nx] == 6)
			break;

		y = ny;
		x = nx;

		visited[ny][nx] = 1;
	}
}

void CCTV2(int y, int x, int dir)
{
	int curY = y;
	int curX = x;
	while (true)
	{
		int ny = y + dy[dir];
		int nx = x + dx[dir];

		if (ny < 1 || nx < 1 || ny > n || nx > m)
			break;
		if (board[ny][nx] == 6)
			break;

		y = ny;
		x = nx;

		visited[ny][nx] = 1;
	}

	dir = (dir + 2) % 4;
	y = curY;
	x = curX;
	while (true)
	{
		int ny = y + dy[dir];
		int nx = x + dx[dir];

		if (ny < 1 || nx < 1 || ny > n || nx > m)
			break;
		if (board[ny][nx] == 6)
			break;

		y = ny;
		x = nx;

		visited[ny][nx] = 1;
	}
}

void CCTV3(int y, int x, int dir)
{
	int curY = y;
	int curX = x;
	while (true)
	{
		int ny = y + dy[dir];
		int nx = x + dx[dir];

		if (ny < 1 || nx < 1 || ny > n || nx > m)
			break;
		if (board[ny][nx] == 6)
			break;

		y = ny;
		x = nx;

		visited[ny][nx] = 1;
	}

	y = curY;
	x = curX;
	dir = (dir + 1) % 4;
	while (true)
	{
		int ny = y + dy[dir];
		int nx = x + dx[dir];

		if (ny < 1 || nx < 1 || ny > n || nx > m)
			break;
		if (board[ny][nx] == 6)
			break;

		y = ny;
		x = nx;

		visited[ny][nx] = 1;
	}
}

void CCTV4(int y, int x, int dir)
{
	int curY = y;
	int curX = x;
	while (true)
	{
		int ny = y + dy[dir];
		int nx = x + dx[dir];

		if (ny < 1 || nx < 1 || ny > n || nx > m)
			break;
		if (board[ny][nx] == 6)
			break;

		y = ny;
		x = nx;

		visited[ny][nx] = 1;
	}

	y = curY;
	x = curX;
	dir = (dir + 1) % 4;
	while (true)
	{
		int ny = y + dy[dir];
		int nx = x + dx[dir];

		if (ny < 1 || nx < 1 || ny > n || nx > m)
			break;
		if (board[ny][nx] == 6)
			break;

		y = ny;
		x = nx;

		visited[ny][nx] = 1;
	}

	y = curY;
	x = curX;
	dir = (dir + 2) % 4;
	while (true)
	{
		int ny = y + dy[dir];
		int nx = x + dx[dir];

		if (ny < 1 || nx < 1 || ny > n || nx > m)
			break;
		if (board[ny][nx] == 6)
			break;

		y = ny;
		x = nx;

		visited[ny][nx] = 1;
	}
}

void CCTV5(int y, int x, int dir)
{
	int curY = y;
	int curX = x;
	for (int i = 1; i <= 4; ++i)
	{
		while (true)
		{
			int ny = y + dy[dir];
			int nx = x + dx[dir];

			if (ny < 1 || nx < 1 || ny > n || nx > m)
				break;
			if (board[ny][nx] == 6)
				break;

			y = ny;
			x = nx;

			visited[ny][nx] = 1;
		}

		dir = (dir + i) % 4;
		y = curY;
		x = curX;
	}
}

void visitedClear()
{
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			visited[i][j] = 0;
		}
	}
}

int checkResult()
{
	int res = 0;

	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			if (board[i][j] == 1 || board[i][j] == 2 || board[i][j] == 3 || board[i][j] == 4 || board[i][j] == 5 || board[i][j] == 6)
				continue;

			if (visited[i][j] == 0 )
				res++;
		}
	}

	return res;
}

void printVisited()
{
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			cout << visited[i][j] << " ";
		}
		cout << endl;
	}

	cout << endl;
}

void DFS(int s, int L)
{
	if (L == cctvCount)
	{
		for (int i = 0; i < L; ++i)
		{
			// ch[i] ๊ฐ€ dir
			// i๋Š” cctv์ธ๋ฑ์Šค

			if (board[cctvs[i].first][cctvs[i].second] == 1)
			{
				CCTV1(cctvs[i].first, cctvs[i].second, ch[i]);
			}
			else if (board[cctvs[i].first][cctvs[i].second] == 2)
			{
				CCTV2(cctvs[i].first, cctvs[i].second, ch[i]);
			}
			else if (board[cctvs[i].first][cctvs[i].second] == 3)
			{
				CCTV3(cctvs[i].first, cctvs[i].second, ch[i]);
			}
			else if (board[cctvs[i].first][cctvs[i].second] == 4)
			{
				CCTV4(cctvs[i].first, cctvs[i].second, ch[i]);
			}
			else if (board[cctvs[i].first][cctvs[i].second] == 5)
			{
				CCTV5(cctvs[i].first, cctvs[i].second, ch[i]);
			}
		}

		res = min(res, checkResult());

		//printVisited();

		visitedClear();

		//cout << ch[i] << " ";
		//cout << endl;
	}
	else
	{
		for (int i = 0; i < 4; ++i)
		{
			ch[L] = i;
			DFS(i + 1, L + 1);
		}
	}
}

void solve()
{
	cin >> n >> m;
	board = vector<vector<int>>(n + 1, vector<int>(m + 1));
	visited = vector<vector<int>>(n + 1, vector<int>(m + 1));

	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			cin >> board[i][j];
			if (board[i][j] >= 1 && board[i][j] <= 5)
				cctvs.push_back({ i, j });
		}
	}

	cctvCount = cctvs.size();

	DFS(0, 0);

	cout << res;
	// cctv๊ฐ€ 3๊ฐœ
	// ๊ฒฝ์šฐ์˜ ์ˆ˜
	// 0 0 0
	// 0 0 1
	// 0 0 2
	// 0 0 3
	// 0 1 0
	// 0 1 1
	// 0 1 2
	// 0 1 3
	// 0 2 0
	// ...

	// 4 * 4 * 4
	// 64
	
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	freopen("input.txt", "rt", stdin);

	solve();

	return 0;
}

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ: