#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ar array
const int mxN = 800;
const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int n, K;
vector<string> g(mxN);
pair<int,int> mecho, home;
vector<pair<int,int>> hives;
vector<vector<int>> disthives(mxN, vector<int>(mxN, (int) 1e7)), dist(mxN, vector<int>(mxN, (int) 1e7));
bool checkhives(int x, int y) {
return x >= 0 && x < n && y >= 0 && y < n && g[x][y] == 'G' && disthives[x][y] == (int) 1e7;
}
bool checkbeer(int DIST, int x, int y) {
return x >= 0 && x < n && y >= 0 && y < n && g[x][y] != 'T' && g[x][y] != 'H' && disthives[x][y] > DIST && dist[x][y] == (int) 1e7;
}
void bfs(int m) {
priority_queue<ar<int, 3>, vector<ar<int, 3>>, greater<ar<int, 3>>> pq;
pq.push({m, mecho.first, mecho.second});
while (!pq.empty()) {
int d = pq.top()[0], x = pq.top()[1], y = pq.top()[2]; pq.pop();
if (dist[x][y] < d) {
continue;
}
for (int k = 0; k < 4; ++k) {
int nx = x + dx[k], ny = y + dy[k];
int DIST = (d + 1) / K;
// if ((d + 1) % K) {
// ++DIST;
// }
if (checkbeer(DIST, nx, ny)) {
pq.push({d + 1, nx, ny});
dist[nx][ny] = d + 1;
}
}
}
// cout << dist[home.first][home.second] << '\n';
// for (int i = 0; i < n; ++i) {
// for (int j = 0; j < n; ++j) {
// cout << dist[i][j] << ' ';
// }
// cout << '\n';
// }
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> K;
for (int i = 0; i < n; ++i) {
cin >> g[i];
for (int j = 0; j < n; ++j) {
if (g[i][j] == 'M') {
mecho = {i, j};
} else if (g[i][j] == 'D') {
home = {i, j};
} else if (g[i][j] == 'H') {
hives.push_back({i, j});
disthives[i][j] = 0;
}
}
}
priority_queue<ar<int, 3>, vector<ar<int, 3>>, greater<ar<int, 3>>> pq;
for (auto i : hives) {
pq.push({0, i.first, i.second});
}
while (!pq.empty()) {
int d = pq.top()[0], x = pq.top()[1], y = pq.top()[2]; pq.pop();
if (disthives[x][y] < d) {
continue;
}
for (int k = 0; k < 4; ++k) {
int nx = x + dx[k], ny = y + dy[k];
if (checkhives(nx, ny)) {
pq.push({d + 1, nx, ny});
disthives[nx][ny] = d + 1;
}
}
}
// for (int i = 0; i < n; ++i) {
// for (int j = 0; j < n; ++j) {
// cout << disthives[i][j] << ' ';
// }
// cout << '\n';
// }
// cerr << "HELLO\n";
int l = 0, r = mxN * mxN;
while (l <= r) {
int m = (l + r) >> 1;
dist = vector<vector<int>>(mxN, vector<int>(mxN, (int) 1e7));
dist[mecho.first][mecho.second] = m * K;
bfs(m * K);
if (dist[home.first][home.second] < 1e7) {
l = m + 1;
} else {
r = m - 1;
}
}
cout << l - 1 << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
15672 KB |
Output is correct |
2 |
Correct |
30 ms |
15496 KB |
Output is correct |
3 |
Correct |
32 ms |
15508 KB |
Output is correct |
4 |
Correct |
31 ms |
15564 KB |
Output is correct |
5 |
Correct |
33 ms |
15496 KB |
Output is correct |
6 |
Correct |
32 ms |
15504 KB |
Output is correct |
7 |
Correct |
451 ms |
16864 KB |
Output is correct |
8 |
Correct |
33 ms |
15524 KB |
Output is correct |
9 |
Correct |
30 ms |
15496 KB |
Output is correct |
10 |
Correct |
32 ms |
15548 KB |
Output is correct |
11 |
Correct |
33 ms |
15692 KB |
Output is correct |
12 |
Incorrect |
31 ms |
15516 KB |
Output isn't correct |
13 |
Incorrect |
32 ms |
15548 KB |
Output isn't correct |
14 |
Correct |
32 ms |
15520 KB |
Output is correct |
15 |
Correct |
29 ms |
15516 KB |
Output is correct |
16 |
Correct |
31 ms |
15556 KB |
Output is correct |
17 |
Correct |
30 ms |
15512 KB |
Output is correct |
18 |
Correct |
29 ms |
15512 KB |
Output is correct |
19 |
Correct |
30 ms |
15492 KB |
Output is correct |
20 |
Correct |
29 ms |
15476 KB |
Output is correct |
21 |
Correct |
30 ms |
15516 KB |
Output is correct |
22 |
Correct |
31 ms |
15500 KB |
Output is correct |
23 |
Correct |
29 ms |
15540 KB |
Output is correct |
24 |
Correct |
28 ms |
15520 KB |
Output is correct |
25 |
Correct |
30 ms |
15524 KB |
Output is correct |
26 |
Correct |
30 ms |
15500 KB |
Output is correct |
27 |
Correct |
30 ms |
15564 KB |
Output is correct |
28 |
Correct |
30 ms |
15532 KB |
Output is correct |
29 |
Correct |
30 ms |
15532 KB |
Output is correct |
30 |
Correct |
31 ms |
15552 KB |
Output is correct |
31 |
Correct |
25 ms |
15460 KB |
Output is correct |
32 |
Correct |
30 ms |
15516 KB |
Output is correct |
33 |
Correct |
33 ms |
15736 KB |
Output is correct |
34 |
Correct |
26 ms |
15712 KB |
Output is correct |
35 |
Correct |
121 ms |
15816 KB |
Output is correct |
36 |
Correct |
37 ms |
15804 KB |
Output is correct |
37 |
Correct |
29 ms |
15756 KB |
Output is correct |
38 |
Correct |
148 ms |
15800 KB |
Output is correct |
39 |
Correct |
25 ms |
15820 KB |
Output is correct |
40 |
Incorrect |
48 ms |
15916 KB |
Output isn't correct |
41 |
Correct |
183 ms |
15900 KB |
Output is correct |
42 |
Correct |
29 ms |
15968 KB |
Output is correct |
43 |
Incorrect |
79 ms |
16008 KB |
Output isn't correct |
44 |
Correct |
233 ms |
15976 KB |
Output is correct |
45 |
Correct |
30 ms |
16028 KB |
Output is correct |
46 |
Incorrect |
103 ms |
16032 KB |
Output isn't correct |
47 |
Correct |
278 ms |
16084 KB |
Output is correct |
48 |
Correct |
38 ms |
16172 KB |
Output is correct |
49 |
Incorrect |
131 ms |
16156 KB |
Output isn't correct |
50 |
Correct |
333 ms |
16352 KB |
Output is correct |
51 |
Correct |
40 ms |
16296 KB |
Output is correct |
52 |
Incorrect |
150 ms |
16212 KB |
Output isn't correct |
53 |
Correct |
396 ms |
16232 KB |
Output is correct |
54 |
Correct |
42 ms |
16324 KB |
Output is correct |
55 |
Incorrect |
198 ms |
16228 KB |
Output isn't correct |
56 |
Correct |
471 ms |
16332 KB |
Output is correct |
57 |
Correct |
50 ms |
16420 KB |
Output is correct |
58 |
Incorrect |
237 ms |
16360 KB |
Output isn't correct |
59 |
Correct |
549 ms |
16472 KB |
Output is correct |
60 |
Correct |
53 ms |
16456 KB |
Output is correct |
61 |
Incorrect |
268 ms |
16428 KB |
Output isn't correct |
62 |
Correct |
626 ms |
16544 KB |
Output is correct |
63 |
Correct |
341 ms |
16508 KB |
Output is correct |
64 |
Correct |
617 ms |
16596 KB |
Output is correct |
65 |
Correct |
483 ms |
16748 KB |
Output is correct |
66 |
Correct |
375 ms |
16456 KB |
Output is correct |
67 |
Correct |
374 ms |
16608 KB |
Output is correct |
68 |
Correct |
158 ms |
16564 KB |
Output is correct |
69 |
Correct |
153 ms |
16588 KB |
Output is correct |
70 |
Correct |
132 ms |
16556 KB |
Output is correct |
71 |
Correct |
154 ms |
16648 KB |
Output is correct |
72 |
Incorrect |
128 ms |
16552 KB |
Output isn't correct |
73 |
Incorrect |
364 ms |
17340 KB |
Output isn't correct |
74 |
Correct |
298 ms |
17408 KB |
Output is correct |
75 |
Correct |
353 ms |
17276 KB |
Output is correct |
76 |
Correct |
322 ms |
17496 KB |
Output is correct |
77 |
Correct |
304 ms |
17252 KB |
Output is correct |
78 |
Correct |
392 ms |
17220 KB |
Output is correct |
79 |
Correct |
263 ms |
17156 KB |
Output is correct |
80 |
Correct |
303 ms |
17176 KB |
Output is correct |
81 |
Correct |
327 ms |
17184 KB |
Output is correct |
82 |
Correct |
297 ms |
17176 KB |
Output is correct |
83 |
Correct |
431 ms |
17160 KB |
Output is correct |
84 |
Correct |
360 ms |
17068 KB |
Output is correct |
85 |
Correct |
418 ms |
17148 KB |
Output is correct |
86 |
Correct |
406 ms |
17052 KB |
Output is correct |
87 |
Correct |
414 ms |
17120 KB |
Output is correct |
88 |
Correct |
437 ms |
16956 KB |
Output is correct |
89 |
Correct |
453 ms |
17016 KB |
Output is correct |
90 |
Correct |
449 ms |
16916 KB |
Output is correct |
91 |
Correct |
437 ms |
16920 KB |
Output is correct |
92 |
Correct |
488 ms |
16928 KB |
Output is correct |