#include<bits/stdc++.h>
#define ll long long
#define mp make_pair
#define pub push_back
#define pob pop_back
#define ss second
#define ff first
#define ext exit(0)
using namespace std;
int n,s,l,r,x,y,zu[801][801],bo[801][801],ax,ay,h[4]={0,0,-1,1},v[4]={-1,1,0,0};
char c[801][801];
queue<pair<pair<int,int>,int> >q,p;
void dfs(){
memset(bo,0,sizeof bo);
bo[q.front().ff.ff][q.front().ff.ss]=1;
zu[q.front().ff.ff][q.front().ff.ss]=0;
while(!q.empty()){
int ii=q.front().ff.ff,jj=q.front().ff.ss,zz=q.front().ss;
for(int k=0;k<4;k++){
if(ii+h[k]<=n && ii+h[k]>0 && jj+v[k]<=n && jj+v[k]>0 && c[ii+h[k]][jj+v[k]]!='T' && c[ii+h[k]][jj+v[k]]!='D' && !bo[ii+h[k]][jj+v[k]] && zu[ii+h[k]][jj+v[k]]>zz+s){
bo[ii+h[k]][jj+v[k]]=1;
zu[ii+h[k]][jj+v[k]]=zz+s;
q.push(mp(mp(ii+h[k],jj+v[k]),zz+s));
}
}
q.pop();
}
}
bool can(int k){
q=p;
if(k>=zu[x][y]) return 0;
memset(bo,0,sizeof bo);
q.push(mp(mp(x,y),k));
bo[x][y]=1;
while(!q.empty()){
int xx=q.front().ff.ff,yy=q.front().ff.ss,kk=q.front().ss;
for(int i=0;i<4;i++){
if(bo[ax][ay]) return 1;
if(xx+h[i]>0 && xx+h[i]<=n && yy+v[i]>0 && yy+v[i]<=n && c[xx+h[i]][yy+v[i]]!='T' && !bo[xx+h[i]][yy+v[i]] && zu[xx+h[i]][yy+v[i]]>kk+1){
bo[xx+h[i]][yy+v[i]]=1;
q.push(mp(mp(xx+h[i],yy+v[i]),kk+1));
}
}
q.pop();
}
return 0;
}
int main (){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
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') x=i,y=j;
if(c[i][j]=='D') ax=i,ay=j;
zu[i][j]=1e9;
}
}
for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(c[i][j]=='H')q.push(mp(mp(i,j),0));
dfs();
if(can(0)==0) {
cout<<-1;
ext;
}
l=0,r=1e9;
while(l+1!=r){
ll mid=(l+r)/2;
if(can(mid)) l=mid;
else r=mid;
}
cout<<l/s;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2936 KB |
Output is correct |
2 |
Correct |
7 ms |
3048 KB |
Output is correct |
3 |
Correct |
7 ms |
3048 KB |
Output is correct |
4 |
Correct |
6 ms |
3048 KB |
Output is correct |
5 |
Correct |
7 ms |
3048 KB |
Output is correct |
6 |
Correct |
6 ms |
3152 KB |
Output is correct |
7 |
Correct |
260 ms |
6348 KB |
Output is correct |
8 |
Correct |
5 ms |
6348 KB |
Output is correct |
9 |
Correct |
6 ms |
6348 KB |
Output is correct |
10 |
Correct |
7 ms |
6348 KB |
Output is correct |
11 |
Correct |
6 ms |
6348 KB |
Output is correct |
12 |
Correct |
9 ms |
6348 KB |
Output is correct |
13 |
Correct |
6 ms |
6348 KB |
Output is correct |
14 |
Correct |
5 ms |
6348 KB |
Output is correct |
15 |
Correct |
5 ms |
6348 KB |
Output is correct |
16 |
Correct |
6 ms |
6348 KB |
Output is correct |
17 |
Correct |
5 ms |
6348 KB |
Output is correct |
18 |
Correct |
5 ms |
6348 KB |
Output is correct |
19 |
Correct |
6 ms |
6348 KB |
Output is correct |
20 |
Correct |
6 ms |
6348 KB |
Output is correct |
21 |
Correct |
5 ms |
6348 KB |
Output is correct |
22 |
Correct |
7 ms |
6348 KB |
Output is correct |
23 |
Correct |
5 ms |
6348 KB |
Output is correct |
24 |
Correct |
6 ms |
6348 KB |
Output is correct |
25 |
Correct |
6 ms |
6348 KB |
Output is correct |
26 |
Correct |
7 ms |
6348 KB |
Output is correct |
27 |
Correct |
6 ms |
6348 KB |
Output is correct |
28 |
Correct |
7 ms |
6348 KB |
Output is correct |
29 |
Correct |
5 ms |
6348 KB |
Output is correct |
30 |
Correct |
7 ms |
6348 KB |
Output is correct |
31 |
Correct |
6 ms |
6348 KB |
Output is correct |
32 |
Correct |
6 ms |
6348 KB |
Output is correct |
33 |
Correct |
12 ms |
6348 KB |
Output is correct |
34 |
Correct |
12 ms |
6348 KB |
Output is correct |
35 |
Correct |
60 ms |
6348 KB |
Output is correct |
36 |
Correct |
13 ms |
6348 KB |
Output is correct |
37 |
Correct |
17 ms |
6348 KB |
Output is correct |
38 |
Correct |
124 ms |
6348 KB |
Output is correct |
39 |
Correct |
13 ms |
6348 KB |
Output is correct |
40 |
Correct |
14 ms |
6348 KB |
Output is correct |
41 |
Correct |
118 ms |
6348 KB |
Output is correct |
42 |
Correct |
17 ms |
6348 KB |
Output is correct |
43 |
Correct |
20 ms |
6348 KB |
Output is correct |
44 |
Correct |
120 ms |
6348 KB |
Output is correct |
45 |
Correct |
18 ms |
6348 KB |
Output is correct |
46 |
Correct |
22 ms |
6348 KB |
Output is correct |
47 |
Correct |
217 ms |
6348 KB |
Output is correct |
48 |
Correct |
21 ms |
6348 KB |
Output is correct |
49 |
Correct |
21 ms |
6348 KB |
Output is correct |
50 |
Correct |
214 ms |
6348 KB |
Output is correct |
51 |
Correct |
24 ms |
6348 KB |
Output is correct |
52 |
Correct |
24 ms |
6348 KB |
Output is correct |
53 |
Correct |
254 ms |
6348 KB |
Output is correct |
54 |
Correct |
24 ms |
6348 KB |
Output is correct |
55 |
Correct |
30 ms |
6348 KB |
Output is correct |
56 |
Correct |
299 ms |
6348 KB |
Output is correct |
57 |
Correct |
27 ms |
6348 KB |
Output is correct |
58 |
Correct |
25 ms |
6348 KB |
Output is correct |
59 |
Correct |
339 ms |
6348 KB |
Output is correct |
60 |
Correct |
26 ms |
6348 KB |
Output is correct |
61 |
Correct |
29 ms |
6348 KB |
Output is correct |
62 |
Correct |
402 ms |
6348 KB |
Output is correct |
63 |
Correct |
184 ms |
6396 KB |
Output is correct |
64 |
Correct |
456 ms |
6396 KB |
Output is correct |
65 |
Correct |
301 ms |
6396 KB |
Output is correct |
66 |
Correct |
229 ms |
6396 KB |
Output is correct |
67 |
Correct |
49 ms |
6396 KB |
Output is correct |
68 |
Correct |
111 ms |
6396 KB |
Output is correct |
69 |
Correct |
87 ms |
6396 KB |
Output is correct |
70 |
Correct |
71 ms |
6396 KB |
Output is correct |
71 |
Correct |
74 ms |
6396 KB |
Output is correct |
72 |
Correct |
51 ms |
6396 KB |
Output is correct |
73 |
Correct |
72 ms |
6652 KB |
Output is correct |
74 |
Correct |
207 ms |
6652 KB |
Output is correct |
75 |
Correct |
127 ms |
6652 KB |
Output is correct |
76 |
Correct |
115 ms |
6652 KB |
Output is correct |
77 |
Correct |
144 ms |
6652 KB |
Output is correct |
78 |
Correct |
48 ms |
6652 KB |
Output is correct |
79 |
Correct |
141 ms |
6652 KB |
Output is correct |
80 |
Correct |
145 ms |
6652 KB |
Output is correct |
81 |
Correct |
165 ms |
6652 KB |
Output is correct |
82 |
Correct |
197 ms |
6708 KB |
Output is correct |
83 |
Correct |
195 ms |
6708 KB |
Output is correct |
84 |
Correct |
278 ms |
6708 KB |
Output is correct |
85 |
Correct |
204 ms |
6708 KB |
Output is correct |
86 |
Correct |
210 ms |
6708 KB |
Output is correct |
87 |
Correct |
246 ms |
6708 KB |
Output is correct |
88 |
Correct |
233 ms |
6708 KB |
Output is correct |
89 |
Correct |
311 ms |
6708 KB |
Output is correct |
90 |
Correct |
284 ms |
6708 KB |
Output is correct |
91 |
Correct |
268 ms |
6708 KB |
Output is correct |
92 |
Correct |
234 ms |
6708 KB |
Output is correct |