#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 8e2 + 5;
int n, s;
char grid[N][N];
pair<int, int> init, home;
vector<pair<int, int>> hive;
vector<int> dx = {-1, 0, 1, 0}, dy = {0, 1, 0, -1};
queue<pair<int, int>> qmecho, qbee;
bool vmecho[N][N], vbee[N][N];
int dmecho[N][N], dbee[N][N];
bool bee_inbound(int x, int y) {
return x >= 1 && x <= n && y >= 1 && y <= n && !vbee[x][y] && grid[x][y] == 'G';
}
bool mecho_inbound(int x, int y) {
return x >= 1 && x <= n && y >= 1 && y <= n && !vmecho[x][y] && (grid[x][y] == 'G' || grid[x][y] == 'D');
}
bool valid(int m) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
vmecho[i][j] = vbee[i][j] = false;
dmecho[i][j] = dbee[i][j] = 1e18;
}
}
for (auto&b : hive) {
int x = b.first, y = b.second;
dbee[x][y] = 0, vbee[x][y] = true, qbee.push(b);
}
while (qbee.size()) {
int x = qbee.front().first, y = qbee.front().second; qbee.pop();
for (int k = 0; k < 4; k++) {
int tox = x + dx[k], toy = y + dy[k];
if (!bee_inbound(tox, toy)) continue;
dbee[tox][toy] = dbee[x][y] + 1, vbee[tox][toy] = true, qbee.push({tox, toy});
}
}
int x = init.first, y = init.second;
dmecho[x][y] = 0, vmecho[x][y] = true, qmecho.push({x, y});
while (qmecho.size()) {
int x = qmecho.front().first, y = qmecho.front().second; qmecho.pop();
if (dmecho[x][y]/s >= dbee[x][y] - m) continue;
for (int k = 0; k < 4; k++) {
int tox = x + dx[k], toy = y + dy[k];
if (!mecho_inbound(tox, toy)) continue;
dmecho[tox][toy] = dmecho[x][y] + 1, vmecho[tox][toy] = true, qmecho.push({tox, toy});
}
}
x = home.first, y = home.second;
return vmecho[x][y] && dmecho[x][y]/s < dbee[x][y] - m;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> s;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> grid[i][j];
if (grid[i][j] == 'M') init = {i, j};
else if(grid[i][j] == 'D') home = {i, j};
else if(grid[i][j] == 'H') hive.push_back({i, j});
}
}
int l = 0, r = n*n, ans = -1;
while (l <= r) {
int m = (l + r)/2;
if (valid(m)) ans = m, l = m + 1;
else r = m - 1;
}
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
476 ms |
13068 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Incorrect |
1 ms |
980 KB |
Output isn't correct |
13 |
Incorrect |
2 ms |
852 KB |
Output isn't correct |
14 |
Correct |
2 ms |
976 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
724 KB |
Output is correct |
22 |
Correct |
1 ms |
716 KB |
Output is correct |
23 |
Correct |
1 ms |
864 KB |
Output is correct |
24 |
Correct |
1 ms |
852 KB |
Output is correct |
25 |
Correct |
1 ms |
980 KB |
Output is correct |
26 |
Correct |
1 ms |
980 KB |
Output is correct |
27 |
Correct |
2 ms |
1108 KB |
Output is correct |
28 |
Correct |
2 ms |
1108 KB |
Output is correct |
29 |
Correct |
3 ms |
1108 KB |
Output is correct |
30 |
Correct |
2 ms |
1112 KB |
Output is correct |
31 |
Correct |
2 ms |
1108 KB |
Output is correct |
32 |
Correct |
3 ms |
1108 KB |
Output is correct |
33 |
Correct |
35 ms |
5692 KB |
Output is correct |
34 |
Correct |
32 ms |
5696 KB |
Output is correct |
35 |
Correct |
61 ms |
5592 KB |
Output is correct |
36 |
Correct |
38 ms |
6484 KB |
Output is correct |
37 |
Correct |
40 ms |
6492 KB |
Output is correct |
38 |
Correct |
74 ms |
6492 KB |
Output is correct |
39 |
Correct |
60 ms |
7272 KB |
Output is correct |
40 |
Correct |
68 ms |
7252 KB |
Output is correct |
41 |
Correct |
106 ms |
7284 KB |
Output is correct |
42 |
Correct |
70 ms |
8064 KB |
Output is correct |
43 |
Correct |
81 ms |
8020 KB |
Output is correct |
44 |
Correct |
161 ms |
8076 KB |
Output is correct |
45 |
Correct |
84 ms |
8876 KB |
Output is correct |
46 |
Correct |
79 ms |
8864 KB |
Output is correct |
47 |
Correct |
231 ms |
8872 KB |
Output is correct |
48 |
Correct |
98 ms |
9660 KB |
Output is correct |
49 |
Correct |
105 ms |
9684 KB |
Output is correct |
50 |
Correct |
235 ms |
9680 KB |
Output is correct |
51 |
Correct |
123 ms |
10472 KB |
Output is correct |
52 |
Correct |
119 ms |
10472 KB |
Output is correct |
53 |
Correct |
255 ms |
10496 KB |
Output is correct |
54 |
Correct |
136 ms |
11284 KB |
Output is correct |
55 |
Correct |
161 ms |
11192 KB |
Output is correct |
56 |
Correct |
298 ms |
11320 KB |
Output is correct |
57 |
Correct |
164 ms |
11980 KB |
Output is correct |
58 |
Correct |
214 ms |
12120 KB |
Output is correct |
59 |
Correct |
380 ms |
12364 KB |
Output is correct |
60 |
Correct |
198 ms |
12884 KB |
Output is correct |
61 |
Correct |
171 ms |
12932 KB |
Output is correct |
62 |
Correct |
405 ms |
12948 KB |
Output is correct |
63 |
Correct |
464 ms |
12944 KB |
Output is correct |
64 |
Correct |
608 ms |
12948 KB |
Output is correct |
65 |
Correct |
568 ms |
12956 KB |
Output is correct |
66 |
Correct |
488 ms |
12852 KB |
Output is correct |
67 |
Correct |
489 ms |
12944 KB |
Output is correct |
68 |
Correct |
454 ms |
13012 KB |
Output is correct |
69 |
Correct |
484 ms |
13132 KB |
Output is correct |
70 |
Correct |
431 ms |
13040 KB |
Output is correct |
71 |
Correct |
454 ms |
13012 KB |
Output is correct |
72 |
Incorrect |
476 ms |
13012 KB |
Output isn't correct |
73 |
Incorrect |
481 ms |
13540 KB |
Output isn't correct |
74 |
Correct |
405 ms |
13536 KB |
Output is correct |
75 |
Correct |
436 ms |
13544 KB |
Output is correct |
76 |
Correct |
468 ms |
13540 KB |
Output is correct |
77 |
Correct |
468 ms |
13664 KB |
Output is correct |
78 |
Correct |
475 ms |
13592 KB |
Output is correct |
79 |
Correct |
429 ms |
13468 KB |
Output is correct |
80 |
Correct |
417 ms |
13596 KB |
Output is correct |
81 |
Correct |
436 ms |
13472 KB |
Output is correct |
82 |
Correct |
498 ms |
13524 KB |
Output is correct |
83 |
Correct |
455 ms |
13396 KB |
Output is correct |
84 |
Correct |
439 ms |
13432 KB |
Output is correct |
85 |
Correct |
497 ms |
13392 KB |
Output is correct |
86 |
Correct |
445 ms |
13396 KB |
Output is correct |
87 |
Correct |
461 ms |
13396 KB |
Output is correct |
88 |
Correct |
426 ms |
13288 KB |
Output is correct |
89 |
Correct |
447 ms |
13288 KB |
Output is correct |
90 |
Correct |
442 ms |
13388 KB |
Output is correct |
91 |
Correct |
469 ms |
13288 KB |
Output is correct |
92 |
Correct |
490 ms |
13244 KB |
Output is correct |