#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 + 1;
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 |
30 ms |
15516 KB |
Output is correct |
2 |
Correct |
30 ms |
15500 KB |
Output is correct |
3 |
Correct |
30 ms |
15492 KB |
Output is correct |
4 |
Correct |
29 ms |
15500 KB |
Output is correct |
5 |
Correct |
29 ms |
15516 KB |
Output is correct |
6 |
Correct |
32 ms |
15564 KB |
Output is correct |
7 |
Correct |
476 ms |
16464 KB |
Output is correct |
8 |
Correct |
30 ms |
15516 KB |
Output is correct |
9 |
Correct |
28 ms |
15492 KB |
Output is correct |
10 |
Correct |
28 ms |
15504 KB |
Output is correct |
11 |
Correct |
30 ms |
15564 KB |
Output is correct |
12 |
Incorrect |
28 ms |
15540 KB |
Output isn't correct |
13 |
Incorrect |
32 ms |
15552 KB |
Output isn't correct |
14 |
Correct |
31 ms |
15588 KB |
Output is correct |
15 |
Correct |
27 ms |
15500 KB |
Output is correct |
16 |
Correct |
30 ms |
15508 KB |
Output is correct |
17 |
Correct |
28 ms |
15556 KB |
Output is correct |
18 |
Correct |
29 ms |
15520 KB |
Output is correct |
19 |
Correct |
29 ms |
15512 KB |
Output is correct |
20 |
Correct |
29 ms |
15524 KB |
Output is correct |
21 |
Correct |
29 ms |
15516 KB |
Output is correct |
22 |
Correct |
30 ms |
15468 KB |
Output is correct |
23 |
Correct |
28 ms |
15540 KB |
Output is correct |
24 |
Correct |
29 ms |
15508 KB |
Output is correct |
25 |
Correct |
31 ms |
15544 KB |
Output is correct |
26 |
Correct |
31 ms |
15488 KB |
Output is correct |
27 |
Correct |
31 ms |
15504 KB |
Output is correct |
28 |
Correct |
29 ms |
15564 KB |
Output is correct |
29 |
Correct |
32 ms |
15500 KB |
Output is correct |
30 |
Correct |
30 ms |
15504 KB |
Output is correct |
31 |
Correct |
27 ms |
15476 KB |
Output is correct |
32 |
Correct |
31 ms |
15528 KB |
Output is correct |
33 |
Correct |
33 ms |
15680 KB |
Output is correct |
34 |
Correct |
25 ms |
15604 KB |
Output is correct |
35 |
Correct |
121 ms |
15616 KB |
Output is correct |
36 |
Correct |
34 ms |
15664 KB |
Output is correct |
37 |
Correct |
28 ms |
15604 KB |
Output is correct |
38 |
Correct |
152 ms |
15664 KB |
Output is correct |
39 |
Correct |
25 ms |
15628 KB |
Output is correct |
40 |
Correct |
35 ms |
15760 KB |
Output is correct |
41 |
Correct |
194 ms |
15692 KB |
Output is correct |
42 |
Correct |
27 ms |
15720 KB |
Output is correct |
43 |
Correct |
39 ms |
15792 KB |
Output is correct |
44 |
Correct |
243 ms |
15796 KB |
Output is correct |
45 |
Correct |
28 ms |
15772 KB |
Output is correct |
46 |
Correct |
45 ms |
15824 KB |
Output is correct |
47 |
Correct |
283 ms |
15832 KB |
Output is correct |
48 |
Correct |
43 ms |
15892 KB |
Output is correct |
49 |
Correct |
30 ms |
15820 KB |
Output is correct |
50 |
Correct |
325 ms |
15876 KB |
Output is correct |
51 |
Correct |
41 ms |
15932 KB |
Output is correct |
52 |
Correct |
36 ms |
15984 KB |
Output is correct |
53 |
Correct |
407 ms |
15988 KB |
Output is correct |
54 |
Correct |
41 ms |
16012 KB |
Output is correct |
55 |
Correct |
37 ms |
15948 KB |
Output is correct |
56 |
Correct |
481 ms |
16084 KB |
Output is correct |
57 |
Correct |
43 ms |
16092 KB |
Output is correct |
58 |
Correct |
44 ms |
16056 KB |
Output is correct |
59 |
Correct |
586 ms |
16164 KB |
Output is correct |
60 |
Correct |
50 ms |
16164 KB |
Output is correct |
61 |
Correct |
50 ms |
16084 KB |
Output is correct |
62 |
Correct |
647 ms |
16164 KB |
Output is correct |
63 |
Correct |
333 ms |
16156 KB |
Output is correct |
64 |
Correct |
619 ms |
16244 KB |
Output is correct |
65 |
Correct |
481 ms |
16164 KB |
Output is correct |
66 |
Correct |
374 ms |
16176 KB |
Output is correct |
67 |
Correct |
367 ms |
16144 KB |
Output is correct |
68 |
Correct |
163 ms |
16252 KB |
Output is correct |
69 |
Correct |
155 ms |
16268 KB |
Output is correct |
70 |
Correct |
125 ms |
16256 KB |
Output is correct |
71 |
Correct |
142 ms |
16300 KB |
Output is correct |
72 |
Incorrect |
125 ms |
16292 KB |
Output isn't correct |
73 |
Incorrect |
357 ms |
16972 KB |
Output isn't correct |
74 |
Correct |
274 ms |
16956 KB |
Output is correct |
75 |
Correct |
360 ms |
16976 KB |
Output is correct |
76 |
Correct |
309 ms |
16964 KB |
Output is correct |
77 |
Correct |
314 ms |
17156 KB |
Output is correct |
78 |
Correct |
373 ms |
16880 KB |
Output is correct |
79 |
Correct |
245 ms |
16992 KB |
Output is correct |
80 |
Correct |
296 ms |
16888 KB |
Output is correct |
81 |
Correct |
328 ms |
16876 KB |
Output is correct |
82 |
Correct |
299 ms |
16916 KB |
Output is correct |
83 |
Correct |
419 ms |
16760 KB |
Output is correct |
84 |
Correct |
385 ms |
16868 KB |
Output is correct |
85 |
Correct |
398 ms |
16896 KB |
Output is correct |
86 |
Correct |
391 ms |
16804 KB |
Output is correct |
87 |
Correct |
395 ms |
16764 KB |
Output is correct |
88 |
Correct |
426 ms |
16752 KB |
Output is correct |
89 |
Correct |
420 ms |
16624 KB |
Output is correct |
90 |
Correct |
451 ms |
16632 KB |
Output is correct |
91 |
Correct |
442 ms |
16628 KB |
Output is correct |
92 |
Correct |
477 ms |
16604 KB |
Output is correct |