#include <iostream>
#include <cstdio>
#include <queue>
#include <stack>
#include <tuple>
#include <algorithm>
using namespace std;
using I = int;
using C = char;
using B = bool;
const I N = 800;
C cels[N][N];
B tres[N][N];
B bees[N][N];
B viss[N][N];
queue<tuple<I, I, I>> bee_que;
stack<tuple<I, I, I>> rem_stk;
stack<tuple<I, I, I>> add_stk;
queue<tuple<I, I, I, I>> mec_que;
I n, s;
I mi, mj;
I di, dj;
B bnd(I i, I j) {
return i >= 0 && i < n && j >= 0 && j < n;
}
void bee_vis(I i, I j, I tim) {
if (!bnd(i, j))
return;
if (tres[i][j] || bees[i][j])
return;
if (i == di && j == dj)
return;
bees[i][j] = true;
bee_que.push({i, j, tim});
rem_stk.push({i, j, tim});
}
B mec_vis(I i, I j, I tim, I mid) {
if (!bnd(i, j))
return false;
if (tres[i][j] || bees[i][j] || viss[i][j])
return false;
if (i == di && j == dj)
return true;
viss[i][j] = true;
mec_que.push({i, j, tim + mid / s, mid % s});
return false;
}
void set(I cur) {
while (!rem_stk.empty()) {
const auto [i, j, tim] = rem_stk.top();
if (tim <= cur)
break;
rem_stk.pop();
bees[i][j] = false;
add_stk.push({i, j, tim});
}
while (!add_stk.empty()) {
const auto [i, j, tim] = add_stk.top();
if (tim > cur)
break;
add_stk.pop();
bees[i][j] = true;
rem_stk.push({i, j, tim});
}
}
void fil() {
while (!bee_que.empty()) {
const auto [i, j, tim] = bee_que.front();
bee_que.pop();
bee_vis(i - 1, j, tim + 1);
bee_vis(i + 1, j, tim + 1);
bee_vis(i, j - 1, tim + 1);
bee_vis(i, j + 1, tim + 1);
}
}
B tes(I cur) {
fill(&viss[0][0], &viss[0][0] + sizeof(viss) / sizeof(B), false);
while (!mec_que.empty())
mec_que.pop();
set(cur);
mec_vis(mi, mj, cur, 0);
while (!mec_que.empty()) {
const auto [i, j, tim, mid] = mec_que.front();
mec_que.pop();
if (cur != tim) {
cur = tim;
set(cur);
}
if (bees[i][j])
continue;
if (mec_vis(i - 1, j, tim, mid + 1))
return true;
if (mec_vis(i + 1, j, tim, mid + 1))
return true;
if (mec_vis(i, j - 1, tim, mid + 1))
return true;
if (mec_vis(i, j + 1, tim, mid + 1))
return true;
}
return false;
}
I slv() {
fil();
I l = -1;
I r = get<2>(rem_stk.top());
while (l < r) {
const I m = l + (r - l + 1) / 2;
if (tes(m))
l = m;
else
r = m - 1;
}
return l;
}
I main(void) {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> s;
for (I i = 0; i < n; i++)
cin >> cels[i];
for (I i = 0; i < n; i++) {
for (I j = 0; j < n; j++) {
const C cel = cels[i][j];
if (cel == 'T')
tres[i][j] = true;
else if (cel == 'M') {
mi = i;
mj = j;
} else if (cel == 'D') {
di = i;
dj = j;
} else if (cel == 'H')
bee_vis(i, j, 0);
}
}
printf("%i\n", slv());
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
852 KB |
Output is correct |
2 |
Correct |
1 ms |
852 KB |
Output is correct |
3 |
Correct |
1 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
113 ms |
11420 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
968 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
2 ms |
1108 KB |
Output is correct |
13 |
Correct |
2 ms |
1108 KB |
Output is correct |
14 |
Correct |
2 ms |
1108 KB |
Output is correct |
15 |
Correct |
1 ms |
980 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
1 ms |
976 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
1 ms |
972 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
972 KB |
Output is correct |
23 |
Correct |
1 ms |
980 KB |
Output is correct |
24 |
Correct |
1 ms |
1108 KB |
Output is correct |
25 |
Correct |
1 ms |
1108 KB |
Output is correct |
26 |
Correct |
1 ms |
1100 KB |
Output is correct |
27 |
Correct |
1 ms |
1108 KB |
Output is correct |
28 |
Correct |
1 ms |
1108 KB |
Output is correct |
29 |
Correct |
1 ms |
1108 KB |
Output is correct |
30 |
Correct |
2 ms |
1108 KB |
Output is correct |
31 |
Correct |
1 ms |
1108 KB |
Output is correct |
32 |
Correct |
1 ms |
1100 KB |
Output is correct |
33 |
Correct |
4 ms |
2644 KB |
Output is correct |
34 |
Correct |
5 ms |
2644 KB |
Output is correct |
35 |
Correct |
22 ms |
3028 KB |
Output is correct |
36 |
Correct |
6 ms |
3028 KB |
Output is correct |
37 |
Correct |
5 ms |
3032 KB |
Output is correct |
38 |
Correct |
30 ms |
3524 KB |
Output is correct |
39 |
Correct |
7 ms |
3412 KB |
Output is correct |
40 |
Correct |
7 ms |
3540 KB |
Output is correct |
41 |
Correct |
40 ms |
4140 KB |
Output is correct |
42 |
Correct |
7 ms |
3924 KB |
Output is correct |
43 |
Correct |
9 ms |
3948 KB |
Output is correct |
44 |
Correct |
46 ms |
4736 KB |
Output is correct |
45 |
Correct |
10 ms |
4456 KB |
Output is correct |
46 |
Correct |
10 ms |
4436 KB |
Output is correct |
47 |
Correct |
59 ms |
5392 KB |
Output is correct |
48 |
Correct |
13 ms |
4972 KB |
Output is correct |
49 |
Correct |
12 ms |
4976 KB |
Output is correct |
50 |
Correct |
71 ms |
6212 KB |
Output is correct |
51 |
Correct |
12 ms |
5488 KB |
Output is correct |
52 |
Correct |
13 ms |
5504 KB |
Output is correct |
53 |
Correct |
84 ms |
6904 KB |
Output is correct |
54 |
Correct |
16 ms |
6072 KB |
Output is correct |
55 |
Correct |
17 ms |
6076 KB |
Output is correct |
56 |
Correct |
101 ms |
7628 KB |
Output is correct |
57 |
Correct |
15 ms |
6652 KB |
Output is correct |
58 |
Correct |
16 ms |
6740 KB |
Output is correct |
59 |
Correct |
105 ms |
8556 KB |
Output is correct |
60 |
Correct |
18 ms |
7356 KB |
Output is correct |
61 |
Correct |
19 ms |
7348 KB |
Output is correct |
62 |
Correct |
134 ms |
9568 KB |
Output is correct |
63 |
Correct |
142 ms |
8272 KB |
Output is correct |
64 |
Correct |
178 ms |
8272 KB |
Output is correct |
65 |
Correct |
227 ms |
8228 KB |
Output is correct |
66 |
Correct |
158 ms |
8208 KB |
Output is correct |
67 |
Correct |
143 ms |
8260 KB |
Output is correct |
68 |
Correct |
95 ms |
8248 KB |
Output is correct |
69 |
Correct |
44 ms |
8160 KB |
Output is correct |
70 |
Correct |
67 ms |
8268 KB |
Output is correct |
71 |
Correct |
59 ms |
8276 KB |
Output is correct |
72 |
Correct |
38 ms |
8100 KB |
Output is correct |
73 |
Correct |
62 ms |
11444 KB |
Output is correct |
74 |
Correct |
104 ms |
11228 KB |
Output is correct |
75 |
Correct |
102 ms |
11204 KB |
Output is correct |
76 |
Correct |
118 ms |
11232 KB |
Output is correct |
77 |
Correct |
79 ms |
11228 KB |
Output is correct |
78 |
Correct |
121 ms |
11520 KB |
Output is correct |
79 |
Correct |
65 ms |
11216 KB |
Output is correct |
80 |
Correct |
83 ms |
11280 KB |
Output is correct |
81 |
Correct |
100 ms |
11356 KB |
Output is correct |
82 |
Correct |
76 ms |
11228 KB |
Output is correct |
83 |
Correct |
137 ms |
11416 KB |
Output is correct |
84 |
Correct |
80 ms |
11184 KB |
Output is correct |
85 |
Correct |
79 ms |
11164 KB |
Output is correct |
86 |
Correct |
95 ms |
11328 KB |
Output is correct |
87 |
Correct |
114 ms |
11212 KB |
Output is correct |
88 |
Correct |
141 ms |
11484 KB |
Output is correct |
89 |
Correct |
101 ms |
11264 KB |
Output is correct |
90 |
Correct |
100 ms |
11272 KB |
Output is correct |
91 |
Correct |
93 ms |
11264 KB |
Output is correct |
92 |
Correct |
137 ms |
11492 KB |
Output is correct |