#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};
const int maxn = 800;
char grid[maxn][maxn];
int dist[maxn][maxn];
int dist2[maxn][maxn];
int main() {
cin.tie(0)->sync_with_stdio(0);
int N, S;
cin >> N >> S;
for (int i=0; i<N; i++) {
for (int j=0; j<N; j++) {
cin >> grid[i][j];
dist[i][j] = 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
for (int i=0; i<N; i++) {
for (int j=0; j<N; j++) {
dist2[i][j] = inf;
}
}
dist2[begin.first][begin.second] = 0;
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:34:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
34 | auto [i, j] = Q.front(); Q.pop();
| ^
mecho.cpp:67:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
67 | auto [i, j] = Q.front(); Q.pop();
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2648 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Runtime error |
211 ms |
65536 KB |
Execution killed with signal 9 |
8 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
13 |
Runtime error |
354 ms |
65536 KB |
Execution killed with signal 9 |
14 |
Runtime error |
428 ms |
65536 KB |
Execution killed with signal 9 |
15 |
Correct |
1 ms |
2648 KB |
Output is correct |
16 |
Correct |
1 ms |
2648 KB |
Output is correct |
17 |
Correct |
1 ms |
2392 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
1 ms |
2652 KB |
Output is correct |
25 |
Correct |
1 ms |
4700 KB |
Output is correct |
26 |
Correct |
2 ms |
4700 KB |
Output is correct |
27 |
Correct |
1 ms |
4696 KB |
Output is correct |
28 |
Correct |
1 ms |
4700 KB |
Output is correct |
29 |
Correct |
1 ms |
4700 KB |
Output is correct |
30 |
Correct |
1 ms |
4700 KB |
Output is correct |
31 |
Correct |
1 ms |
4700 KB |
Output is correct |
32 |
Correct |
1 ms |
4700 KB |
Output is correct |
33 |
Correct |
6 ms |
4956 KB |
Output is correct |
34 |
Correct |
7 ms |
5020 KB |
Output is correct |
35 |
Runtime error |
187 ms |
65536 KB |
Execution killed with signal 9 |
36 |
Correct |
7 ms |
4952 KB |
Output is correct |
37 |
Correct |
7 ms |
4956 KB |
Output is correct |
38 |
Runtime error |
190 ms |
65536 KB |
Execution killed with signal 9 |
39 |
Correct |
9 ms |
4956 KB |
Output is correct |
40 |
Correct |
9 ms |
4956 KB |
Output is correct |
41 |
Runtime error |
185 ms |
65536 KB |
Execution killed with signal 9 |
42 |
Correct |
12 ms |
4952 KB |
Output is correct |
43 |
Correct |
12 ms |
5208 KB |
Output is correct |
44 |
Runtime error |
187 ms |
65536 KB |
Execution killed with signal 9 |
45 |
Correct |
13 ms |
4956 KB |
Output is correct |
46 |
Correct |
12 ms |
5008 KB |
Output is correct |
47 |
Runtime error |
186 ms |
65536 KB |
Execution killed with signal 9 |
48 |
Correct |
17 ms |
5320 KB |
Output is correct |
49 |
Correct |
15 ms |
5076 KB |
Output is correct |
50 |
Runtime error |
193 ms |
65536 KB |
Execution killed with signal 9 |
51 |
Correct |
18 ms |
5464 KB |
Output is correct |
52 |
Correct |
18 ms |
5468 KB |
Output is correct |
53 |
Runtime error |
189 ms |
65536 KB |
Execution killed with signal 9 |
54 |
Correct |
27 ms |
5720 KB |
Output is correct |
55 |
Correct |
22 ms |
5468 KB |
Output is correct |
56 |
Runtime error |
186 ms |
65536 KB |
Execution killed with signal 9 |
57 |
Correct |
22 ms |
5720 KB |
Output is correct |
58 |
Correct |
22 ms |
5736 KB |
Output is correct |
59 |
Runtime error |
195 ms |
65536 KB |
Execution killed with signal 9 |
60 |
Correct |
26 ms |
5984 KB |
Output is correct |
61 |
Correct |
28 ms |
5980 KB |
Output is correct |
62 |
Runtime error |
187 ms |
65536 KB |
Execution killed with signal 9 |
63 |
Correct |
103 ms |
6224 KB |
Output is correct |
64 |
Correct |
142 ms |
6072 KB |
Output is correct |
65 |
Correct |
138 ms |
5980 KB |
Output is correct |
66 |
Correct |
111 ms |
5980 KB |
Output is correct |
67 |
Incorrect |
102 ms |
6072 KB |
Output isn't correct |
68 |
Correct |
49 ms |
6100 KB |
Output is correct |
69 |
Correct |
60 ms |
5972 KB |
Output is correct |
70 |
Correct |
42 ms |
5980 KB |
Output is correct |
71 |
Correct |
40 ms |
5940 KB |
Output is correct |
72 |
Incorrect |
47 ms |
5972 KB |
Output isn't correct |
73 |
Runtime error |
397 ms |
65536 KB |
Execution killed with signal 9 |
74 |
Runtime error |
375 ms |
65536 KB |
Execution killed with signal 9 |
75 |
Runtime error |
410 ms |
65536 KB |
Execution killed with signal 9 |
76 |
Runtime error |
376 ms |
65536 KB |
Execution killed with signal 9 |
77 |
Runtime error |
404 ms |
65536 KB |
Execution killed with signal 9 |
78 |
Runtime error |
332 ms |
65536 KB |
Execution killed with signal 9 |
79 |
Runtime error |
309 ms |
65536 KB |
Execution killed with signal 9 |
80 |
Runtime error |
306 ms |
65536 KB |
Execution killed with signal 9 |
81 |
Runtime error |
313 ms |
65536 KB |
Execution killed with signal 9 |
82 |
Runtime error |
302 ms |
65536 KB |
Execution killed with signal 9 |
83 |
Runtime error |
265 ms |
65536 KB |
Execution killed with signal 9 |
84 |
Runtime error |
273 ms |
65536 KB |
Execution killed with signal 9 |
85 |
Runtime error |
264 ms |
65536 KB |
Execution killed with signal 9 |
86 |
Runtime error |
278 ms |
65536 KB |
Execution killed with signal 9 |
87 |
Runtime error |
262 ms |
65536 KB |
Execution killed with signal 9 |
88 |
Runtime error |
239 ms |
65536 KB |
Execution killed with signal 9 |
89 |
Runtime error |
232 ms |
65536 KB |
Execution killed with signal 9 |
90 |
Runtime error |
233 ms |
65536 KB |
Execution killed with signal 9 |
91 |
Runtime error |
238 ms |
65536 KB |
Execution killed with signal 9 |
92 |
Runtime error |
229 ms |
65536 KB |
Execution killed with signal 9 |