#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define F first
#define S second
#define pb push_back
#define size(a) (int) a.size()
#define all(a) a.begin(), a.end()
const int maxn = 800 + 4, inf = 1e8;
int n, s, mx, my, dx, dy, d[maxn][maxn], save[maxn][maxn];
bitset<maxn> vis[maxn], savevis[maxn];
string a[maxn];
queue<array<int, 2>> q;
void rest() {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
vis[i][j] = 0;
d[i][j] = inf;
}
}
return;
}
bool check0(int x, int y) {
bool ret = ((0 <= x) && (x < n));
ret &= ((0 <= y) && (y < n));
if (ret) {
ret &= (!vis[x][y]);
ret &= ((a[x][y] == 'G') || (a[x][y] == 'M'));
}
return ret;
}
void upd0(int x, int y) {
int dis = d[x][y] + 1;
if (check0(x - 1, y)) {
vis[x - 1][y] = 1;
d[x - 1][y] = dis;
q.push({x - 1, y});
}
if (check0(x + 1, y)) {
vis[x + 1][y] = 1;
d[x + 1][y] = dis;
q.push({x + 1, y});
}
if (check0(x, y - 1)) {
vis[x][y - 1] = 1;
d[x][y - 1] = dis;
q.push({x, y - 1});
}
if (check0(x, y + 1)) {
vis[x][y + 1] = 1;
d[x][y + 1] = dis;
q.push({x, y + 1});
}
}
bool check1(int x, int y, int dis, int k) {
bool ret = ((0 <= x) && (x < n));
ret &= ((0 <= y) && (y < n));
if (ret) {
ret &= (!vis[x][y]);
ret &= ((a[x][y] == 'G') || (a[x][y] == 'D'));
ret &= ((((dis + s - 1) / s) <= (save[x][y] - k)) || !savevis[x][y]);
}
return ret;
}
void upd1(int x, int y, int k) {
int dis = d[x][y] + 1;
if (check1(x - 1, y, dis, k)) {
vis[x - 1][y] = 1;
d[x - 1][y] = dis;
q.push({x - 1, y});
}
if (check1(x + 1, y, dis, k)) {
vis[x + 1][y] = 1;
d[x + 1][y] = dis;
q.push({x + 1, y});
}
if (check1(x, y - 1, dis, k)) {
vis[x][y - 1] = 1;
d[x][y - 1] = dis;
q.push({x, y - 1});
}
if (check1(x, y + 1, dis, k)) {
vis[x][y + 1] = 1;
d[x][y + 1] = dis;
q.push({x, y + 1});
}
}
bool bfs(int k) {
rest();
d[mx][my] = 0;
vis[mx][my] = 1;
q.push({mx, my});
while (!q.empty()) {
array<int, 2> v = q.front();
q.pop();
if (d[v[0]][v[1]] % s == 0 && d[v[0]][v[1]] / s == save[v[0]][v[1]] - k) {
continue;
}
upd1(v[0], v[1], k);
}
return (d[dx][dy] != inf);
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> s;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (a[i][j] == 'H') {
q.push({i, j});
vis[i][j] = 1;
}
}
}
while (!q.empty()) {
array<int, 2> v = q.front();
q.pop();
upd0(v[0], v[1]);
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (a[i][j] == 'M') {
mx = i;
my = j;
}
if (a[i][j] == 'D') {
dx = i;
dy = j;
}
save[i][j] = d[i][j];
savevis[i][j] = vis[i][j];
}
}
int l, r;
l = -1;
r = save[mx][my];
while (r - l > 1) {
int m = (l + r) >> 1;
if (bfs(m)) {
l = m;
}
else {
r = m;
}
}
cout << l << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
99 ms |
6228 KB |
Output is correct |
8 |
Correct |
0 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
736 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
0 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
468 KB |
Output is correct |
18 |
Correct |
0 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
592 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
772 KB |
Output is correct |
27 |
Correct |
1 ms |
724 KB |
Output is correct |
28 |
Correct |
1 ms |
724 KB |
Output is correct |
29 |
Correct |
1 ms |
724 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
1 ms |
852 KB |
Output is correct |
32 |
Correct |
1 ms |
852 KB |
Output is correct |
33 |
Correct |
8 ms |
2644 KB |
Output is correct |
34 |
Correct |
10 ms |
2896 KB |
Output is correct |
35 |
Correct |
22 ms |
2644 KB |
Output is correct |
36 |
Correct |
10 ms |
3028 KB |
Output is correct |
37 |
Correct |
10 ms |
3028 KB |
Output is correct |
38 |
Correct |
33 ms |
3028 KB |
Output is correct |
39 |
Correct |
13 ms |
3412 KB |
Output is correct |
40 |
Correct |
13 ms |
3496 KB |
Output is correct |
41 |
Correct |
41 ms |
3412 KB |
Output is correct |
42 |
Correct |
15 ms |
3876 KB |
Output is correct |
43 |
Correct |
15 ms |
3796 KB |
Output is correct |
44 |
Correct |
52 ms |
3864 KB |
Output is correct |
45 |
Correct |
19 ms |
4180 KB |
Output is correct |
46 |
Correct |
19 ms |
4304 KB |
Output is correct |
47 |
Correct |
60 ms |
4180 KB |
Output is correct |
48 |
Correct |
21 ms |
4688 KB |
Output is correct |
49 |
Correct |
23 ms |
4564 KB |
Output is correct |
50 |
Correct |
73 ms |
4632 KB |
Output is correct |
51 |
Correct |
25 ms |
4948 KB |
Output is correct |
52 |
Correct |
26 ms |
4948 KB |
Output is correct |
53 |
Correct |
85 ms |
4948 KB |
Output is correct |
54 |
Correct |
30 ms |
5384 KB |
Output is correct |
55 |
Correct |
30 ms |
5332 KB |
Output is correct |
56 |
Correct |
100 ms |
5332 KB |
Output is correct |
57 |
Correct |
33 ms |
5716 KB |
Output is correct |
58 |
Correct |
35 ms |
5716 KB |
Output is correct |
59 |
Correct |
122 ms |
5820 KB |
Output is correct |
60 |
Correct |
38 ms |
6204 KB |
Output is correct |
61 |
Correct |
40 ms |
6100 KB |
Output is correct |
62 |
Correct |
155 ms |
6208 KB |
Output is correct |
63 |
Correct |
126 ms |
6200 KB |
Output is correct |
64 |
Correct |
210 ms |
6100 KB |
Output is correct |
65 |
Correct |
206 ms |
6204 KB |
Output is correct |
66 |
Correct |
145 ms |
6200 KB |
Output is correct |
67 |
Correct |
133 ms |
6100 KB |
Output is correct |
68 |
Correct |
49 ms |
6228 KB |
Output is correct |
69 |
Correct |
41 ms |
6220 KB |
Output is correct |
70 |
Correct |
39 ms |
6256 KB |
Output is correct |
71 |
Correct |
31 ms |
6228 KB |
Output is correct |
72 |
Correct |
32 ms |
6228 KB |
Output is correct |
73 |
Correct |
38 ms |
6484 KB |
Output is correct |
74 |
Correct |
70 ms |
6480 KB |
Output is correct |
75 |
Correct |
75 ms |
6464 KB |
Output is correct |
76 |
Correct |
79 ms |
6484 KB |
Output is correct |
77 |
Correct |
77 ms |
6484 KB |
Output is correct |
78 |
Correct |
83 ms |
6456 KB |
Output is correct |
79 |
Correct |
64 ms |
6356 KB |
Output is correct |
80 |
Correct |
82 ms |
6540 KB |
Output is correct |
81 |
Correct |
76 ms |
6452 KB |
Output is correct |
82 |
Correct |
76 ms |
6452 KB |
Output is correct |
83 |
Correct |
91 ms |
6416 KB |
Output is correct |
84 |
Correct |
74 ms |
6420 KB |
Output is correct |
85 |
Correct |
77 ms |
6356 KB |
Output is correct |
86 |
Correct |
88 ms |
6392 KB |
Output is correct |
87 |
Correct |
78 ms |
6356 KB |
Output is correct |
88 |
Correct |
94 ms |
6356 KB |
Output is correct |
89 |
Correct |
95 ms |
6476 KB |
Output is correct |
90 |
Correct |
98 ms |
6356 KB |
Output is correct |
91 |
Correct |
98 ms |
6364 KB |
Output is correct |
92 |
Correct |
102 ms |
6356 KB |
Output is correct |