-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBOJ_14502_2.cpp
129 lines (93 loc) · 1.91 KB
/
BOJ_14502_2.cpp
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
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
#define MAX_SIZE 500
typedef pair<int, int> P;
int map[MAX_SIZE][MAX_SIZE];
int tmp[MAX_SIZE][MAX_SIZE];
int dx[4] = { -1, 1, 0, 0 };
int dy[4] = { 0, 0, -1, 1 };
vector<P> zero;
vector<P> virus;
queue<P> q;
int n, m, cnt, MAX;
void BFS() {
int size;
while (!q.empty()) {
size = q.size();
for (int i = 0; i < size; i++) {
int xp = q.front().first;
int yp = q.front().second;
q.pop();
for (int j = 0; j < 4; j++) {
int xp2 = xp + dx[j];
int yp2 = yp + dy[j];
if (xp2 < 0 || yp2 < 0 || n <= xp2 || m <= yp2) continue;
if (map[xp2][yp2] == 1 || map[xp2][yp2] == 2) continue;
map[xp2][yp2] = 2;
q.push(P(xp2, yp2));
}
}
}
}
int get_area() {
int ret = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (map[i][j] == 0) ret++;
}
}
return ret;
}
void dfs(int idx, int d) {
if (d == 3) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
tmp[i][j] = map[i][j];
}
}
for (int i = 0; i < virus.size(); i++) {
q.push(P(virus[i].first, virus[i].second));
}
BFS();
cnt = get_area();
MAX = max(MAX, cnt);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
map[i][j] = tmp[i][j];
}
}
return;
}
for (int i = idx + 1; i < zero.size(); i++) {
int x = zero[i].first;
int y = zero[i].second;
map[x][y] = 1;
dfs(i, d + 1);
map[x][y] = 0;
}
}
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> map[i][j];
if (map[i][j] == 0) zero.push_back(P(i, j));
if (map[i][j] == 2) virus.push_back(P(i, j));
}
}
MAX = 0;
for (int i = 0; i < zero.size(); i++) {
int x = zero[i].first;
int y = zero[i].second;
map[x][y] = 1;
dfs(i, 1);
map[x][y] = 0;
}
cout << MAX;
return 0;
}