#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
template<typename T> bool chkmin(T&x,T y){return x>y?x=y,1:0;}
template<typename T> bool chkmax(T&x,T y){return x<y?x=y,1:0;}
int n,s,sx,sy,ex,ey,dist[805][805],dis[805][805];
char base[805][805];
const int dir[4][2]={0,1,0,-1,1,0,-1,0};
void bfs(){
memset(dist,-1,sizeof dist);
queue<pii> que;
for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(base[i][j]=='H') que.push(mp(i,j)),dist[i][j]=0;
while(!que.empty()){
int i=que.front().fi;
int j=que.front().se;
que.pop();
for(int k=0;k<4;k++){
int ni=i+dir[k][0];
int nj=j+dir[k][1];
if(ni>=1&&ni<=n&&nj>=1&&nj<=n&&(base[ni][nj]=='G'||base[ni][nj]=='M')&dist[ni][nj]==-1){
que.push(mp(ni,nj)); dist[ni][nj]=dist[i][j]+s;
}
}
}
}
bool check(int t){
if(dist[sx][sy]<=t*s) return false;
memset(dis,-1,sizeof dis);
queue<pii> que;
que.push(mp(sx,sy)); dis[sx][sy]=t*s;
while(!que.empty()){
int i=que.front().fi;
int j=que.front().se;
que.pop();
for(int k=0;k<4;k++){
int ni=i+dir[k][0];
int nj=j+dir[k][1];
if(ni>=1&&ni<=n&&nj>=1&&nj<=n&&dis[ni][nj]==-1&&(base[ni][nj]=='D'||(base[ni][nj]=='G'&&dist[ni][nj]>dis[i][j]+1))){
que.push(mp(ni,nj)); dis[ni][nj]=dis[i][j]+1;
}
}
}
return dis[ex][ey]!=-1;
}
int main(){
scanf("%d%d",&n,&s);
for(int i=1;i<=n;i++){
scanf("%s",base[i]+1);
for(int j=1;j<=n;j++){
if(base[i][j]=='M') sx=i,sy=j;
if(base[i][j]=='D') ex=i,ey=j;
}
}
bfs();
int l=-1,r=n*n+5,ans=-1;
while(l<=r){
int m=(l+r)/2;
if(check(m)) ans=m,l=m+1; else r=m-1;
}
printf("%d",ans);
return 0;
}
Compilation message
mecho.cpp: In function 'void bfs()':
mecho.cpp:33:86: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]
33 | if(ni>=1&&ni<=n&&nj>=1&&nj<=n&&(base[ni][nj]=='G'||base[ni][nj]=='M')&dist[ni][nj]==-1){
| ~~~~~~~~~~~~^~~~
mecho.cpp: In function 'int main()':
mecho.cpp:61:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | scanf("%d%d",&n,&s);
| ~~~~~^~~~~~~~~~~~~~
mecho.cpp:63:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | scanf("%s",base[i]+1);
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5332 KB |
Output is correct |
2 |
Correct |
3 ms |
5332 KB |
Output is correct |
3 |
Correct |
4 ms |
5332 KB |
Output is correct |
4 |
Correct |
3 ms |
5332 KB |
Output is correct |
5 |
Correct |
3 ms |
5332 KB |
Output is correct |
6 |
Correct |
3 ms |
5332 KB |
Output is correct |
7 |
Correct |
81 ms |
6012 KB |
Output is correct |
8 |
Correct |
3 ms |
5332 KB |
Output is correct |
9 |
Correct |
4 ms |
5332 KB |
Output is correct |
10 |
Correct |
2 ms |
5332 KB |
Output is correct |
11 |
Correct |
3 ms |
5332 KB |
Output is correct |
12 |
Correct |
3 ms |
5332 KB |
Output is correct |
13 |
Correct |
2 ms |
5332 KB |
Output is correct |
14 |
Correct |
3 ms |
5332 KB |
Output is correct |
15 |
Correct |
3 ms |
5332 KB |
Output is correct |
16 |
Correct |
3 ms |
5332 KB |
Output is correct |
17 |
Correct |
3 ms |
5332 KB |
Output is correct |
18 |
Correct |
2 ms |
5332 KB |
Output is correct |
19 |
Correct |
3 ms |
5332 KB |
Output is correct |
20 |
Correct |
3 ms |
5332 KB |
Output is correct |
21 |
Correct |
3 ms |
5332 KB |
Output is correct |
22 |
Correct |
3 ms |
5332 KB |
Output is correct |
23 |
Correct |
3 ms |
5376 KB |
Output is correct |
24 |
Correct |
3 ms |
5332 KB |
Output is correct |
25 |
Correct |
3 ms |
5400 KB |
Output is correct |
26 |
Correct |
3 ms |
5332 KB |
Output is correct |
27 |
Correct |
3 ms |
5332 KB |
Output is correct |
28 |
Correct |
3 ms |
5332 KB |
Output is correct |
29 |
Correct |
4 ms |
5332 KB |
Output is correct |
30 |
Correct |
4 ms |
5332 KB |
Output is correct |
31 |
Correct |
3 ms |
5332 KB |
Output is correct |
32 |
Correct |
3 ms |
5336 KB |
Output is correct |
33 |
Correct |
6 ms |
5588 KB |
Output is correct |
34 |
Correct |
6 ms |
5652 KB |
Output is correct |
35 |
Correct |
19 ms |
5656 KB |
Output is correct |
36 |
Correct |
6 ms |
5588 KB |
Output is correct |
37 |
Correct |
7 ms |
5588 KB |
Output is correct |
38 |
Correct |
24 ms |
5588 KB |
Output is correct |
39 |
Correct |
7 ms |
5716 KB |
Output is correct |
40 |
Correct |
9 ms |
5716 KB |
Output is correct |
41 |
Correct |
29 ms |
5732 KB |
Output is correct |
42 |
Correct |
9 ms |
5664 KB |
Output is correct |
43 |
Correct |
11 ms |
5716 KB |
Output is correct |
44 |
Correct |
49 ms |
5696 KB |
Output is correct |
45 |
Correct |
10 ms |
5716 KB |
Output is correct |
46 |
Correct |
13 ms |
5808 KB |
Output is correct |
47 |
Correct |
45 ms |
5708 KB |
Output is correct |
48 |
Correct |
10 ms |
5844 KB |
Output is correct |
49 |
Correct |
12 ms |
5820 KB |
Output is correct |
50 |
Correct |
52 ms |
5840 KB |
Output is correct |
51 |
Correct |
12 ms |
5844 KB |
Output is correct |
52 |
Correct |
14 ms |
5844 KB |
Output is correct |
53 |
Correct |
58 ms |
5844 KB |
Output is correct |
54 |
Correct |
15 ms |
5828 KB |
Output is correct |
55 |
Correct |
22 ms |
5844 KB |
Output is correct |
56 |
Correct |
82 ms |
5916 KB |
Output is correct |
57 |
Correct |
15 ms |
5844 KB |
Output is correct |
58 |
Correct |
22 ms |
5964 KB |
Output is correct |
59 |
Correct |
100 ms |
5952 KB |
Output is correct |
60 |
Correct |
16 ms |
5908 KB |
Output is correct |
61 |
Correct |
23 ms |
5904 KB |
Output is correct |
62 |
Correct |
166 ms |
5992 KB |
Output is correct |
63 |
Correct |
86 ms |
5972 KB |
Output is correct |
64 |
Correct |
133 ms |
5988 KB |
Output is correct |
65 |
Correct |
159 ms |
5984 KB |
Output is correct |
66 |
Correct |
114 ms |
5996 KB |
Output is correct |
67 |
Correct |
100 ms |
5992 KB |
Output is correct |
68 |
Correct |
40 ms |
5924 KB |
Output is correct |
69 |
Correct |
53 ms |
6016 KB |
Output is correct |
70 |
Correct |
37 ms |
5940 KB |
Output is correct |
71 |
Correct |
31 ms |
5932 KB |
Output is correct |
72 |
Correct |
24 ms |
5928 KB |
Output is correct |
73 |
Correct |
33 ms |
6176 KB |
Output is correct |
74 |
Correct |
54 ms |
6304 KB |
Output is correct |
75 |
Correct |
55 ms |
6148 KB |
Output is correct |
76 |
Correct |
58 ms |
6164 KB |
Output is correct |
77 |
Correct |
54 ms |
6164 KB |
Output is correct |
78 |
Correct |
68 ms |
6236 KB |
Output is correct |
79 |
Correct |
40 ms |
6124 KB |
Output is correct |
80 |
Correct |
53 ms |
6164 KB |
Output is correct |
81 |
Correct |
59 ms |
6240 KB |
Output is correct |
82 |
Correct |
55 ms |
6228 KB |
Output is correct |
83 |
Correct |
71 ms |
6116 KB |
Output is correct |
84 |
Correct |
75 ms |
6200 KB |
Output is correct |
85 |
Correct |
81 ms |
6112 KB |
Output is correct |
86 |
Correct |
64 ms |
6204 KB |
Output is correct |
87 |
Correct |
75 ms |
6200 KB |
Output is correct |
88 |
Correct |
83 ms |
6144 KB |
Output is correct |
89 |
Correct |
103 ms |
6056 KB |
Output is correct |
90 |
Correct |
88 ms |
6052 KB |
Output is correct |
91 |
Correct |
74 ms |
6152 KB |
Output is correct |
92 |
Correct |
80 ms |
6144 KB |
Output is correct |