#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<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*N+j);
}
}
}
while (Q.size()) {
int x = Q.front(); Q.pop();
int i = x/N, j = x%N;
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] + 1) continue;
dist[i2][j2] = dist[i][j] + 1;
Q.push(i2*N+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 = -1;
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.first*N+begin.second);
while (Q.size()) {
auto x = Q.front(); Q.pop();
int i = x/N, j = x%N;
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] + 1) 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*N+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 |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
82 ms |
5980 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is 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 |
4696 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
2648 KB |
Output isn't correct |
14 |
Correct |
1 ms |
4700 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2524 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 |
2648 KB |
Output is correct |
24 |
Correct |
1 ms |
2652 KB |
Output is correct |
25 |
Correct |
1 ms |
4700 KB |
Output is correct |
26 |
Correct |
1 ms |
4700 KB |
Output is correct |
27 |
Correct |
1 ms |
4700 KB |
Output is correct |
28 |
Correct |
1 ms |
4696 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 |
5 ms |
4956 KB |
Output is correct |
34 |
Correct |
5 ms |
4956 KB |
Output is correct |
35 |
Correct |
23 ms |
5204 KB |
Output is correct |
36 |
Correct |
6 ms |
4956 KB |
Output is correct |
37 |
Correct |
6 ms |
4956 KB |
Output is correct |
38 |
Correct |
27 ms |
4956 KB |
Output is correct |
39 |
Correct |
7 ms |
4952 KB |
Output is correct |
40 |
Correct |
9 ms |
4956 KB |
Output is correct |
41 |
Correct |
35 ms |
5212 KB |
Output is correct |
42 |
Correct |
9 ms |
5136 KB |
Output is correct |
43 |
Correct |
10 ms |
4956 KB |
Output is correct |
44 |
Correct |
43 ms |
5304 KB |
Output is correct |
45 |
Correct |
10 ms |
4956 KB |
Output is correct |
46 |
Correct |
10 ms |
4956 KB |
Output is correct |
47 |
Correct |
54 ms |
5208 KB |
Output is correct |
48 |
Correct |
12 ms |
5208 KB |
Output is correct |
49 |
Correct |
13 ms |
5212 KB |
Output is correct |
50 |
Correct |
66 ms |
5492 KB |
Output is correct |
51 |
Correct |
15 ms |
5468 KB |
Output is correct |
52 |
Correct |
15 ms |
5468 KB |
Output is correct |
53 |
Correct |
83 ms |
5820 KB |
Output is correct |
54 |
Correct |
17 ms |
5464 KB |
Output is correct |
55 |
Correct |
19 ms |
5468 KB |
Output is correct |
56 |
Correct |
87 ms |
6012 KB |
Output is correct |
57 |
Correct |
17 ms |
5724 KB |
Output is correct |
58 |
Correct |
18 ms |
5720 KB |
Output is correct |
59 |
Correct |
103 ms |
6244 KB |
Output is correct |
60 |
Correct |
20 ms |
5980 KB |
Output is correct |
61 |
Correct |
22 ms |
5968 KB |
Output is correct |
62 |
Correct |
116 ms |
6484 KB |
Output is correct |
63 |
Correct |
97 ms |
6072 KB |
Output is correct |
64 |
Correct |
158 ms |
6132 KB |
Output is correct |
65 |
Correct |
149 ms |
5980 KB |
Output is correct |
66 |
Correct |
114 ms |
5968 KB |
Output is correct |
67 |
Correct |
114 ms |
5972 KB |
Output is correct |
68 |
Correct |
46 ms |
5968 KB |
Output is correct |
69 |
Correct |
46 ms |
5920 KB |
Output is correct |
70 |
Correct |
38 ms |
6064 KB |
Output is correct |
71 |
Correct |
33 ms |
5972 KB |
Output is correct |
72 |
Incorrect |
36 ms |
5980 KB |
Output isn't correct |
73 |
Incorrect |
68 ms |
6484 KB |
Output isn't correct |
74 |
Correct |
58 ms |
6624 KB |
Output is correct |
75 |
Correct |
67 ms |
6480 KB |
Output is correct |
76 |
Correct |
62 ms |
6596 KB |
Output is correct |
77 |
Correct |
64 ms |
6464 KB |
Output is correct |
78 |
Correct |
83 ms |
6596 KB |
Output is correct |
79 |
Correct |
62 ms |
6480 KB |
Output is correct |
80 |
Correct |
62 ms |
6568 KB |
Output is correct |
81 |
Correct |
73 ms |
6480 KB |
Output is correct |
82 |
Correct |
64 ms |
6484 KB |
Output is correct |
83 |
Correct |
78 ms |
6484 KB |
Output is correct |
84 |
Correct |
74 ms |
6252 KB |
Output is correct |
85 |
Correct |
71 ms |
6488 KB |
Output is correct |
86 |
Correct |
77 ms |
6480 KB |
Output is correct |
87 |
Correct |
73 ms |
6616 KB |
Output is correct |
88 |
Correct |
80 ms |
6484 KB |
Output is correct |
89 |
Correct |
80 ms |
6492 KB |
Output is correct |
90 |
Correct |
85 ms |
6484 KB |
Output is correct |
91 |
Correct |
78 ms |
6564 KB |
Output is correct |
92 |
Correct |
87 ms |
6740 KB |
Output is correct |