#include <iostream>
#include <vector>
#include <cassert>
#include <queue>
#include <algorithm>
using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
int const nmax = 800;
int const iplus[4] = {0, 0, 1, -1};
int const jplus[4] = {1, -1, 0, 0};
char v[1 + nmax][1 + nmax];
int n, k;
bool valid(int x, int y) {
return 1 <= x && x <= n && 1 <= y && y <= n;
}
int bee[1 + nmax][1 + nmax];
void add_bees() {
std::queue<std::pair<int,int>> q;
for(int i = 1;i <= n; i++)
for(int j = 1;j <= n; j++)
bee[i][j] = -1;
for(int i = 1;i <= n; i++)
for(int j = 1;j <= n; j++)
if(v[i][j] == 'H') {
bee[i][j] = 0;
q.push({i, j});
}
while(0 < q.size()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
for(int h = 0; h < 4; h++) {
int newx = x + iplus[h];
int newy = y + jplus[h];
if(valid(newx, newy) == 1 && (v[newx][newy] == 'G' || v[newx][newy] == 'M'))
if(bee[newx][newy] == -1) {
bee[newx][newy] = bee[x][y] + 1;
q.push({newx, newy});
}
}
}
/*
std::cout << "bees\n";
for(int i = 1;i <= n; i++) {
for(int j = 1; j <= n; j++)
std::cout << bee[i][j] << " ";
std::cout << '\n';
}
*/
}
int dp[1 + nmax][1 + nmax];
bool can_reach(int _sleep) {
for(int i = 1;i <= n; i++)
for(int j = 1;j <= n; j++)
dp[i][j] = -1;
std::queue<std::pair<int,int>> q;
for(int i = 1;i <= n; i++)
for(int j = 1;j <= n; j++)
if(v[i][j] == 'M') {
if(_sleep < bee[i][j]) {
q.push({i, j});
dp[i][j] = _sleep * k;
}
}
while(0 < q.size()) {
int x = q.front().first;
int y = q.front().second;
if(v[x][y] == 'D')
return true;
q.pop();
for(int h = 0;h < 4; h++) {
int newx = x + iplus[h];
int newy = y + jplus[h];
if(valid(newx, newy) && (v[newx][newy] == 'G' || v[newx][newy] == 'D'))
if(dp[newx][newy] == -1 && (v[newx][newy] == 'D' || dp[x][y] + 1 < bee[newx][newy] * k || bee[newx][newy] == -1)) {
dp[newx][newy] = dp[x][y] + 1;
q.push({newx, newy});
}
}
}
return false;
}
int main() {
std::cin >> n >> k;
for(int i = 1;i <= n; i++)
for(int j = 1; j <= n; j++)
std::cin >> v[i][j];
add_bees();
if(can_reach(0) == false)
std::cout << -1;
else {
int x = 0;
for(int jump = (1 << 20); 0 < jump; jump /= 2)
if(can_reach(x + jump) == true)
x += jump;
std::cout << x;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
177 ms |
6756 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
748 KB |
Output is correct |
13 |
Correct |
1 ms |
748 KB |
Output is correct |
14 |
Correct |
1 ms |
748 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
488 KB |
Output is correct |
21 |
Correct |
1 ms |
768 KB |
Output is correct |
22 |
Correct |
1 ms |
620 KB |
Output is correct |
23 |
Correct |
1 ms |
748 KB |
Output is correct |
24 |
Correct |
1 ms |
748 KB |
Output is correct |
25 |
Correct |
1 ms |
748 KB |
Output is correct |
26 |
Correct |
1 ms |
748 KB |
Output is correct |
27 |
Correct |
1 ms |
744 KB |
Output is correct |
28 |
Correct |
2 ms |
748 KB |
Output is correct |
29 |
Correct |
2 ms |
876 KB |
Output is correct |
30 |
Correct |
2 ms |
876 KB |
Output is correct |
31 |
Correct |
1 ms |
876 KB |
Output is correct |
32 |
Correct |
2 ms |
1024 KB |
Output is correct |
33 |
Correct |
15 ms |
2924 KB |
Output is correct |
34 |
Correct |
19 ms |
2924 KB |
Output is correct |
35 |
Correct |
44 ms |
2924 KB |
Output is correct |
36 |
Correct |
20 ms |
3308 KB |
Output is correct |
37 |
Correct |
23 ms |
3308 KB |
Output is correct |
38 |
Correct |
50 ms |
3308 KB |
Output is correct |
39 |
Correct |
25 ms |
3692 KB |
Output is correct |
40 |
Correct |
30 ms |
3692 KB |
Output is correct |
41 |
Correct |
65 ms |
3692 KB |
Output is correct |
42 |
Correct |
29 ms |
4068 KB |
Output is correct |
43 |
Correct |
34 ms |
4076 KB |
Output is correct |
44 |
Correct |
84 ms |
4068 KB |
Output is correct |
45 |
Correct |
35 ms |
4580 KB |
Output is correct |
46 |
Correct |
37 ms |
4580 KB |
Output is correct |
47 |
Correct |
107 ms |
4580 KB |
Output is correct |
48 |
Correct |
40 ms |
4972 KB |
Output is correct |
49 |
Correct |
45 ms |
4964 KB |
Output is correct |
50 |
Correct |
134 ms |
5240 KB |
Output is correct |
51 |
Correct |
57 ms |
5356 KB |
Output is correct |
52 |
Correct |
48 ms |
5348 KB |
Output is correct |
53 |
Correct |
149 ms |
5412 KB |
Output is correct |
54 |
Correct |
59 ms |
5860 KB |
Output is correct |
55 |
Correct |
56 ms |
5732 KB |
Output is correct |
56 |
Correct |
173 ms |
5732 KB |
Output is correct |
57 |
Correct |
62 ms |
6244 KB |
Output is correct |
58 |
Correct |
63 ms |
6244 KB |
Output is correct |
59 |
Correct |
203 ms |
6244 KB |
Output is correct |
60 |
Correct |
70 ms |
6628 KB |
Output is correct |
61 |
Correct |
71 ms |
6628 KB |
Output is correct |
62 |
Correct |
221 ms |
6628 KB |
Output is correct |
63 |
Correct |
199 ms |
6628 KB |
Output is correct |
64 |
Correct |
271 ms |
6628 KB |
Output is correct |
65 |
Correct |
264 ms |
6756 KB |
Output is correct |
66 |
Correct |
229 ms |
6628 KB |
Output is correct |
67 |
Correct |
77 ms |
6628 KB |
Output is correct |
68 |
Correct |
110 ms |
6628 KB |
Output is correct |
69 |
Correct |
106 ms |
6628 KB |
Output is correct |
70 |
Correct |
101 ms |
6756 KB |
Output is correct |
71 |
Correct |
100 ms |
6756 KB |
Output is correct |
72 |
Correct |
85 ms |
6628 KB |
Output is correct |
73 |
Correct |
101 ms |
6884 KB |
Output is correct |
74 |
Correct |
147 ms |
6884 KB |
Output is correct |
75 |
Correct |
148 ms |
7012 KB |
Output is correct |
76 |
Correct |
141 ms |
6884 KB |
Output is correct |
77 |
Correct |
156 ms |
6884 KB |
Output is correct |
78 |
Correct |
75 ms |
6884 KB |
Output is correct |
79 |
Correct |
137 ms |
7140 KB |
Output is correct |
80 |
Correct |
137 ms |
6884 KB |
Output is correct |
81 |
Correct |
155 ms |
7012 KB |
Output is correct |
82 |
Correct |
143 ms |
7012 KB |
Output is correct |
83 |
Correct |
178 ms |
6756 KB |
Output is correct |
84 |
Correct |
171 ms |
6756 KB |
Output is correct |
85 |
Correct |
155 ms |
6756 KB |
Output is correct |
86 |
Correct |
172 ms |
6756 KB |
Output is correct |
87 |
Correct |
163 ms |
6756 KB |
Output is correct |
88 |
Correct |
165 ms |
6756 KB |
Output is correct |
89 |
Correct |
181 ms |
6760 KB |
Output is correct |
90 |
Correct |
180 ms |
6756 KB |
Output is correct |
91 |
Correct |
175 ms |
6756 KB |
Output is correct |
92 |
Correct |
172 ms |
6756 KB |
Output is correct |