ํฌ์ŠคํŠธ

Baekjoon 14502 - ์—ฐ๊ตฌ์†Œ

Baekjoon 14502 - ์—ฐ๊ตฌ์†Œ

๐Ÿ’ซ ๋ฌธ์ œ


๐Ÿซง Example Input/Output

1
2
3
4
5
6
7
8
9
10
11
// IN
7 7 // N, M
2 0 0 0 1 1 0 // ๊ฐ ์นธ ์ •๋ณด
0 0 1 0 1 2 0 // 0 = ๋นˆ์นธ
0 1 1 0 1 0 0 // 1 = ๋ฒฝ
0 1 0 0 0 0 0 // 2 = ๋ฐ”์ด๋Ÿฌ์Šค
0 0 0 0 0 1 1
0 1 0 0 0 0 0
0 1 0 0 0 0 0
// OUT
27 // ์ตœ๋Œ€ ์•ˆ์ „ ๊ตฌ์—ญ ์ˆ˜

๐Ÿ’ซ C++ ํ’€์ด


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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <bits/stdc++.h>
using namespace std;

const int MX = 10;
int world[MX][MX];
bool canMove[MX][MX];

int n, m;

int sim()
{
	queue<pair<int, int>> q;

	// Init
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			canMove[i][j] = world[i][j] == 0;

			if (world[i][j] == 2)
			{
				q.push({i, j});
			}
		}
	}

	// BFS
	while (q.empty() == false)
	{
		pair<int, int> cur = q.front();
		q.pop();

		int r = cur.first;
		int c = cur.second;

		if (r - 1 >= 0 && canMove[r - 1][c])
		{
			canMove[r - 1][c] = false;
			q.push({r - 1, c});
		}

		if (r + 1 < n && canMove[r + 1][c])
		{
			canMove[r + 1][c] = false;
			q.push({r + 1, c});
		}

		if (c - 1 >= 0 && canMove[r][c - 1])
		{
			canMove[r][c - 1] = false;
			q.push({r, c - 1});
		}

		if (c + 1 < m && canMove[r][c + 1])
		{
			canMove[r][c + 1] = false;
			q.push({r, c + 1});
		}
	}

	// Result
	int emptyCount = 0;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			if (canMove[i][j])
			{
				emptyCount++;
			}
		}
	}

	return emptyCount;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> n >> m;

	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cin >> world[i][j];
		}
	}

	int maxCoord = n * m;
	int maxVirus = 0;

	for (int first = 0; first < maxCoord; first++)
	{
		int firstY = first % m;
		int firstX = first / m;

		if (world[firstX][firstY] != 0)
			continue;

		world[firstX][firstY] = 1;

		for (int second = first + 1; second < maxCoord; second++)
		{
			int secondY = second % m;
			int secondX = second / m;

			if (world[secondX][secondY] != 0)
				continue;

			world[secondX][secondY] = 1;

			for (int third = second + 1; third < maxCoord; third++)
			{
				int thirdY = third % m;
				int thirdX = third / m;

				if (world[thirdX][thirdY] != 0)
					continue;

				world[thirdX][thirdY] = 1;

				maxVirus = max(maxVirus, sim());

				world[thirdX][thirdY] = 0;
			}

			world[secondX][secondY] = 0;
		}

		world[firstX][firstY] = 0;
	}

	cout << maxVirus;
}

๐Ÿ’ซ Memo


๐Ÿซง ๋ฌธ์ œ ์ •๋ฆฌ

  • 3 <= N, M <= 8
  • 2 <= ๋ฐ”์ด๋Ÿฌ์Šค ์ˆ˜ <= 10
  • 3 <= ๋นˆ์นธ ์ˆ˜

  • N*M ์›”๋“œ
    • 0 : ๋นˆ ์นธ
    • 1 : ๋ฒฝ
      • ๋ฒฝ 3๊ฐœ๋ฅผ ์ƒˆ๋กœ ์„ธ์šด๋‹ค
    • 2 : ๋ฐ”์ด๋Ÿฌ์Šค
      • ๋ฐ”์ด๋Ÿฌ์Šค๋Š” ์ƒํ•˜์ขŒ์šฐ ํผ์ ธ๋‚˜๊ฐ„๋‹ค

๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์—†๋Š” ๊ณณ ์•ˆ์ „ ์˜์—ญ

๐Ÿซง ํ’€์ด

  • 3์ค‘ for๋ฌธ
    1. 3๊ฐœ์˜ ๋ฒฝ์„ ์„ธ์šฐ๊ณ 
    2. ๋ฐ”์ด๋Ÿฌ์Šค BFS
    3. ์ตœ๋Œ€ ์•ˆ์ „ ์˜์—ญ = max(์ตœ๋Œ€ ์•ˆ์ „ ์˜์—ญ, ํ˜„์žฌ ์•ˆ์ „ ์˜์—ญ)

๐Ÿซง ๋ฉ”๋ชจ

  • Column Row ์‹ ๊ฒฝ์“ธ ๊ฒƒ
  • 1151 -> 1156
์ด ๊ธฐ์‚ฌ๋Š” ์ €์ž‘๊ถŒ์ž์˜ CC BY 4.0 ๋ผ์ด์„ผ์Šค๋ฅผ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค.