#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define rep2(i,a,b) for(int i=a; i<(b); i++)
#define all(x) x.begin(),x.end()
using ll=long long;
const ll INF=1LL<<60;
const int four[5]={0,1,0,-1,0};
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
ll s;
cin>>n>>s;
vector<string> forest(n);
rep(i,n){
cin>>forest[i];
}
vector<vector<ll>> bee(n,vector<ll>(n,INF));
queue<pair<int,int>> vert;
rep(i,n){
rep(j,n){
if(forest[i][j]=='H'){
bee[i][j]=0;
vert.push({i,j});
}
}
}
while(!vert.empty()){
int x,y;
tie(x,y)=vert.front();
vert.pop();
rep(i,4){
int nx=x+four[i],ny=y+four[i+1];
if(nx<0 or n<=nx or ny<0 or n<=ny){
continue;
}
if(forest[nx][ny]!='G' and forest[nx][ny]!='M'){
continue;
}
if(bee[nx][ny]>bee[x][y]+1){
bee[nx][ny]=bee[x][y]+1;
vert.push({nx,ny});
}
}
}
int hx,hy;
ll ok=-1,ng;
rep(i,n){
rep(j,n){
if(forest[i][j]=='D'){
hx=i;
hy=j;
}
if(forest[i][j]=='M'){
ng=bee[i][j];
}
}
}
vector<vector<ll>> dist(n,vector<ll>(n));
while(ng-ok>1){
ll mid=(ok+ng)>>1;
rep(i,n){
rep(j,n){
if(forest[i][j]=='M'){
dist[i][j]=mid*s;
vert.push({i,j});
}
else{
dist[i][j]=INF;
}
}
}
bool can=false;
while(!vert.empty()){
int x,y;
tie(x,y)=vert.front();
vert.pop();
rep(i,4){
int nx=x+four[i],ny=y+four[i+1];
if(nx<0 or n<=nx or ny<0 or n<=ny){
continue;
}
if(forest[nx][ny]=='D'){
can=true;
}
if(forest[nx][ny]!='G'){
continue;
}
if((dist[x][y]+1LL)/s>=bee[nx][ny]){
continue;
}
if(dist[nx][ny]>dist[x][y]+1){
dist[nx][ny]=dist[x][y]+1;
vert.push({nx,ny});
}
}
}
if(can){
ok=mid;
}
else{
ng=mid;
}
}
cout<<ok<<"\n";
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:48:9: warning: variable 'hx' set but not used [-Wunused-but-set-variable]
48 | int hx,hy;
| ^~
mecho.cpp:48:12: warning: variable 'hy' set but not used [-Wunused-but-set-variable]
48 | int hx,hy;
| ^~
mecho.cpp:63:19: warning: 'ng' may be used uninitialized in this function [-Wmaybe-uninitialized]
63 | ll mid=(ok+ng)>>1;
| ~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 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 |
88 ms |
11244 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
504 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
6 ms |
2396 KB |
Output is correct |
34 |
Correct |
6 ms |
2396 KB |
Output is correct |
35 |
Correct |
19 ms |
2396 KB |
Output is correct |
36 |
Correct |
7 ms |
3164 KB |
Output is correct |
37 |
Correct |
8 ms |
3164 KB |
Output is correct |
38 |
Correct |
26 ms |
3412 KB |
Output is correct |
39 |
Correct |
10 ms |
3672 KB |
Output is correct |
40 |
Correct |
12 ms |
3676 KB |
Output is correct |
41 |
Correct |
32 ms |
3676 KB |
Output is correct |
42 |
Correct |
12 ms |
4444 KB |
Output is correct |
43 |
Correct |
12 ms |
4696 KB |
Output is correct |
44 |
Correct |
42 ms |
4664 KB |
Output is correct |
45 |
Correct |
14 ms |
5468 KB |
Output is correct |
46 |
Correct |
19 ms |
5468 KB |
Output is correct |
47 |
Correct |
64 ms |
5468 KB |
Output is correct |
48 |
Correct |
17 ms |
6492 KB |
Output is correct |
49 |
Correct |
17 ms |
6388 KB |
Output is correct |
50 |
Correct |
57 ms |
6488 KB |
Output is correct |
51 |
Correct |
20 ms |
7512 KB |
Output is correct |
52 |
Correct |
21 ms |
7516 KB |
Output is correct |
53 |
Correct |
69 ms |
7572 KB |
Output is correct |
54 |
Correct |
24 ms |
8540 KB |
Output is correct |
55 |
Correct |
23 ms |
8540 KB |
Output is correct |
56 |
Correct |
94 ms |
8728 KB |
Output is correct |
57 |
Correct |
26 ms |
9816 KB |
Output is correct |
58 |
Correct |
28 ms |
9820 KB |
Output is correct |
59 |
Correct |
98 ms |
9820 KB |
Output is correct |
60 |
Correct |
30 ms |
11108 KB |
Output is correct |
61 |
Correct |
32 ms |
11100 KB |
Output is correct |
62 |
Correct |
145 ms |
11100 KB |
Output is correct |
63 |
Correct |
96 ms |
11220 KB |
Output is correct |
64 |
Correct |
153 ms |
11100 KB |
Output is correct |
65 |
Correct |
146 ms |
11220 KB |
Output is correct |
66 |
Correct |
114 ms |
11356 KB |
Output is correct |
67 |
Correct |
100 ms |
11100 KB |
Output is correct |
68 |
Correct |
44 ms |
11100 KB |
Output is correct |
69 |
Correct |
39 ms |
11072 KB |
Output is correct |
70 |
Correct |
37 ms |
11100 KB |
Output is correct |
71 |
Correct |
31 ms |
11100 KB |
Output is correct |
72 |
Correct |
40 ms |
11060 KB |
Output is correct |
73 |
Correct |
38 ms |
11092 KB |
Output is correct |
74 |
Correct |
68 ms |
11064 KB |
Output is correct |
75 |
Correct |
61 ms |
11072 KB |
Output is correct |
76 |
Correct |
65 ms |
11276 KB |
Output is correct |
77 |
Correct |
91 ms |
11344 KB |
Output is correct |
78 |
Correct |
76 ms |
11256 KB |
Output is correct |
79 |
Correct |
61 ms |
11100 KB |
Output is correct |
80 |
Correct |
69 ms |
11268 KB |
Output is correct |
81 |
Correct |
68 ms |
11272 KB |
Output is correct |
82 |
Correct |
71 ms |
11280 KB |
Output is correct |
83 |
Correct |
87 ms |
11096 KB |
Output is correct |
84 |
Correct |
80 ms |
11088 KB |
Output is correct |
85 |
Correct |
71 ms |
11096 KB |
Output is correct |
86 |
Correct |
95 ms |
11260 KB |
Output is correct |
87 |
Correct |
72 ms |
11100 KB |
Output is correct |
88 |
Correct |
87 ms |
11252 KB |
Output is correct |
89 |
Correct |
90 ms |
11256 KB |
Output is correct |
90 |
Correct |
93 ms |
11256 KB |
Output is correct |
91 |
Correct |
87 ms |
11204 KB |
Output is correct |
92 |
Correct |
116 ms |
11252 KB |
Output is correct |