#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) 1e11)), dist(mxN, vector<int>(mxN, (int) 1e11));
bool checkhives(int x, int y) {
return x >= 0 && x < n && y >= 0 && y < n && g[x][y] == 'G' && disthives[x][y] == (int) 1e11;
}
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) 1e11;
}
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) 1e11));
dist[mecho.first][mecho.second] = m * K;
bfs(m * K);
if (dist[home.first][home.second] < 1e11) {
l = m + 1;
} else {
r = m - 1;
}
}
cout << l - 1 << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
15512 KB |
Output is correct |
2 |
Correct |
32 ms |
15524 KB |
Output is correct |
3 |
Correct |
28 ms |
15492 KB |
Output is correct |
4 |
Correct |
27 ms |
15508 KB |
Output is correct |
5 |
Correct |
32 ms |
15512 KB |
Output is correct |
6 |
Correct |
31 ms |
15540 KB |
Output is correct |
7 |
Correct |
462 ms |
16696 KB |
Output is correct |
8 |
Correct |
29 ms |
15500 KB |
Output is correct |
9 |
Correct |
29 ms |
15496 KB |
Output is correct |
10 |
Correct |
29 ms |
15532 KB |
Output is correct |
11 |
Correct |
29 ms |
15564 KB |
Output is correct |
12 |
Incorrect |
31 ms |
15528 KB |
Output isn't correct |
13 |
Incorrect |
29 ms |
15540 KB |
Output isn't correct |
14 |
Correct |
30 ms |
15512 KB |
Output is correct |
15 |
Correct |
30 ms |
15504 KB |
Output is correct |
16 |
Correct |
30 ms |
15548 KB |
Output is correct |
17 |
Correct |
29 ms |
15508 KB |
Output is correct |
18 |
Correct |
31 ms |
15500 KB |
Output is correct |
19 |
Correct |
31 ms |
15504 KB |
Output is correct |
20 |
Correct |
31 ms |
15528 KB |
Output is correct |
21 |
Correct |
33 ms |
15512 KB |
Output is correct |
22 |
Correct |
32 ms |
15500 KB |
Output is correct |
23 |
Correct |
31 ms |
15516 KB |
Output is correct |
24 |
Correct |
34 ms |
15520 KB |
Output is correct |
25 |
Correct |
31 ms |
15504 KB |
Output is correct |
26 |
Correct |
29 ms |
15508 KB |
Output is correct |
27 |
Correct |
32 ms |
15500 KB |
Output is correct |
28 |
Correct |
33 ms |
15544 KB |
Output is correct |
29 |
Correct |
28 ms |
15544 KB |
Output is correct |
30 |
Correct |
31 ms |
15508 KB |
Output is correct |
31 |
Correct |
26 ms |
15488 KB |
Output is correct |
32 |
Correct |
31 ms |
15528 KB |
Output is correct |
33 |
Correct |
33 ms |
15688 KB |
Output is correct |
34 |
Correct |
25 ms |
15648 KB |
Output is correct |
35 |
Correct |
117 ms |
15676 KB |
Output is correct |
36 |
Correct |
36 ms |
15764 KB |
Output is correct |
37 |
Correct |
30 ms |
15664 KB |
Output is correct |
38 |
Correct |
165 ms |
15728 KB |
Output is correct |
39 |
Correct |
25 ms |
15696 KB |
Output is correct |
40 |
Correct |
44 ms |
15776 KB |
Output is correct |
41 |
Correct |
185 ms |
15760 KB |
Output is correct |
42 |
Correct |
29 ms |
15796 KB |
Output is correct |
43 |
Correct |
38 ms |
15844 KB |
Output is correct |
44 |
Correct |
247 ms |
16020 KB |
Output is correct |
45 |
Correct |
33 ms |
15880 KB |
Output is correct |
46 |
Correct |
45 ms |
15900 KB |
Output is correct |
47 |
Correct |
281 ms |
15920 KB |
Output is correct |
48 |
Correct |
39 ms |
15976 KB |
Output is correct |
49 |
Correct |
33 ms |
15948 KB |
Output is correct |
50 |
Correct |
341 ms |
16100 KB |
Output is correct |
51 |
Correct |
40 ms |
16116 KB |
Output is correct |
52 |
Correct |
39 ms |
16048 KB |
Output is correct |
53 |
Correct |
406 ms |
16152 KB |
Output is correct |
54 |
Correct |
41 ms |
16200 KB |
Output is correct |
55 |
Correct |
35 ms |
16156 KB |
Output is correct |
56 |
Correct |
456 ms |
16196 KB |
Output is correct |
57 |
Correct |
47 ms |
16288 KB |
Output is correct |
58 |
Correct |
46 ms |
16196 KB |
Output is correct |
59 |
Correct |
528 ms |
16288 KB |
Output is correct |
60 |
Correct |
51 ms |
16396 KB |
Output is correct |
61 |
Correct |
53 ms |
16332 KB |
Output is correct |
62 |
Correct |
607 ms |
16372 KB |
Output is correct |
63 |
Correct |
321 ms |
16588 KB |
Output is correct |
64 |
Correct |
597 ms |
16392 KB |
Output is correct |
65 |
Correct |
462 ms |
16372 KB |
Output is correct |
66 |
Correct |
366 ms |
16412 KB |
Output is correct |
67 |
Correct |
366 ms |
16376 KB |
Output is correct |
68 |
Correct |
159 ms |
16456 KB |
Output is correct |
69 |
Correct |
153 ms |
16472 KB |
Output is correct |
70 |
Correct |
140 ms |
16508 KB |
Output is correct |
71 |
Correct |
145 ms |
16472 KB |
Output is correct |
72 |
Incorrect |
119 ms |
16444 KB |
Output isn't correct |
73 |
Incorrect |
368 ms |
17316 KB |
Output isn't correct |
74 |
Correct |
279 ms |
17232 KB |
Output is correct |
75 |
Correct |
348 ms |
17176 KB |
Output is correct |
76 |
Correct |
311 ms |
17204 KB |
Output is correct |
77 |
Correct |
296 ms |
17188 KB |
Output is correct |
78 |
Correct |
369 ms |
17148 KB |
Output is correct |
79 |
Correct |
246 ms |
17088 KB |
Output is correct |
80 |
Correct |
293 ms |
17332 KB |
Output is correct |
81 |
Correct |
317 ms |
17180 KB |
Output is correct |
82 |
Correct |
293 ms |
17116 KB |
Output is correct |
83 |
Correct |
418 ms |
17036 KB |
Output is correct |
84 |
Correct |
367 ms |
16996 KB |
Output is correct |
85 |
Correct |
397 ms |
17000 KB |
Output is correct |
86 |
Correct |
382 ms |
16988 KB |
Output is correct |
87 |
Correct |
392 ms |
17072 KB |
Output is correct |
88 |
Correct |
396 ms |
16844 KB |
Output is correct |
89 |
Correct |
415 ms |
16908 KB |
Output is correct |
90 |
Correct |
434 ms |
16844 KB |
Output is correct |
91 |
Correct |
419 ms |
16896 KB |
Output is correct |
92 |
Correct |
479 ms |
16908 KB |
Output is correct |