// source: https://oj.uz/problem/view/IOI09_mecho
#include<iostream>
#include<queue>
#define pa pair<int,int>
#define fi first
#define se second
using namespace std;
const int N=802, oo=1e9;
constexpr int X[]={0,1,0,-1,0};
int n,s;
char c[N][N];
int Ong[N][N],Gau[N][N];
pa St,Fi;
queue<pa> q;
bool inside(int x,int y){
return 1<=x&&x<=n&&1<=y&&y<=n;
}
void BFSOng(){
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
if(c[i][j]=='H') Ong[i][j]=0, q.push({i,j});
else Ong[i][j]=oo;
}
while(!q.empty()){
pa P=q.front(); q.pop();
for(int k=0;k<4;k++){
int nx=P.fi+X[k], ny=P.se+X[k+1];
if(inside(nx,ny)&&c[nx][ny]!='D'&&c[nx][ny]!='T')
if(Ong[nx][ny]==oo) Ong[nx][ny]=Ong[P.fi][P.se]+1, q.push({nx,ny});
}
}
}
bool check(int x){
while(!q.empty()) q.pop();
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
if(c[i][j]=='M') Gau[i][j]=0, q.push({i,j});
else Gau[i][j]=oo;
}
for(int i=x,step=s;!q.empty();i++,step+=s) while(!q.empty()){
pa P=q.front();
if(Gau[P.fi][P.se]==step) break;
if(P==Fi) return 1;
q.pop();
if(Ong[P.fi][P.se]<=i) continue;
for(int k=0;k<4;k++){
int nx=P.fi+X[k], ny=P.se+X[k+1];
if(inside(nx,ny)&&c[nx][ny]!='T')
if(Ong[nx][ny]>i&&Gau[nx][ny]==oo) Gau[nx][ny]=Gau[P.fi][P.se]+1, q.push({nx,ny});
}
}
return 0;
}
int main(){
cin>>n>>s;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
cin>>c[i][j];
if(c[i][j]=='M') St={i,j};
if(c[i][j]=='D') Fi={i,j};
}
BFSOng();
int l=0, r=Ong[St.fi][St.se], ans=-1;
while(l<=r){
int mid=(l+r)>>1;
if(check(mid)) ans=mid, l=++mid;
else r=--mid;
}
cout<<ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
304 KB |
Output is correct |
4 |
Correct |
1 ms |
304 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
308 KB |
Output is correct |
7 |
Correct |
98 ms |
6676 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
308 KB |
Output is correct |
11 |
Correct |
1 ms |
300 KB |
Output is correct |
12 |
Correct |
1 ms |
716 KB |
Output is correct |
13 |
Correct |
1 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
716 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
0 ms |
432 KB |
Output is correct |
18 |
Correct |
1 ms |
436 KB |
Output is correct |
19 |
Correct |
0 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
1 ms |
588 KB |
Output is correct |
22 |
Correct |
1 ms |
552 KB |
Output is correct |
23 |
Correct |
1 ms |
588 KB |
Output is correct |
24 |
Correct |
1 ms |
588 KB |
Output is correct |
25 |
Correct |
1 ms |
716 KB |
Output is correct |
26 |
Correct |
1 ms |
716 KB |
Output is correct |
27 |
Correct |
1 ms |
688 KB |
Output is correct |
28 |
Correct |
1 ms |
716 KB |
Output is correct |
29 |
Correct |
1 ms |
820 KB |
Output is correct |
30 |
Correct |
1 ms |
716 KB |
Output is correct |
31 |
Correct |
1 ms |
844 KB |
Output is correct |
32 |
Correct |
1 ms |
844 KB |
Output is correct |
33 |
Correct |
10 ms |
2892 KB |
Output is correct |
34 |
Correct |
10 ms |
2876 KB |
Output is correct |
35 |
Correct |
21 ms |
2900 KB |
Output is correct |
36 |
Correct |
13 ms |
3252 KB |
Output is correct |
37 |
Correct |
16 ms |
3216 KB |
Output is correct |
38 |
Correct |
30 ms |
3292 KB |
Output is correct |
39 |
Correct |
16 ms |
3604 KB |
Output is correct |
40 |
Correct |
17 ms |
3588 KB |
Output is correct |
41 |
Correct |
37 ms |
3668 KB |
Output is correct |
42 |
Correct |
21 ms |
3960 KB |
Output is correct |
43 |
Correct |
23 ms |
4012 KB |
Output is correct |
44 |
Correct |
43 ms |
4072 KB |
Output is correct |
45 |
Correct |
24 ms |
4408 KB |
Output is correct |
46 |
Correct |
24 ms |
4384 KB |
Output is correct |
47 |
Correct |
55 ms |
4404 KB |
Output is correct |
48 |
Correct |
32 ms |
4912 KB |
Output is correct |
49 |
Correct |
29 ms |
4888 KB |
Output is correct |
50 |
Correct |
66 ms |
4860 KB |
Output is correct |
51 |
Correct |
34 ms |
5272 KB |
Output is correct |
52 |
Correct |
36 ms |
5332 KB |
Output is correct |
53 |
Correct |
81 ms |
5312 KB |
Output is correct |
54 |
Correct |
40 ms |
5692 KB |
Output is correct |
55 |
Correct |
38 ms |
5700 KB |
Output is correct |
56 |
Correct |
89 ms |
5720 KB |
Output is correct |
57 |
Correct |
44 ms |
6196 KB |
Output is correct |
58 |
Correct |
45 ms |
6024 KB |
Output is correct |
59 |
Correct |
103 ms |
6076 KB |
Output is correct |
60 |
Correct |
53 ms |
6452 KB |
Output is correct |
61 |
Correct |
50 ms |
6452 KB |
Output is correct |
62 |
Correct |
115 ms |
6564 KB |
Output is correct |
63 |
Correct |
122 ms |
6520 KB |
Output is correct |
64 |
Correct |
180 ms |
6516 KB |
Output is correct |
65 |
Correct |
198 ms |
6564 KB |
Output is correct |
66 |
Correct |
137 ms |
6564 KB |
Output is correct |
67 |
Correct |
133 ms |
6452 KB |
Output is correct |
68 |
Correct |
70 ms |
6520 KB |
Output is correct |
69 |
Correct |
67 ms |
6492 KB |
Output is correct |
70 |
Correct |
68 ms |
6476 KB |
Output is correct |
71 |
Correct |
66 ms |
6568 KB |
Output is correct |
72 |
Correct |
59 ms |
6520 KB |
Output is correct |
73 |
Correct |
56 ms |
6836 KB |
Output is correct |
74 |
Correct |
89 ms |
6820 KB |
Output is correct |
75 |
Correct |
82 ms |
6952 KB |
Output is correct |
76 |
Correct |
82 ms |
6844 KB |
Output is correct |
77 |
Correct |
91 ms |
6776 KB |
Output is correct |
78 |
Correct |
89 ms |
6824 KB |
Output is correct |
79 |
Correct |
77 ms |
6700 KB |
Output is correct |
80 |
Correct |
100 ms |
6704 KB |
Output is correct |
81 |
Correct |
83 ms |
6820 KB |
Output is correct |
82 |
Correct |
81 ms |
6880 KB |
Output is correct |
83 |
Correct |
96 ms |
6704 KB |
Output is correct |
84 |
Correct |
84 ms |
6776 KB |
Output is correct |
85 |
Correct |
84 ms |
6776 KB |
Output is correct |
86 |
Correct |
98 ms |
6836 KB |
Output is correct |
87 |
Correct |
89 ms |
6780 KB |
Output is correct |
88 |
Correct |
96 ms |
6720 KB |
Output is correct |
89 |
Correct |
93 ms |
6724 KB |
Output is correct |
90 |
Correct |
98 ms |
6720 KB |
Output is correct |
91 |
Correct |
104 ms |
6612 KB |
Output is correct |
92 |
Correct |
104 ms |
6644 KB |
Output is correct |