#include<bits/stdc++.h>
using namespace std;
const int N=2000009;
int a,c,d,e,i,j,ii,jj,zx,xc,S,B[809][809],lef,rig,mid,f[809][809],Mi,Mj,X[809][809],dis[809][809],Di,Dj;
bool bo[809][809];
string s;
char ch[809][809];
deque <pair <int, int> > de;
void chk(int q, int w){
if(q<1||q>a||w<1||w>a) return;
if(f[q][w]==0) return;
if(B[q][w]>B[i][j]+1){
B[q][w]=B[i][j]+1;
de.push_back({q,w});
}
}
void CHK(int q, int w){
if(q<1||q>a||w<1||w>a) return;
if(f[q][w]==0) return;
if(bo[q][w]==1) return;
if(dis[i][j]+1>X[q][w]) return;
bo[q][w]=1;
dis[q][w]=dis[i][j]+1;
de.push_back({q,w});
}
int main(){
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>a>>S;
for(i=1; i<=a; i++){
cin>>s;
for(j=1; j<=a; j++){
ch[i][j]=s[j-1];
}
}
for(i=0; i<=a+1; i++){
for(j=0; j<=a+1; j++){
B[i][j]=N;
}
}
for(i=1; i<=a; i++){
for(j=1; j<=a; j++){
if(ch[i][j]=='H'){
de.push_back({i,j});
B[i][j]=0;
}
if(ch[i][j]=='T'||ch[i][j]=='D'){
f[i][j]=0;
}else{
f[i][j]=1;
}
if(ch[i][j]=='M'){
Mi=i;Mj=j;
}
if(ch[i][j]=='D'){
Di=i;Dj=j;
}
}
}
while(de.size()){
i=de.front().first;j=de.front().second;
de.pop_front();
chk(i+1,j);chk(i-1,j);chk(i,j+1);chk(i,j-1);
}
/*for(i=1; i<=a; i++){
for(j=1; j<=a; j++){
cout<<B[i][j]<<" ";
}
cout<<"\n";
}*/
lef=-1;rig=a*a+2;
while(1){
if(lef+1>=rig) break;
mid=(lef+rig)/2;
if(B[Mi][Mj]<=mid){
rig=mid;
continue;
}
for(i=1; i<=a; i++){
for(j=1; j<=a; j++){
if(ch[i][j]=='T'||ch[i][j]=='H'){
f[i][j]=0;
}else{
f[i][j]=1;
}
bo[i][j]=0;
if(B[i][j]<=mid){
X[i][j]=-1;
}else{
zx=B[i][j]-mid;
X[i][j]=zx*S-1;
}
}
}
while(de.size()) de.pop_back();
de.push_back({Mi,Mj});bo[Mi][Mj]=1;dis[Mi][Mj]=0;
while(de.size()){
i=de.front().first;j=de.front().second;
de.pop_front();
CHK(i+1,j);CHK(i-1,j);CHK(i,j+1);CHK(i,j-1);
}
if(bo[Di][Dj]==1){
lef=mid;
}else{
rig=mid;
}
}
cout<<lef;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
120 ms |
11524 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
464 KB |
Output is correct |
12 |
Correct |
2 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
972 KB |
Output is correct |
14 |
Correct |
2 ms |
1108 KB |
Output is correct |
15 |
Correct |
0 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
588 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
716 KB |
Output is correct |
21 |
Correct |
1 ms |
852 KB |
Output is correct |
22 |
Correct |
1 ms |
852 KB |
Output is correct |
23 |
Correct |
1 ms |
844 KB |
Output is correct |
24 |
Correct |
1 ms |
980 KB |
Output is correct |
25 |
Correct |
1 ms |
980 KB |
Output is correct |
26 |
Correct |
1 ms |
1108 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 |
1 ms |
1232 KB |
Output is correct |
31 |
Correct |
1 ms |
1236 KB |
Output is correct |
32 |
Correct |
1 ms |
1364 KB |
Output is correct |
33 |
Correct |
9 ms |
4564 KB |
Output is correct |
34 |
Correct |
8 ms |
4820 KB |
Output is correct |
35 |
Correct |
24 ms |
5352 KB |
Output is correct |
36 |
Correct |
9 ms |
5208 KB |
Output is correct |
37 |
Correct |
10 ms |
5568 KB |
Output is correct |
38 |
Correct |
31 ms |
6100 KB |
Output is correct |
39 |
Correct |
12 ms |
5844 KB |
Output is correct |
40 |
Correct |
13 ms |
6268 KB |
Output is correct |
41 |
Correct |
55 ms |
6964 KB |
Output is correct |
42 |
Correct |
16 ms |
6552 KB |
Output is correct |
43 |
Correct |
22 ms |
6936 KB |
Output is correct |
44 |
Correct |
50 ms |
7708 KB |
Output is correct |
45 |
Correct |
18 ms |
7200 KB |
Output is correct |
46 |
Correct |
17 ms |
7568 KB |
Output is correct |
47 |
Correct |
59 ms |
8420 KB |
Output is correct |
48 |
Correct |
23 ms |
7724 KB |
Output is correct |
49 |
Correct |
23 ms |
8412 KB |
Output is correct |
50 |
Correct |
74 ms |
9244 KB |
Output is correct |
51 |
Correct |
25 ms |
8436 KB |
Output is correct |
52 |
Correct |
24 ms |
8904 KB |
Output is correct |
53 |
Correct |
91 ms |
9932 KB |
Output is correct |
54 |
Correct |
31 ms |
9148 KB |
Output is correct |
55 |
Correct |
28 ms |
9680 KB |
Output is correct |
56 |
Correct |
106 ms |
10784 KB |
Output is correct |
57 |
Correct |
32 ms |
9824 KB |
Output is correct |
58 |
Correct |
38 ms |
10284 KB |
Output is correct |
59 |
Correct |
143 ms |
11576 KB |
Output is correct |
60 |
Correct |
39 ms |
10372 KB |
Output is correct |
61 |
Correct |
37 ms |
11092 KB |
Output is correct |
62 |
Correct |
134 ms |
12372 KB |
Output is correct |
63 |
Correct |
116 ms |
12480 KB |
Output is correct |
64 |
Correct |
242 ms |
12312 KB |
Output is correct |
65 |
Correct |
161 ms |
12372 KB |
Output is correct |
66 |
Correct |
135 ms |
12432 KB |
Output is correct |
67 |
Correct |
128 ms |
12296 KB |
Output is correct |
68 |
Correct |
66 ms |
12368 KB |
Output is correct |
69 |
Correct |
75 ms |
12328 KB |
Output is correct |
70 |
Correct |
49 ms |
12384 KB |
Output is correct |
71 |
Correct |
56 ms |
12332 KB |
Output is correct |
72 |
Correct |
45 ms |
10424 KB |
Output is correct |
73 |
Correct |
45 ms |
12584 KB |
Output is correct |
74 |
Correct |
64 ms |
12652 KB |
Output is correct |
75 |
Correct |
78 ms |
12644 KB |
Output is correct |
76 |
Correct |
70 ms |
12656 KB |
Output is correct |
77 |
Correct |
73 ms |
12548 KB |
Output is correct |
78 |
Correct |
86 ms |
11212 KB |
Output is correct |
79 |
Correct |
62 ms |
10948 KB |
Output is correct |
80 |
Correct |
67 ms |
11048 KB |
Output is correct |
81 |
Correct |
74 ms |
11112 KB |
Output is correct |
82 |
Correct |
70 ms |
11144 KB |
Output is correct |
83 |
Correct |
93 ms |
11344 KB |
Output is correct |
84 |
Correct |
79 ms |
11172 KB |
Output is correct |
85 |
Correct |
84 ms |
11240 KB |
Output is correct |
86 |
Correct |
89 ms |
11224 KB |
Output is correct |
87 |
Correct |
86 ms |
11232 KB |
Output is correct |
88 |
Correct |
90 ms |
11372 KB |
Output is correct |
89 |
Correct |
91 ms |
11220 KB |
Output is correct |
90 |
Correct |
117 ms |
11316 KB |
Output is correct |
91 |
Correct |
90 ms |
11340 KB |
Output is correct |
92 |
Correct |
101 ms |
11340 KB |
Output is correct |