#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 30;
int di[4] = {1, -1, 0, 0};
int dj[4] = {0, 0, 1, -1};
int main() {
cin.tie(0)->sync_with_stdio(0);
int N, S;
cin >> N >> S;
vector<string> grid(N);
for (int i=0; i<N; i++) {
cin >> grid[i];
}
vector<vector<int>> dist(N, vector<int>(N, inf));
queue<pair<int,int>> Q;
for (int i=0; i<N; i++) {
for (int j=0; j<N; j++) {
if (grid[i][j] == 'H') {
dist[i][j] = 0;
Q.push({i, j});
}
}
}
while (Q.size()) {
auto [i, j] = Q.front(); Q.pop();
for (int k=0; k<4; k++) {
int i2 = i + di[k];
int j2 = j + dj[k];
if (i2 < 0 || j2 < 0 || i2 >= N || j2 >= N) continue;
if (grid[i2][j2] != 'G') continue;
if (dist[i2][j2] <= dist[i][j]) continue;
dist[i2][j2] = dist[i][j] + 1;
Q.push({i2, j2});
}
}
pair<int,int> begin, end;
for (int i=0; i<N; i++) {
for (int j=0; j<N; j++) {
if (grid[i][j] == 'M') {
begin = {i, j};
} else if (grid[i][j] == 'D') {
end = {i, j};
}
}
}
int lo = 0, hi = 1 << 30, ans = 0;
while (lo <= hi) {
int mid = (lo + hi) / 2;
// mid = number of steps the bees have progressed already
vector<vector<int>> dist2(N, vector<int>(N, inf));
dist2[begin.first][begin.second] = 0;
queue<pair<int,int>> Q;
Q.push(begin);
while (Q.size()) {
auto [i, j] = Q.front(); Q.pop();
for (int k=0; k<4; k++) {
int i2 = i + di[k];
int j2 = j + dj[k];
if (i2 < 0 || j2 < 0 || i2 >= N || j2 >= N) continue;
if (grid[i2][j2] != 'G' && grid[i2][j2] != 'D') continue;
if (dist2[i2][j2] <= dist2[i][j]) continue;
int T = mid + (dist2[i][j] + 1) / S;
if (dist[i2][j2] <= T) continue;
dist2[i2][j2] = dist2[i][j] + 1;
Q.push({i2, j2});
}
}
if (dist2[end.first][end.second] == inf) {
hi = mid - 1;
} else {
ans = mid;
lo = mid + 1;
}
}
cout << ans << "\n";
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:28:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
28 | auto [i, j] = Q.front(); Q.pop();
| ^
mecho.cpp:58:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
58 | auto [i, j] = Q.front(); Q.pop();
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Runtime error |
212 ms |
65536 KB |
Execution killed with signal 9 |
8 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
13 |
Runtime error |
332 ms |
65536 KB |
Execution killed with signal 9 |
14 |
Runtime error |
469 ms |
65536 KB |
Execution killed with signal 9 |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
356 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
6 ms |
1572 KB |
Output is correct |
34 |
Correct |
6 ms |
1576 KB |
Output is correct |
35 |
Runtime error |
189 ms |
65536 KB |
Execution killed with signal 9 |
36 |
Correct |
7 ms |
1884 KB |
Output is correct |
37 |
Correct |
7 ms |
1884 KB |
Output is correct |
38 |
Runtime error |
191 ms |
65536 KB |
Execution killed with signal 9 |
39 |
Correct |
10 ms |
2296 KB |
Output is correct |
40 |
Correct |
10 ms |
2380 KB |
Output is correct |
41 |
Runtime error |
188 ms |
65536 KB |
Execution killed with signal 9 |
42 |
Correct |
15 ms |
2872 KB |
Output is correct |
43 |
Correct |
13 ms |
2732 KB |
Output is correct |
44 |
Runtime error |
187 ms |
65536 KB |
Execution killed with signal 9 |
45 |
Correct |
14 ms |
3304 KB |
Output is correct |
46 |
Correct |
14 ms |
3180 KB |
Output is correct |
47 |
Runtime error |
185 ms |
65536 KB |
Execution killed with signal 9 |
48 |
Correct |
17 ms |
3812 KB |
Output is correct |
49 |
Correct |
17 ms |
3716 KB |
Output is correct |
50 |
Runtime error |
185 ms |
65536 KB |
Execution killed with signal 9 |
51 |
Correct |
20 ms |
4320 KB |
Output is correct |
52 |
Correct |
20 ms |
4332 KB |
Output is correct |
53 |
Runtime error |
195 ms |
65536 KB |
Execution killed with signal 9 |
54 |
Correct |
30 ms |
4964 KB |
Output is correct |
55 |
Correct |
26 ms |
4964 KB |
Output is correct |
56 |
Runtime error |
190 ms |
65536 KB |
Execution killed with signal 9 |
57 |
Correct |
25 ms |
5608 KB |
Output is correct |
58 |
Correct |
24 ms |
5604 KB |
Output is correct |
59 |
Runtime error |
193 ms |
65536 KB |
Execution killed with signal 9 |
60 |
Correct |
30 ms |
6384 KB |
Output is correct |
61 |
Correct |
29 ms |
6296 KB |
Output is correct |
62 |
Runtime error |
186 ms |
65536 KB |
Execution killed with signal 9 |
63 |
Correct |
101 ms |
6272 KB |
Output is correct |
64 |
Correct |
154 ms |
6256 KB |
Output is correct |
65 |
Correct |
149 ms |
6304 KB |
Output is correct |
66 |
Correct |
119 ms |
6272 KB |
Output is correct |
67 |
Incorrect |
124 ms |
6400 KB |
Output isn't correct |
68 |
Correct |
55 ms |
6320 KB |
Output is correct |
69 |
Correct |
52 ms |
6452 KB |
Output is correct |
70 |
Correct |
47 ms |
6288 KB |
Output is correct |
71 |
Correct |
41 ms |
6328 KB |
Output is correct |
72 |
Incorrect |
44 ms |
6332 KB |
Output isn't correct |
73 |
Runtime error |
369 ms |
65536 KB |
Execution killed with signal 9 |
74 |
Runtime error |
354 ms |
65536 KB |
Execution killed with signal 9 |
75 |
Runtime error |
359 ms |
65536 KB |
Execution killed with signal 9 |
76 |
Runtime error |
364 ms |
65536 KB |
Execution killed with signal 9 |
77 |
Runtime error |
363 ms |
65536 KB |
Execution killed with signal 9 |
78 |
Runtime error |
322 ms |
65536 KB |
Execution killed with signal 9 |
79 |
Runtime error |
299 ms |
65536 KB |
Execution killed with signal 9 |
80 |
Runtime error |
303 ms |
65536 KB |
Execution killed with signal 9 |
81 |
Runtime error |
305 ms |
65536 KB |
Execution killed with signal 9 |
82 |
Runtime error |
305 ms |
65536 KB |
Execution killed with signal 9 |
83 |
Runtime error |
260 ms |
65536 KB |
Execution killed with signal 9 |
84 |
Runtime error |
295 ms |
65536 KB |
Execution killed with signal 9 |
85 |
Runtime error |
251 ms |
65536 KB |
Execution killed with signal 9 |
86 |
Runtime error |
254 ms |
65536 KB |
Execution killed with signal 9 |
87 |
Runtime error |
277 ms |
65536 KB |
Execution killed with signal 9 |
88 |
Runtime error |
227 ms |
65536 KB |
Execution killed with signal 9 |
89 |
Runtime error |
230 ms |
65536 KB |
Execution killed with signal 9 |
90 |
Runtime error |
229 ms |
65536 KB |
Execution killed with signal 9 |
91 |
Runtime error |
230 ms |
65536 KB |
Execution killed with signal 9 |
92 |
Runtime error |
224 ms |
65536 KB |
Execution killed with signal 9 |