#include <bits/stdc++.h>
using namespace std;
const int mxN=800;
int n, s, d1[mxN][mxN], d2[mxN][mxN], si, sj, ti, tj, lb=0, rb=mxN*mxN, nb[4][2]={{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
string g[mxN];
array<int, 2> qu[mxN*mxN];
void bfs(int d[mxN][mxN], int a) {
int qt=0;
for(int i=0; i<n; ++i)
for(int j=0; j<n; ++j)
if(!d[i][j])
qu[qt++]={i, j};
for(int i=0; i<qt; ++i) {
array<int, 2> u=qu[i];
// if(a)
// cout << u[0] << " " << u[1] << endl;
if(a>=0&&d[u[0]][u[1]]/s+a>=d1[u[0]][u[1]])
continue;
for(int j=0; j<4; ++j) {
int ni=u[0]+nb[j][0], nj=u[1]+nb[j][1];
if(ni>=0&&ni<n&&nj>=0&&nj<n&&g[ni][nj]!='T'&&(a>=0||g[ni][nj]!='D')&&d[ni][nj]>1e7) {
d[ni][nj]=d[u[0]][u[1]]+1;
qu[qt++]={ni, nj};
}
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> s;
for(int i=0; i<n; ++i)
cin >> g[i];
memset(d1, 1, sizeof(d1));
for(int i=0; i<n; ++i) {
for(int j=0; j<n; ++j) {
if(g[i][j]=='M') {
si=i;
sj=j;
} else if(g[i][j]=='D') {
ti=i;
tj=j;
} else if(g[i][j]=='H')
d1[i][j]=0;
}
}
bfs(d1, -1);
while(lb<=rb) {
int mb=(lb+rb)/2;
// cout << "bs " << mb << endl;
memset(d2, 1, sizeof(d2));
d2[si][sj]=0;
bfs(d2, mb);
if(d2[ti][tj]<1e7)
lb=mb+1;
else
rb=mb-1;
}
cout << rb;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
5368 KB |
Output is correct |
2 |
Correct |
9 ms |
5540 KB |
Output is correct |
3 |
Correct |
9 ms |
5540 KB |
Output is correct |
4 |
Correct |
8 ms |
5540 KB |
Output is correct |
5 |
Correct |
8 ms |
5540 KB |
Output is correct |
6 |
Correct |
9 ms |
5540 KB |
Output is correct |
7 |
Correct |
130 ms |
11128 KB |
Output is correct |
8 |
Correct |
8 ms |
11128 KB |
Output is correct |
9 |
Correct |
9 ms |
11128 KB |
Output is correct |
10 |
Correct |
8 ms |
11128 KB |
Output is correct |
11 |
Correct |
9 ms |
11128 KB |
Output is correct |
12 |
Correct |
8 ms |
11128 KB |
Output is correct |
13 |
Correct |
9 ms |
11128 KB |
Output is correct |
14 |
Correct |
9 ms |
11128 KB |
Output is correct |
15 |
Correct |
9 ms |
11128 KB |
Output is correct |
16 |
Correct |
9 ms |
11128 KB |
Output is correct |
17 |
Correct |
8 ms |
11128 KB |
Output is correct |
18 |
Correct |
8 ms |
11128 KB |
Output is correct |
19 |
Correct |
8 ms |
11128 KB |
Output is correct |
20 |
Correct |
8 ms |
11128 KB |
Output is correct |
21 |
Correct |
9 ms |
11128 KB |
Output is correct |
22 |
Correct |
8 ms |
11128 KB |
Output is correct |
23 |
Correct |
8 ms |
11128 KB |
Output is correct |
24 |
Correct |
8 ms |
11128 KB |
Output is correct |
25 |
Correct |
9 ms |
11128 KB |
Output is correct |
26 |
Correct |
9 ms |
11128 KB |
Output is correct |
27 |
Correct |
9 ms |
11128 KB |
Output is correct |
28 |
Correct |
9 ms |
11128 KB |
Output is correct |
29 |
Correct |
9 ms |
11128 KB |
Output is correct |
30 |
Correct |
8 ms |
11128 KB |
Output is correct |
31 |
Correct |
8 ms |
11128 KB |
Output is correct |
32 |
Correct |
9 ms |
11128 KB |
Output is correct |
33 |
Correct |
14 ms |
11128 KB |
Output is correct |
34 |
Correct |
13 ms |
11128 KB |
Output is correct |
35 |
Correct |
35 ms |
11128 KB |
Output is correct |
36 |
Correct |
15 ms |
11128 KB |
Output is correct |
37 |
Correct |
16 ms |
11128 KB |
Output is correct |
38 |
Correct |
44 ms |
11128 KB |
Output is correct |
39 |
Correct |
16 ms |
11128 KB |
Output is correct |
40 |
Correct |
17 ms |
11128 KB |
Output is correct |
41 |
Correct |
54 ms |
11128 KB |
Output is correct |
42 |
Correct |
20 ms |
11128 KB |
Output is correct |
43 |
Correct |
19 ms |
11128 KB |
Output is correct |
44 |
Correct |
106 ms |
11128 KB |
Output is correct |
45 |
Correct |
23 ms |
11128 KB |
Output is correct |
46 |
Correct |
25 ms |
11128 KB |
Output is correct |
47 |
Correct |
77 ms |
11128 KB |
Output is correct |
48 |
Correct |
24 ms |
11128 KB |
Output is correct |
49 |
Correct |
24 ms |
11128 KB |
Output is correct |
50 |
Correct |
135 ms |
11128 KB |
Output is correct |
51 |
Correct |
26 ms |
11128 KB |
Output is correct |
52 |
Correct |
26 ms |
11128 KB |
Output is correct |
53 |
Correct |
134 ms |
11128 KB |
Output is correct |
54 |
Correct |
27 ms |
11128 KB |
Output is correct |
55 |
Correct |
27 ms |
11128 KB |
Output is correct |
56 |
Correct |
173 ms |
11128 KB |
Output is correct |
57 |
Correct |
34 ms |
11128 KB |
Output is correct |
58 |
Correct |
34 ms |
11128 KB |
Output is correct |
59 |
Correct |
195 ms |
11128 KB |
Output is correct |
60 |
Correct |
35 ms |
11128 KB |
Output is correct |
61 |
Correct |
39 ms |
11128 KB |
Output is correct |
62 |
Correct |
209 ms |
11128 KB |
Output is correct |
63 |
Correct |
147 ms |
11128 KB |
Output is correct |
64 |
Correct |
290 ms |
11128 KB |
Output is correct |
65 |
Correct |
229 ms |
11128 KB |
Output is correct |
66 |
Correct |
188 ms |
11128 KB |
Output is correct |
67 |
Correct |
159 ms |
11128 KB |
Output is correct |
68 |
Correct |
74 ms |
11128 KB |
Output is correct |
69 |
Correct |
72 ms |
11128 KB |
Output is correct |
70 |
Correct |
57 ms |
11128 KB |
Output is correct |
71 |
Correct |
63 ms |
11128 KB |
Output is correct |
72 |
Correct |
49 ms |
11128 KB |
Output is correct |
73 |
Correct |
59 ms |
11260 KB |
Output is correct |
74 |
Correct |
90 ms |
11260 KB |
Output is correct |
75 |
Correct |
109 ms |
11260 KB |
Output is correct |
76 |
Correct |
93 ms |
11260 KB |
Output is correct |
77 |
Correct |
103 ms |
11260 KB |
Output is correct |
78 |
Correct |
115 ms |
11260 KB |
Output is correct |
79 |
Correct |
76 ms |
11260 KB |
Output is correct |
80 |
Correct |
93 ms |
11264 KB |
Output is correct |
81 |
Correct |
116 ms |
11264 KB |
Output is correct |
82 |
Correct |
111 ms |
11264 KB |
Output is correct |
83 |
Correct |
125 ms |
11264 KB |
Output is correct |
84 |
Correct |
116 ms |
11264 KB |
Output is correct |
85 |
Correct |
132 ms |
11264 KB |
Output is correct |
86 |
Correct |
122 ms |
11264 KB |
Output is correct |
87 |
Correct |
115 ms |
11264 KB |
Output is correct |
88 |
Correct |
124 ms |
11264 KB |
Output is correct |
89 |
Correct |
125 ms |
11352 KB |
Output is correct |
90 |
Correct |
139 ms |
11352 KB |
Output is correct |
91 |
Correct |
142 ms |
11352 KB |
Output is correct |
92 |
Correct |
145 ms |
11352 KB |
Output is correct |