#include <bits/stdc++.h>
using namespace std;
#define cin(vec) for(auto& i : vec) cin >> i
#define cout(vec) for(auto& i : vec) cout << i << " "; cout << "\n";
#define fast ios::sync_with_stdio(0);cin.tie(0);
#define loop(i,a,b) for (int i = a; i < b; i++)
#define F first
#define S second
#define pb(n) push_back(n)
#define pf(n) push_front(n)
#define dci(d) fixed<<setprecision(d)
#define sp ' '
#define el '\n'
#define all(v) v.begin(),v.end()
#define int long long
int dx[8]= {0,0,1,-1,-1,1,1,-1};
int dy[8]= {-1,1,0,0,-1,1,-1,1};
int const N=8e2+5,M=1e2+5,Mod=1e9;
char grid[N][N];
int block[N][N],n,s;
bool vis[N][N];
int valid(int i,int j){
return (i>=0&&j>=0&&i<n&&j<n);
}
struct cell{
int i;
int j;
int t;
};
queue<cell>q;
void bfs(){
while(q.size()){
int i=q.front().i;
int j=q.front().j;
int t=q.front().t;
q.pop();
if(block[i][j]<t){
continue;
}
block[i][j]=t;
for(int k=0;k<4;k++){
int x=dx[k]+i;
int y=dy[k]+j;
if(valid(x,y)&&(grid[x][y]=='G'||grid[x][y]=='M')){
q.push({x,y,t+1});
}
}
}
}
bool bfs2(int i,int j,int st){
while(q.size())q.pop();
q.push({i,j,0});
vis[i][j]=1;
int t;
while(q.size()){
i=q.front().i;
j=q.front().j;
t=q.front().t;
q.pop();
if(grid[i][j]=='D')return 1;
for(int k=0;k<4;k++){
int x=dx[k]+i;
int y=dy[k]+j;
if(valid(x,y)&&(grid[x][y]=='G'||grid[x][y]=='D'||grid[x][y]=='M')&&block[x][y]>st+(t+1)/s&&!vis[x][y]){
q.push({x,y,t+1});
vis[x][y]=1;
}
}
}
return 0;
}
void testcase(int h){
cin>>n>>s;
int xst,yst,xed,yed;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
block[i][j]=1e9;
}
}
for(int i=0;i<n;i++){
string s;cin>>s;
for(int j=0;j<n;j++){
grid[i][j]=s[j];
if(s[j]=='H')block[i][j]=0,q.push({i,j,0});
if(s[j]=='M')xst=i,yst=j;
if(s[j]=='D')xed=i,yed=j;
}
}
bfs();
int l=0,r=1e9;
int mx=-1;
while(l<=r){
int m=(l+r)/2;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
vis[i][j]=0;
int x=bfs2(xst,yst,m);
if(x){
mx=m;
l=m+1;
}
else{
r=m-1;
}
}
cout<<mx<<el;
}
int32_t main()
{
// fast
testcase(1);
// int tc;cin>>tc;for(int i=1;i<=tc;i++)testcase(i);
return 0;
}
Compilation message
mecho.cpp: In function 'void testcase(long long int)':
mecho.cpp:74:15: warning: variable 'xed' set but not used [-Wunused-but-set-variable]
74 | int xst,yst,xed,yed;
| ^~~
mecho.cpp:74:19: warning: variable 'yed' set but not used [-Wunused-but-set-variable]
74 | int xst,yst,xed,yed;
| ^~~
mecho.cpp:97:15: warning: 'yst' may be used uninitialized in this function [-Wmaybe-uninitialized]
97 | int x=bfs2(xst,yst,m);
| ~~~~^~~~~~~~~~~
mecho.cpp:97:15: warning: 'xst' may be used uninitialized in this function [-Wmaybe-uninitialized]
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Runtime error |
104 ms |
65536 KB |
Execution killed with signal 9 |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
13 |
Incorrect |
16 ms |
3796 KB |
Output isn't correct |
14 |
Correct |
454 ms |
63332 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
0 ms |
468 KB |
Output is correct |
22 |
Correct |
0 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
724 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
1 ms |
724 KB |
Output is correct |
32 |
Correct |
1 ms |
724 KB |
Output is correct |
33 |
Correct |
6 ms |
3028 KB |
Output is correct |
34 |
Correct |
6 ms |
3060 KB |
Output is correct |
35 |
Runtime error |
93 ms |
65536 KB |
Execution killed with signal 9 |
36 |
Correct |
7 ms |
3412 KB |
Output is correct |
37 |
Correct |
7 ms |
3412 KB |
Output is correct |
38 |
Runtime error |
90 ms |
65536 KB |
Execution killed with signal 9 |
39 |
Correct |
11 ms |
3796 KB |
Output is correct |
40 |
Correct |
12 ms |
3860 KB |
Output is correct |
41 |
Runtime error |
98 ms |
65536 KB |
Execution killed with signal 9 |
42 |
Correct |
12 ms |
4180 KB |
Output is correct |
43 |
Correct |
13 ms |
4180 KB |
Output is correct |
44 |
Runtime error |
93 ms |
65536 KB |
Execution killed with signal 9 |
45 |
Correct |
12 ms |
4564 KB |
Output is correct |
46 |
Correct |
14 ms |
4640 KB |
Output is correct |
47 |
Runtime error |
94 ms |
65536 KB |
Execution killed with signal 9 |
48 |
Correct |
18 ms |
4948 KB |
Output is correct |
49 |
Correct |
16 ms |
4952 KB |
Output is correct |
50 |
Runtime error |
97 ms |
65536 KB |
Execution killed with signal 9 |
51 |
Correct |
18 ms |
5336 KB |
Output is correct |
52 |
Correct |
19 ms |
5436 KB |
Output is correct |
53 |
Runtime error |
97 ms |
65536 KB |
Execution killed with signal 9 |
54 |
Correct |
23 ms |
5788 KB |
Output is correct |
55 |
Correct |
23 ms |
5792 KB |
Output is correct |
56 |
Runtime error |
117 ms |
65536 KB |
Execution killed with signal 9 |
57 |
Correct |
24 ms |
6140 KB |
Output is correct |
58 |
Correct |
23 ms |
6120 KB |
Output is correct |
59 |
Runtime error |
103 ms |
65536 KB |
Execution killed with signal 9 |
60 |
Correct |
26 ms |
6504 KB |
Output is correct |
61 |
Correct |
26 ms |
6476 KB |
Output is correct |
62 |
Runtime error |
99 ms |
65536 KB |
Execution killed with signal 9 |
63 |
Correct |
123 ms |
6620 KB |
Output is correct |
64 |
Correct |
183 ms |
6612 KB |
Output is correct |
65 |
Correct |
178 ms |
6500 KB |
Output is correct |
66 |
Correct |
134 ms |
6604 KB |
Output is correct |
67 |
Correct |
119 ms |
6512 KB |
Output is correct |
68 |
Correct |
62 ms |
6732 KB |
Output is correct |
69 |
Correct |
67 ms |
6732 KB |
Output is correct |
70 |
Correct |
46 ms |
6668 KB |
Output is correct |
71 |
Correct |
49 ms |
6672 KB |
Output is correct |
72 |
Incorrect |
47 ms |
6676 KB |
Output isn't correct |
73 |
Runtime error |
120 ms |
65536 KB |
Execution killed with signal 9 |
74 |
Runtime error |
120 ms |
65536 KB |
Execution killed with signal 9 |
75 |
Runtime error |
125 ms |
65536 KB |
Execution killed with signal 9 |
76 |
Runtime error |
138 ms |
65536 KB |
Execution killed with signal 9 |
77 |
Runtime error |
125 ms |
65536 KB |
Execution killed with signal 9 |
78 |
Runtime error |
116 ms |
65536 KB |
Execution killed with signal 9 |
79 |
Runtime error |
123 ms |
65536 KB |
Execution killed with signal 9 |
80 |
Runtime error |
126 ms |
65536 KB |
Execution killed with signal 9 |
81 |
Runtime error |
119 ms |
65536 KB |
Execution killed with signal 9 |
82 |
Runtime error |
119 ms |
65536 KB |
Execution killed with signal 9 |
83 |
Runtime error |
113 ms |
65536 KB |
Execution killed with signal 9 |
84 |
Runtime error |
115 ms |
65536 KB |
Execution killed with signal 9 |
85 |
Runtime error |
119 ms |
65536 KB |
Execution killed with signal 9 |
86 |
Runtime error |
132 ms |
65536 KB |
Execution killed with signal 9 |
87 |
Runtime error |
115 ms |
65536 KB |
Execution killed with signal 9 |
88 |
Runtime error |
109 ms |
65536 KB |
Execution killed with signal 9 |
89 |
Runtime error |
107 ms |
65536 KB |
Execution killed with signal 9 |
90 |
Runtime error |
113 ms |
65536 KB |
Execution killed with signal 9 |
91 |
Runtime error |
148 ms |
65536 KB |
Execution killed with signal 9 |
92 |
Runtime error |
110 ms |
65536 KB |
Execution killed with signal 9 |