#include<bits/stdc++.h>
#define int long long
using namespace std;
const int inf=INT_MAX;
int n,s;
bool safe(int i,int j){
return i>=0 && i<n && j>=0 && j<n;
}
int di[4]={0,0,-1,1};
int dj[4]={1,-1,0,0};
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>s;
char mat[n][n];
int dist1[n][n];
int dist2[n][n];
int homex,homey;
//vector<pair<int,int>>hives;
int honx,hony;
queue<pair<int,int>>q;
bool vis1[n][n];
bool vis2[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
vis1[i][j]=false;
vis2[i][j]=false;
dist1[i][j]=inf;
dist2[i][j]=inf;
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>mat[i][j];
if(mat[i][j]=='M'){
honx=i;
hony=j;
}
if(mat[i][j]=='D'){
homex=i;
homey=j;
}
if(mat[i][j]=='H'){
q.push({i,j});
dist1[i][j]=0;
vis1[i][j]=true;
}
}
}
while(!q.empty()){
const auto &[i,j]=q.front();
q.pop();
for(int l=0;l<4;l++){
int x=i+di[l];
int y=j+dj[l];
if(safe(x,y) && !vis1[x][y] && (mat[x][y]=='G'||mat[x][y]=='M')){
dist1[x][y]=dist1[i][j]+1;
vis1[x][y]=true;
q.push({x,y});
}
}
}
bool reached=false;
int ll=0,rr=1e6;
while(ll+1<rr){
int mid=(ll+rr)/2;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
vis2[i][j]=false;
dist2[i][j]=inf;
}
}
dist2[honx][hony]=mid;
if(dist2[honx][hony]<dist1[honx][hony]){
vis2[honx][hony]=true;
q.push({honx,hony});
}
while(!q.empty()){
const auto &[i,j]=q.front();
q.pop();
for(int l=0;l<4;l++){
int x=i+di[l];
int y=j+dj[l];
if(safe(x,y) && (mat[x][y]=='G'|| mat[x][y]=='D')&& !vis2[x][y] && (((dist2[i][j]+1)/s)<dist1[x][y])){
dist2[x][y]=dist2[i][j]+1;
vis2[x][y]=true;
q.push({x,y});
}
}
}
bool reached=false;
for(int i=0;i<4;i++){
int x=homex+di[i];
int y=homey+dj[i];
if(safe(x,y) && (mat[x][y]=='G'|| mat[x][y]=='D')&& (((dist2[x][y])/s)<dist1[x][y])){
reached=true;
}
}
if(reached){
ll=mid;
}
else{
rr=mid;
}
}
cout<<ll-2<<endl;
}
Compilation message
mecho.cpp: In function 'int32_t main()':
mecho.cpp:53:21: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
53 | const auto &[i,j]=q.front();
| ^
mecho.cpp:82:21: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
82 | const auto &[i,j]=q.front();
| ^
mecho.cpp:65:10: warning: unused variable 'reached' [-Wunused-variable]
65 | bool reached=false;
| ^~~~~~~
mecho.cpp:97:13: warning: 'homex' may be used uninitialized in this function [-Wmaybe-uninitialized]
97 | int x=homex+di[i];
| ^
mecho.cpp:76:26: warning: 'honx' may be used uninitialized in this function [-Wmaybe-uninitialized]
76 | dist2[honx][hony]=mid;
| ~~~~~~~~~~~~~~~~~^~~~
mecho.cpp:98:13: warning: 'homey' may be used uninitialized in this function [-Wmaybe-uninitialized]
98 | int y=homey+dj[i];
| ^
mecho.cpp:76:26: warning: 'hony' may be used uninitialized in this function [-Wmaybe-uninitialized]
76 | dist2[honx][hony]=mid;
| ~~~~~~~~~~~~~~~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Incorrect |
0 ms |
452 KB |
Output isn't correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Incorrect |
72 ms |
12552 KB |
Output isn't correct |
8 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
9 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
10 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
13 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
14 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
15 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
16 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
17 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
18 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
19 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
20 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
21 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
22 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
23 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
24 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
25 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
27 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
29 |
Incorrect |
1 ms |
544 KB |
Output isn't correct |
30 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
31 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
32 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
33 |
Incorrect |
6 ms |
2652 KB |
Output isn't correct |
34 |
Incorrect |
17 ms |
2720 KB |
Output isn't correct |
35 |
Incorrect |
12 ms |
2652 KB |
Output isn't correct |
36 |
Incorrect |
7 ms |
3420 KB |
Output isn't correct |
37 |
Incorrect |
16 ms |
3424 KB |
Output isn't correct |
38 |
Incorrect |
20 ms |
3436 KB |
Output isn't correct |
39 |
Incorrect |
9 ms |
4184 KB |
Output isn't correct |
40 |
Incorrect |
22 ms |
4184 KB |
Output isn't correct |
41 |
Incorrect |
25 ms |
4184 KB |
Output isn't correct |
42 |
Incorrect |
11 ms |
4956 KB |
Output isn't correct |
43 |
Incorrect |
38 ms |
4952 KB |
Output isn't correct |
44 |
Incorrect |
32 ms |
5208 KB |
Output isn't correct |
45 |
Incorrect |
12 ms |
5976 KB |
Output isn't correct |
46 |
Incorrect |
35 ms |
5976 KB |
Output isn't correct |
47 |
Incorrect |
28 ms |
5976 KB |
Output isn't correct |
48 |
Incorrect |
15 ms |
7004 KB |
Output isn't correct |
49 |
Incorrect |
52 ms |
7004 KB |
Output isn't correct |
50 |
Incorrect |
48 ms |
7160 KB |
Output isn't correct |
51 |
Incorrect |
18 ms |
8284 KB |
Output isn't correct |
52 |
Incorrect |
50 ms |
8284 KB |
Output isn't correct |
53 |
Incorrect |
52 ms |
8280 KB |
Output isn't correct |
54 |
Incorrect |
21 ms |
9304 KB |
Output isn't correct |
55 |
Incorrect |
77 ms |
9308 KB |
Output isn't correct |
56 |
Incorrect |
49 ms |
9560 KB |
Output isn't correct |
57 |
Incorrect |
23 ms |
10840 KB |
Output isn't correct |
58 |
Incorrect |
75 ms |
10904 KB |
Output isn't correct |
59 |
Incorrect |
58 ms |
10844 KB |
Output isn't correct |
60 |
Incorrect |
26 ms |
12120 KB |
Output isn't correct |
61 |
Incorrect |
65 ms |
12328 KB |
Output isn't correct |
62 |
Incorrect |
80 ms |
12124 KB |
Output isn't correct |
63 |
Incorrect |
88 ms |
12332 KB |
Output isn't correct |
64 |
Incorrect |
132 ms |
12380 KB |
Output isn't correct |
65 |
Incorrect |
152 ms |
12124 KB |
Output isn't correct |
66 |
Incorrect |
132 ms |
12336 KB |
Output isn't correct |
67 |
Incorrect |
105 ms |
12124 KB |
Output isn't correct |
68 |
Incorrect |
62 ms |
12380 KB |
Output isn't correct |
69 |
Incorrect |
52 ms |
12356 KB |
Output isn't correct |
70 |
Incorrect |
44 ms |
12376 KB |
Output isn't correct |
71 |
Incorrect |
46 ms |
12380 KB |
Output isn't correct |
72 |
Incorrect |
57 ms |
12376 KB |
Output isn't correct |
73 |
Runtime error |
38 ms |
25664 KB |
Execution killed with signal 11 |
74 |
Runtime error |
36 ms |
25632 KB |
Execution killed with signal 11 |
75 |
Incorrect |
57 ms |
13084 KB |
Output isn't correct |
76 |
Incorrect |
47 ms |
12888 KB |
Output isn't correct |
77 |
Runtime error |
60 ms |
25656 KB |
Execution killed with signal 11 |
78 |
Incorrect |
72 ms |
12636 KB |
Output isn't correct |
79 |
Incorrect |
125 ms |
12636 KB |
Output isn't correct |
80 |
Incorrect |
47 ms |
12832 KB |
Output isn't correct |
81 |
Incorrect |
73 ms |
12636 KB |
Output isn't correct |
82 |
Incorrect |
68 ms |
12636 KB |
Output isn't correct |
83 |
Correct |
84 ms |
12636 KB |
Output is correct |
84 |
Incorrect |
162 ms |
12752 KB |
Output isn't correct |
85 |
Incorrect |
60 ms |
12748 KB |
Output isn't correct |
86 |
Incorrect |
80 ms |
12748 KB |
Output isn't correct |
87 |
Incorrect |
80 ms |
12632 KB |
Output isn't correct |
88 |
Incorrect |
84 ms |
12648 KB |
Output isn't correct |
89 |
Incorrect |
100 ms |
12660 KB |
Output isn't correct |
90 |
Incorrect |
79 ms |
12652 KB |
Output isn't correct |
91 |
Incorrect |
85 ms |
12648 KB |
Output isn't correct |
92 |
Incorrect |
100 ms |
12632 KB |
Output isn't correct |