-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path210209_BOJ_17141.cpp
More file actions
133 lines (102 loc) · 2.29 KB
/
Copy path210209_BOJ_17141.cpp
File metadata and controls
133 lines (102 loc) · 2.29 KB
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
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int INF = 987654321;
const int VIRUS = -2;
const int WALL = -1;
int N = 0;
int M = 0;
vector <vector<int>> v(51, vector<int>(51));
vector <vector<int>> temp(51, vector<int>(51));
bool visit[51][51];
int dir[4][2]{ {-1, 0}, {0, 1}, {1, 0}, {0, -1} };
vector<pair<int, int>> virus;
int Min = 987654321;
void bfs(int a, int b) {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
visit[i][j] = false;
}
}
queue<pair<pair<int, int>, int>> q;
q.push({ {a, b}, 0 });
visit[a][b] = true;
while (!q.empty()) {
int x = q.front().first.first;
int y = q.front().first.second;
int time = q.front().second;
q.pop();
for (int i = 0; i < 4; ++i) {
int n_x = x + dir[i][0];
int n_y = y + dir[i][1];
if (!(n_x >= 0 && n_x < N && n_y >= 0 && n_y < N))
continue;
if (temp[n_x][n_y] == VIRUS || temp[n_x][n_y] == WALL || visit[n_x][n_y])
continue;
if (temp[n_x][n_y] <= time + 1)
continue;
visit[n_x][n_y] = true;
q.push({ {n_x, n_y}, time + 1 });
temp[n_x][n_y] = time + 1;
}
}
//cout << "\n";
//for (int i = 0; i < N; ++i) {
// for (int j = 0; j < N; ++j) {
// cout << temp[i][j] << "\t";
// }
// cout << "\n";
//}
//cout << "\n";
}
void findAnswer(vector<int> t, int previous) {
if (t.size() == M) {
temp = v;
for (int i = 0; i < M; ++i) {
temp[virus[t[i]].first][virus[t[i]].second] = -2;
}
for (int i = 0; i < M; ++i) {
bfs(virus[t[i]].first, virus[t[i]].second);
}
int Max = 0;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if (temp[i][j] == WALL || temp[i][j] == VIRUS)
continue;
if (temp[i][j] > Max)
Max = temp[i][j];
}
}
if (Min > Max)
Min = Max;
return;
}
for (int i = previous + 1; i < virus.size(); ++i) {
t.push_back(i);
findAnswer(t, i);
t.pop_back();
}
}
int main() {
ios::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 < N; ++j) {
cin >> v[i][j];
v[i][j] = v[i][j] * -1;
if (v[i][j] == VIRUS) {
virus.push_back({ i, j });
v[i][j] = 0;
}
if (v[i][j] == 0)
v[i][j] = INF;
}
}
findAnswer({}, -1);
if (Min == INF)
cout << -1 << "\n";
else
cout << Min << "\n";
return 0;
}