#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 = N+N, 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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
452 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Runtime error |
202 ms |
65536 KB |
Execution killed with signal 9 |
8 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
9 |
Correct |
1 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 |
0 ms |
348 KB |
Output isn't correct |
13 |
Runtime error |
398 ms |
65536 KB |
Execution killed with signal 9 |
14 |
Runtime error |
346 ms |
65536 KB |
Execution killed with signal 9 |
15 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
16 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
17 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
18 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
19 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
20 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
21 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
22 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
24 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
25 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
27 |
Incorrect |
1 ms |
500 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
29 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
30 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
31 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
32 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
33 |
Incorrect |
9 ms |
1640 KB |
Output isn't correct |
34 |
Incorrect |
13 ms |
1676 KB |
Output isn't correct |
35 |
Runtime error |
191 ms |
65536 KB |
Execution killed with signal 9 |
36 |
Incorrect |
12 ms |
1884 KB |
Output isn't correct |
37 |
Incorrect |
18 ms |
2396 KB |
Output isn't correct |
38 |
Runtime error |
212 ms |
65536 KB |
Execution killed with signal 9 |
39 |
Incorrect |
15 ms |
2368 KB |
Output isn't correct |
40 |
Incorrect |
27 ms |
2408 KB |
Output isn't correct |
41 |
Runtime error |
191 ms |
65536 KB |
Execution killed with signal 9 |
42 |
Incorrect |
19 ms |
2872 KB |
Output isn't correct |
43 |
Incorrect |
28 ms |
2948 KB |
Output isn't correct |
44 |
Runtime error |
206 ms |
65536 KB |
Execution killed with signal 9 |
45 |
Incorrect |
26 ms |
3368 KB |
Output isn't correct |
46 |
Incorrect |
34 ms |
3376 KB |
Output isn't correct |
47 |
Runtime error |
186 ms |
65536 KB |
Execution killed with signal 9 |
48 |
Incorrect |
29 ms |
3960 KB |
Output isn't correct |
49 |
Incorrect |
45 ms |
3928 KB |
Output isn't correct |
50 |
Runtime error |
186 ms |
65536 KB |
Execution killed with signal 9 |
51 |
Incorrect |
34 ms |
4444 KB |
Output isn't correct |
52 |
Incorrect |
53 ms |
4540 KB |
Output isn't correct |
53 |
Runtime error |
192 ms |
65536 KB |
Execution killed with signal 9 |
54 |
Incorrect |
43 ms |
5156 KB |
Output isn't correct |
55 |
Incorrect |
57 ms |
5220 KB |
Output isn't correct |
56 |
Runtime error |
194 ms |
65536 KB |
Execution killed with signal 9 |
57 |
Incorrect |
46 ms |
5840 KB |
Output isn't correct |
58 |
Incorrect |
65 ms |
5876 KB |
Output isn't correct |
59 |
Runtime error |
184 ms |
65536 KB |
Execution killed with signal 9 |
60 |
Incorrect |
54 ms |
6676 KB |
Output isn't correct |
61 |
Incorrect |
78 ms |
6652 KB |
Output isn't correct |
62 |
Runtime error |
208 ms |
65536 KB |
Execution killed with signal 9 |
63 |
Correct |
88 ms |
6596 KB |
Output is correct |
64 |
Correct |
122 ms |
6576 KB |
Output is correct |
65 |
Correct |
140 ms |
6892 KB |
Output is correct |
66 |
Correct |
106 ms |
6588 KB |
Output is correct |
67 |
Incorrect |
91 ms |
6600 KB |
Output isn't correct |
68 |
Correct |
43 ms |
6672 KB |
Output is correct |
69 |
Correct |
37 ms |
6636 KB |
Output is correct |
70 |
Correct |
37 ms |
6644 KB |
Output is correct |
71 |
Correct |
35 ms |
6716 KB |
Output is correct |
72 |
Incorrect |
28 ms |
6644 KB |
Output isn't correct |
73 |
Runtime error |
392 ms |
65536 KB |
Execution killed with signal 9 |
74 |
Runtime error |
360 ms |
65536 KB |
Execution killed with signal 9 |
75 |
Runtime error |
388 ms |
65536 KB |
Execution killed with signal 9 |
76 |
Runtime error |
383 ms |
65536 KB |
Execution killed with signal 9 |
77 |
Runtime error |
366 ms |
65536 KB |
Execution killed with signal 9 |
78 |
Runtime error |
342 ms |
65536 KB |
Execution killed with signal 9 |
79 |
Runtime error |
340 ms |
65536 KB |
Execution killed with signal 9 |
80 |
Runtime error |
344 ms |
65536 KB |
Execution killed with signal 9 |
81 |
Runtime error |
310 ms |
65536 KB |
Execution killed with signal 9 |
82 |
Runtime error |
322 ms |
65536 KB |
Execution killed with signal 9 |
83 |
Runtime error |
280 ms |
65536 KB |
Execution killed with signal 9 |
84 |
Runtime error |
288 ms |
65536 KB |
Execution killed with signal 9 |
85 |
Runtime error |
294 ms |
65536 KB |
Execution killed with signal 9 |
86 |
Runtime error |
302 ms |
65536 KB |
Execution killed with signal 9 |
87 |
Runtime error |
274 ms |
65536 KB |
Execution killed with signal 9 |
88 |
Runtime error |
256 ms |
65536 KB |
Execution killed with signal 9 |
89 |
Runtime error |
239 ms |
65536 KB |
Execution killed with signal 9 |
90 |
Runtime error |
223 ms |
65536 KB |
Execution killed with signal 9 |
91 |
Runtime error |
231 ms |
65536 KB |
Execution killed with signal 9 |
92 |
Runtime error |
228 ms |
65536 KB |
Execution killed with signal 9 |