#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MX=810, inf=2e9;
int n, s;
char B[MX][MX];
int xs, ys, xl, yl;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
int Y[MX][MX];
bool valid(int x, int y){
return 1<=x && x<=n && 1<=y && y<=n && (B[x][y]=='G' || B[x][y]=='H');
}
void bees(){
queue<pii> Q;
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++){
if(B[i][j]=='H') Y[i][j]=0, Q.push({i,j});
else Y[i][j]=-1;
}
while(!Q.empty()){
pii p=Q.front(); Q.pop();
int x,y; tie(x,y)=p;
for(int k=0; k<4; k++){
int u=x+dx[k], v=y+dy[k];
if(!valid(u,v) || Y[u][v]>=0) continue;
Y[u][v]=Y[x][y]+s;
Q.push({u,v});
}
}
}
int D[MX][MX];
bool solve(int t){
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
D[i][j]=-1;
queue<pii> Q;
D[xs][ys]=t*s;
if(D[xs][ys]>=Y[xs][ys]) return false;
Q.push({xs,ys});
while(!Q.empty()){
int x,y; tie(x,y)=Q.front(); Q.pop();
for(int k=0; k<4; k++){
int u=x+dx[k], v=y+dy[k];
if(B[u][v]=='D') return true;
if(!valid(u,v) || D[u][v]>=0) continue;
int now=D[x][y]+1;
if(Y[u][v]>=0 && now>=Y[u][v]) continue;
D[u][v]=D[x][y]+1;
Q.push({u,v});
}
}
return false;
/*
cout<<"\nSOLVED "<<t<<'\n';
for(int i=1; i<=n; i++, cout<<'\n')
for(int j=1; j<=n; j++)
cout<<D[i][j]<<' ';
*/
}
int main(){
ios::sync_with_stdio(0); cin.tie(0);
cin>>n>>s;
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++){
cin>>B[i][j];
if(B[i][j]=='M') xs=i, ys=j, B[i][j]='G';
if(B[i][j]=='D') xl=i, yl=j;
}
bees();
int s=0, e=n*n;
while(s<e){
int m=(s+e+1)/2;
if(solve(m)) s=m;
else e=m-1;
}
cout<<(solve(s) ? s : -1);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
488 KB |
Output is correct |
3 |
Correct |
2 ms |
556 KB |
Output is correct |
4 |
Correct |
2 ms |
556 KB |
Output is correct |
5 |
Correct |
2 ms |
556 KB |
Output is correct |
6 |
Correct |
2 ms |
556 KB |
Output is correct |
7 |
Correct |
134 ms |
6252 KB |
Output is correct |
8 |
Correct |
2 ms |
6252 KB |
Output is correct |
9 |
Correct |
2 ms |
6252 KB |
Output is correct |
10 |
Correct |
2 ms |
6252 KB |
Output is correct |
11 |
Correct |
2 ms |
6252 KB |
Output is correct |
12 |
Correct |
3 ms |
6252 KB |
Output is correct |
13 |
Correct |
2 ms |
6252 KB |
Output is correct |
14 |
Correct |
3 ms |
6252 KB |
Output is correct |
15 |
Correct |
3 ms |
6252 KB |
Output is correct |
16 |
Correct |
3 ms |
6252 KB |
Output is correct |
17 |
Correct |
2 ms |
6252 KB |
Output is correct |
18 |
Correct |
3 ms |
6252 KB |
Output is correct |
19 |
Correct |
2 ms |
6252 KB |
Output is correct |
20 |
Correct |
2 ms |
6252 KB |
Output is correct |
21 |
Correct |
2 ms |
6252 KB |
Output is correct |
22 |
Correct |
2 ms |
6252 KB |
Output is correct |
23 |
Correct |
2 ms |
6252 KB |
Output is correct |
24 |
Correct |
2 ms |
6252 KB |
Output is correct |
25 |
Correct |
2 ms |
6252 KB |
Output is correct |
26 |
Correct |
2 ms |
6252 KB |
Output is correct |
27 |
Correct |
2 ms |
6252 KB |
Output is correct |
28 |
Correct |
2 ms |
6252 KB |
Output is correct |
29 |
Correct |
2 ms |
6252 KB |
Output is correct |
30 |
Correct |
2 ms |
6252 KB |
Output is correct |
31 |
Correct |
2 ms |
6252 KB |
Output is correct |
32 |
Correct |
2 ms |
6252 KB |
Output is correct |
33 |
Correct |
8 ms |
6252 KB |
Output is correct |
34 |
Correct |
8 ms |
6252 KB |
Output is correct |
35 |
Correct |
29 ms |
6252 KB |
Output is correct |
36 |
Correct |
10 ms |
6252 KB |
Output is correct |
37 |
Correct |
10 ms |
6252 KB |
Output is correct |
38 |
Correct |
39 ms |
6252 KB |
Output is correct |
39 |
Correct |
11 ms |
6252 KB |
Output is correct |
40 |
Correct |
12 ms |
6252 KB |
Output is correct |
41 |
Correct |
58 ms |
6252 KB |
Output is correct |
42 |
Correct |
15 ms |
6252 KB |
Output is correct |
43 |
Correct |
14 ms |
6252 KB |
Output is correct |
44 |
Correct |
65 ms |
6252 KB |
Output is correct |
45 |
Correct |
19 ms |
6252 KB |
Output is correct |
46 |
Correct |
17 ms |
6252 KB |
Output is correct |
47 |
Correct |
76 ms |
6252 KB |
Output is correct |
48 |
Correct |
18 ms |
6252 KB |
Output is correct |
49 |
Correct |
19 ms |
6252 KB |
Output is correct |
50 |
Correct |
98 ms |
6252 KB |
Output is correct |
51 |
Correct |
22 ms |
6252 KB |
Output is correct |
52 |
Correct |
22 ms |
6252 KB |
Output is correct |
53 |
Correct |
122 ms |
6252 KB |
Output is correct |
54 |
Correct |
28 ms |
6252 KB |
Output is correct |
55 |
Correct |
24 ms |
6252 KB |
Output is correct |
56 |
Correct |
139 ms |
6252 KB |
Output is correct |
57 |
Correct |
27 ms |
6252 KB |
Output is correct |
58 |
Correct |
29 ms |
6252 KB |
Output is correct |
59 |
Correct |
161 ms |
6252 KB |
Output is correct |
60 |
Correct |
30 ms |
6252 KB |
Output is correct |
61 |
Correct |
30 ms |
6268 KB |
Output is correct |
62 |
Correct |
182 ms |
6268 KB |
Output is correct |
63 |
Correct |
137 ms |
6272 KB |
Output is correct |
64 |
Correct |
249 ms |
6388 KB |
Output is correct |
65 |
Correct |
197 ms |
6420 KB |
Output is correct |
66 |
Correct |
166 ms |
6420 KB |
Output is correct |
67 |
Correct |
156 ms |
6420 KB |
Output is correct |
68 |
Correct |
70 ms |
6428 KB |
Output is correct |
69 |
Correct |
65 ms |
6428 KB |
Output is correct |
70 |
Correct |
63 ms |
6428 KB |
Output is correct |
71 |
Correct |
57 ms |
6428 KB |
Output is correct |
72 |
Correct |
45 ms |
6428 KB |
Output is correct |
73 |
Correct |
58 ms |
6492 KB |
Output is correct |
74 |
Correct |
87 ms |
6492 KB |
Output is correct |
75 |
Correct |
104 ms |
6492 KB |
Output is correct |
76 |
Correct |
100 ms |
6492 KB |
Output is correct |
77 |
Correct |
106 ms |
6492 KB |
Output is correct |
78 |
Correct |
111 ms |
6532 KB |
Output is correct |
79 |
Correct |
71 ms |
6532 KB |
Output is correct |
80 |
Correct |
86 ms |
6532 KB |
Output is correct |
81 |
Correct |
115 ms |
6532 KB |
Output is correct |
82 |
Correct |
85 ms |
6532 KB |
Output is correct |
83 |
Correct |
113 ms |
6532 KB |
Output is correct |
84 |
Correct |
108 ms |
6532 KB |
Output is correct |
85 |
Correct |
166 ms |
6532 KB |
Output is correct |
86 |
Correct |
117 ms |
6564 KB |
Output is correct |
87 |
Correct |
106 ms |
6564 KB |
Output is correct |
88 |
Correct |
117 ms |
6564 KB |
Output is correct |
89 |
Correct |
127 ms |
6564 KB |
Output is correct |
90 |
Correct |
133 ms |
6564 KB |
Output is correct |
91 |
Correct |
120 ms |
6564 KB |
Output is correct |
92 |
Correct |
121 ms |
6564 KB |
Output is correct |