-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBOJ_14502_1.cpp
108 lines (80 loc) · 1.77 KB
/
BOJ_14502_1.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
#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
#define MAX_SIZE 8
typedef pair<int, int> P;
vector<P> zero;//빈 공간
vector<P> virus;//바이러스
queue<P> q;
int N, M, MAX, cnt;
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 };
void BFS() {
int xp, yp, size;
while (!q.empty()) {
size = q.size();
for (int i = 0; i < size; i++) {
xp = q.front().first;
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 (tmp[xp2][yp2] == 1 || tmp[xp2][yp2] == 2) continue;
tmp[xp2][yp2] = 2;
q.push(P(xp2, yp2));
}
}
}
}
int main(void) {
ios::sync_with_stdio(false);
cin >> N >> M;
MAX = 0;
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));
}
}
}
int size = zero.size();
for (int k = 0; k < size - 2; k++) {
for (int i = k+1; i <size - 1; i++) {
for (int j = i+1; j < size; j++) {
for (int l = 0; l < N; l++) {
for (int m = 0; m < M; m++) {
tmp[l][m] = map[l][m];
}
}
tmp[zero[k].first][zero[k].second] = 1;
tmp[zero[i].first][zero[i].second] = 1;
tmp[zero[j].first][zero[j].second] = 1;
for (int l = 0; l < virus.size(); l++) {
q.push(P(virus[l].first, virus[l].second));
}
cnt = 0;
BFS();
for (int l = 0; l < N; l++) {
for (int m = 0; m < M; m++) {
if (tmp[l][m] == 0) {
cnt++;
}
}
}
MAX = max(MAX, cnt);
}
}
}
cout << MAX;
return 0;
}