#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define ll long long
#define ld long double
const int inf=1e9;
vector<string>a;
bool was[850][850];
int depth[850][850],dist[850][850],n,S;
bool Dobar(int x,int y){
if(x<0||x>=n||y<0||y>=n||a[x][y]=='T'||was[x][y]) return false;
return true;
}
int main(){
scanf("%i%i",&n,&S);
for(int i=0;i<n;i++){string s;cin>>s;a.pb(s);}
int X0,Y0,X1,Y1;
vector<pair<int,int> >H;
for(int i=0;i<n;i++) for(int j=0;j<n;j++){if(a[i][j]=='M'){X0=i,Y0=j;}if(a[i][j]=='H')H.pb({i,j});if(a[i][j]=='D'){X1=i,Y1=j;}}
queue<pair<int,int> >kju;
for(auto i:H){kju.push(i);was[i.fi][i.se]=true;}
while(kju.size()){
int u=kju.front().fi,v=kju.front().se;kju.pop();
if(Dobar(u-1,v)&&!(u-1==X1&&v==Y1)){depth[u-1][v]=depth[u][v]+1;was[u-1][v]=true;kju.push({u-1,v});}
if(Dobar(u+1,v)&&!(u+1==X1&&v==Y1)){depth[u+1][v]=depth[u][v]+1;was[u+1][v]=true;kju.push({u+1,v});}
if(Dobar(u,v-1)&&!(u==X1&&v-1==Y1)){depth[u][v-1]=depth[u][v]+1;was[u][v-1]=true;kju.push({u,v-1});}
if(Dobar(u,v+1)&&!(u==X1&&v+1==Y1)){depth[u][v+1]=depth[u][v]+1;was[u][v+1]=true;kju.push({u,v+1});}
}
depth[X1][Y1]=inf;
//for(int i=0;i<n;i++) {for(int j=0;j<n;j++) printf("%i ",depth[i][j]);printf("\n");}
int l=0,r=5000,res=-1;
while(l<=r){
//printf("%i %i**\n",l,r);
int mid=l+r>>1;
for(int i=0;i<=n;i++) for(int j=0;j<=n;j++) {dist[i][j]=0;was[i][j]=false;}
if(depth[X0][Y0]>mid) kju.push({X0,Y0});was[X0][Y0]=true;
bool moze=false;
for(int i=S,dst=mid;kju.size();i+=S,dst++){
while(kju.size()){
int u=kju.front().fi,v=kju.front().se;
//printf("%i %i %i\n",u,v,dst);
if(u==X1&&v==Y1) moze=true;
if(depth[u][v]<=dst){kju.pop();continue;}
if(dist[u][v]>=i) break;
kju.pop();
if(Dobar(u-1,v)&&depth[u-1][v]>dst){dist[u-1][v]=dist[u][v]+1;was[u-1][v]=true;kju.push({u-1,v});}
if(Dobar(u+1,v)&&depth[u+1][v]>dst){dist[u+1][v]=dist[u][v]+1;was[u+1][v]=true;kju.push({u+1,v});}
if(Dobar(u,v-1)&&depth[u][v-1]>dst){dist[u][v-1]=dist[u][v]+1;was[u][v-1]=true;kju.push({u,v-1});}
if(Dobar(u,v+1)&&depth[u][v+1]>dst){dist[u][v+1]=dist[u][v]+1;was[u][v+1]=true;kju.push({u,v+1});}
}
}
if(moze) res=mid,l=mid+1;
else r=mid-1;
}
printf("%i\n",res);
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:36:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
36 | int mid=l+r>>1;
| ~^~
mecho.cpp:38:3: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
38 | if(depth[X0][Y0]>mid) kju.push({X0,Y0});was[X0][Y0]=true;
| ^~
mecho.cpp:38:43: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
38 | if(depth[X0][Y0]>mid) kju.push({X0,Y0});was[X0][Y0]=true;
| ^~~
mecho.cpp:17:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | scanf("%i%i",&n,&S);
| ~~~~~^~~~~~~~~~~~~~
mecho.cpp:38:18: warning: 'Y0' may be used uninitialized in this function [-Wmaybe-uninitialized]
38 | if(depth[X0][Y0]>mid) kju.push({X0,Y0});was[X0][Y0]=true;
| ~~~~~~~~~~~~^
mecho.cpp:38:18: warning: 'X0' may be used uninitialized in this function [-Wmaybe-uninitialized]
mecho.cpp:44:13: warning: 'Y1' may be used uninitialized in this function [-Wmaybe-uninitialized]
44 | if(u==X1&&v==Y1) moze=true;
| ~~~~~^~~~~~~
mecho.cpp:44:5: warning: 'X1' may be used uninitialized in this function [-Wmaybe-uninitialized]
44 | if(u==X1&&v==Y1) moze=true;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
60 ms |
7760 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
600 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
0 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
0 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
0 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
0 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
860 KB |
Output is correct |
26 |
Correct |
1 ms |
860 KB |
Output is correct |
27 |
Correct |
1 ms |
860 KB |
Output is correct |
28 |
Correct |
1 ms |
856 KB |
Output is correct |
29 |
Correct |
1 ms |
856 KB |
Output is correct |
30 |
Correct |
1 ms |
860 KB |
Output is correct |
31 |
Correct |
1 ms |
860 KB |
Output is correct |
32 |
Correct |
1 ms |
860 KB |
Output is correct |
33 |
Incorrect |
13 ms |
3268 KB |
Output isn't correct |
34 |
Incorrect |
14 ms |
3344 KB |
Output isn't correct |
35 |
Correct |
15 ms |
3164 KB |
Output is correct |
36 |
Incorrect |
17 ms |
3676 KB |
Output isn't correct |
37 |
Incorrect |
19 ms |
3652 KB |
Output isn't correct |
38 |
Correct |
20 ms |
3568 KB |
Output is correct |
39 |
Incorrect |
20 ms |
4216 KB |
Output isn't correct |
40 |
Incorrect |
23 ms |
4184 KB |
Output isn't correct |
41 |
Correct |
25 ms |
4036 KB |
Output is correct |
42 |
Incorrect |
25 ms |
4700 KB |
Output isn't correct |
43 |
Incorrect |
29 ms |
4700 KB |
Output isn't correct |
44 |
Correct |
31 ms |
4696 KB |
Output is correct |
45 |
Incorrect |
30 ms |
4956 KB |
Output isn't correct |
46 |
Incorrect |
35 ms |
4952 KB |
Output isn't correct |
47 |
Correct |
37 ms |
4956 KB |
Output is correct |
48 |
Incorrect |
35 ms |
5468 KB |
Output isn't correct |
49 |
Incorrect |
48 ms |
5468 KB |
Output isn't correct |
50 |
Correct |
44 ms |
5464 KB |
Output is correct |
51 |
Incorrect |
41 ms |
5980 KB |
Output isn't correct |
52 |
Incorrect |
51 ms |
5976 KB |
Output isn't correct |
53 |
Correct |
61 ms |
5980 KB |
Output is correct |
54 |
Incorrect |
50 ms |
6488 KB |
Output isn't correct |
55 |
Incorrect |
57 ms |
6492 KB |
Output isn't correct |
56 |
Correct |
65 ms |
6592 KB |
Output is correct |
57 |
Incorrect |
55 ms |
7196 KB |
Output isn't correct |
58 |
Incorrect |
69 ms |
7000 KB |
Output isn't correct |
59 |
Correct |
75 ms |
7000 KB |
Output is correct |
60 |
Incorrect |
66 ms |
7720 KB |
Output isn't correct |
61 |
Incorrect |
77 ms |
7504 KB |
Output isn't correct |
62 |
Correct |
84 ms |
7516 KB |
Output is correct |
63 |
Correct |
85 ms |
7728 KB |
Output is correct |
64 |
Correct |
151 ms |
7608 KB |
Output is correct |
65 |
Correct |
120 ms |
7732 KB |
Output is correct |
66 |
Correct |
97 ms |
7652 KB |
Output is correct |
67 |
Correct |
90 ms |
7624 KB |
Output is correct |
68 |
Correct |
41 ms |
7760 KB |
Output is correct |
69 |
Correct |
42 ms |
7664 KB |
Output is correct |
70 |
Correct |
34 ms |
7768 KB |
Output is correct |
71 |
Correct |
36 ms |
7656 KB |
Output is correct |
72 |
Correct |
31 ms |
7504 KB |
Output is correct |
73 |
Correct |
27 ms |
8016 KB |
Output is correct |
74 |
Correct |
46 ms |
7924 KB |
Output is correct |
75 |
Correct |
52 ms |
7936 KB |
Output is correct |
76 |
Correct |
46 ms |
8024 KB |
Output is correct |
77 |
Correct |
49 ms |
8028 KB |
Output is correct |
78 |
Correct |
56 ms |
7768 KB |
Output is correct |
79 |
Correct |
37 ms |
7760 KB |
Output is correct |
80 |
Correct |
46 ms |
7764 KB |
Output is correct |
81 |
Correct |
53 ms |
7988 KB |
Output is correct |
82 |
Correct |
54 ms |
7772 KB |
Output is correct |
83 |
Correct |
58 ms |
7948 KB |
Output is correct |
84 |
Correct |
57 ms |
7768 KB |
Output is correct |
85 |
Correct |
57 ms |
7764 KB |
Output is correct |
86 |
Correct |
55 ms |
7760 KB |
Output is correct |
87 |
Correct |
60 ms |
8164 KB |
Output is correct |
88 |
Correct |
59 ms |
7768 KB |
Output is correct |
89 |
Correct |
57 ms |
7768 KB |
Output is correct |
90 |
Correct |
65 ms |
7764 KB |
Output is correct |
91 |
Correct |
60 ms |
7760 KB |
Output is correct |
92 |
Correct |
63 ms |
7772 KB |
Output is correct |