#include <bits/stdc++.h>
#define f first
#define s second
using ll = long long;
using namespace std;
const int inf = 1<<30;
int bees[800][800];
bool go[800][800];
bool vis[800][800];
int bfs[640000][3];
int p1, p2, len;
int ra, rb, rc;
pair<int, int> st, en;
pair<int, int> d4[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int N, S;
void push(int a, int b, int c){
bfs[p2][0] = a;
bfs[p2][1] = b;
bfs[p2][2] = c;
p2++;
if(p2 == 640000) p1 = 0;
len++;
}
void poll(){
ra = bfs[p1][0];
rb = bfs[p1][1];
rc = bfs[p1][2];
p1++;
if(p1 == 640000) p1 = 0;
len--;
}
void reset(int t){
p1 = p2 = 0;
push(st.f, st.s, 0);
vis[st.f][st.s] = 0;
while(len) {
poll();
for(pair<int, int> a: d4) {
int na = ra+a.f, nb = rb+a.s;
if(na >= 0 && na < N && nb >= 0 && nb < N &&
go[na][nb] && (rc+1)/S < bees[na][nb]-t && vis[na][nb]) {
push(na, nb, rc+1);
vis[na][nb] = 0;
}
}
}
}
bool works(int t){
p1 = p2 = 0;
push(st.f, st.s, 0);
vis[st.f][st.s] = 1;
while(len) {
poll();
for(pair<int, int> a: d4) {
int na = ra+a.f, nb = rb+a.s;
if(na >= 0 && na < N && nb >= 0 && nb < N &&
go[na][nb] && (rc+1)/S < bees[na][nb]-t && !vis[na][nb]) {
if(na == en.f && nb == en.s){
reset(t);
return 1;
}
push(na, nb, rc+1);
vis[na][nb] = 1;
}
}
}
reset(t);
return 0;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
p1 = p2 = len = 0;
cin >> N >> S;
for(int i = 0; i<N; i++) for(int j = 0; j<N; j++) bees[i][j] = inf;
for(int i = 0; i<N; i++) {
string c; cin >> c;
for(int j = 0; j<N; j++) {
go[i][j] = c[j] != 'T';
if(c[j] == 'M') st = {i, j};
if(c[j] == 'D') en = {i, j};
if(c[j] == 'H') {
push(i, j, 0);
bees[i][j] = 0;
}
}
}
while(len) {
poll();
int c = bees[ra][rb];
for(pair<int, int> a: d4) {
pair<int, int> next = {ra+a.f, rb+a.s};
if(next.f >= 0 && next.s >= 0 && next.f < N && next.s < N &&
go[next.f][next.s] && bees[next.f][next.s] == inf && (next.f != en.f || next.s != en.s)) {
bees[next.f][next.s] = c+1;
push(next.f, next.s, 0);
}
}
}
int lo = -1, hi = bees[st.f][st.s]-1;
while (lo < hi) {
int mid = (lo+hi+1)>>1;
if (works(mid)) lo = mid;
else hi = mid-1;
}
cout << lo;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
260 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
161 ms |
11332 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
3 ms |
2388 KB |
Output is correct |
34 |
Correct |
3 ms |
2388 KB |
Output is correct |
35 |
Correct |
33 ms |
3028 KB |
Output is correct |
36 |
Correct |
4 ms |
2772 KB |
Output is correct |
37 |
Correct |
4 ms |
2772 KB |
Output is correct |
38 |
Correct |
49 ms |
3616 KB |
Output is correct |
39 |
Correct |
4 ms |
3284 KB |
Output is correct |
40 |
Correct |
7 ms |
3288 KB |
Output is correct |
41 |
Correct |
79 ms |
4232 KB |
Output is correct |
42 |
Correct |
5 ms |
3760 KB |
Output is correct |
43 |
Correct |
6 ms |
3800 KB |
Output is correct |
44 |
Correct |
75 ms |
4820 KB |
Output is correct |
45 |
Correct |
6 ms |
4180 KB |
Output is correct |
46 |
Correct |
7 ms |
4296 KB |
Output is correct |
47 |
Correct |
101 ms |
5588 KB |
Output is correct |
48 |
Correct |
7 ms |
4820 KB |
Output is correct |
49 |
Correct |
11 ms |
4752 KB |
Output is correct |
50 |
Correct |
119 ms |
6332 KB |
Output is correct |
51 |
Correct |
9 ms |
5332 KB |
Output is correct |
52 |
Correct |
10 ms |
5332 KB |
Output is correct |
53 |
Correct |
134 ms |
7116 KB |
Output is correct |
54 |
Correct |
9 ms |
5972 KB |
Output is correct |
55 |
Correct |
12 ms |
5936 KB |
Output is correct |
56 |
Correct |
193 ms |
7944 KB |
Output is correct |
57 |
Correct |
10 ms |
6612 KB |
Output is correct |
58 |
Correct |
13 ms |
6612 KB |
Output is correct |
59 |
Correct |
186 ms |
8820 KB |
Output is correct |
60 |
Correct |
11 ms |
7252 KB |
Output is correct |
61 |
Correct |
13 ms |
7220 KB |
Output is correct |
62 |
Correct |
224 ms |
9716 KB |
Output is correct |
63 |
Correct |
177 ms |
8580 KB |
Output is correct |
64 |
Correct |
306 ms |
8572 KB |
Output is correct |
65 |
Correct |
313 ms |
8536 KB |
Output is correct |
66 |
Correct |
216 ms |
8576 KB |
Output is correct |
67 |
Correct |
209 ms |
8524 KB |
Output is correct |
68 |
Correct |
63 ms |
8576 KB |
Output is correct |
69 |
Correct |
49 ms |
8584 KB |
Output is correct |
70 |
Correct |
43 ms |
8532 KB |
Output is correct |
71 |
Correct |
38 ms |
8532 KB |
Output is correct |
72 |
Correct |
32 ms |
8140 KB |
Output is correct |
73 |
Correct |
50 ms |
11532 KB |
Output is correct |
74 |
Correct |
97 ms |
11528 KB |
Output is correct |
75 |
Correct |
99 ms |
11528 KB |
Output is correct |
76 |
Correct |
102 ms |
11524 KB |
Output is correct |
77 |
Correct |
111 ms |
11480 KB |
Output is correct |
78 |
Correct |
119 ms |
11148 KB |
Output is correct |
79 |
Correct |
84 ms |
11160 KB |
Output is correct |
80 |
Correct |
101 ms |
11180 KB |
Output is correct |
81 |
Correct |
125 ms |
11216 KB |
Output is correct |
82 |
Correct |
94 ms |
11068 KB |
Output is correct |
83 |
Correct |
138 ms |
11200 KB |
Output is correct |
84 |
Correct |
113 ms |
11180 KB |
Output is correct |
85 |
Correct |
149 ms |
11184 KB |
Output is correct |
86 |
Correct |
138 ms |
11232 KB |
Output is correct |
87 |
Correct |
115 ms |
11084 KB |
Output is correct |
88 |
Correct |
160 ms |
11324 KB |
Output is correct |
89 |
Correct |
142 ms |
11204 KB |
Output is correct |
90 |
Correct |
155 ms |
11252 KB |
Output is correct |
91 |
Correct |
159 ms |
11224 KB |
Output is correct |
92 |
Correct |
153 ms |
11212 KB |
Output is correct |