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;
}
메모
문제 정리
- 3 <= N, M <= 8
- 2 <= 바이러스 수 <= 10
3 <= 빈칸 수
- N*M 월드
- 0: 빈 칸
- 1: 벽
- 벽 3개를 새로 세운다
- 2: 바이러스
- 바이러스는 상하좌우 퍼져나간다
바이러스가 없는 곳 안전 영역
풀이
- 3중 for문
- 3개의 벽을 세우고
- 바이러스 BFS
- 최대 안전 영역 = max(최대 안전 영역, 현재 안전 영역)
메모
- Column Row 신경쓸 것
- 1151 -> 1156
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.