#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 801,INF=1e12;
int n , o, srcx, srcy, destx, desty, ar[N][N];
int dx[4]={0,-1,1,0};
int dy[4]={-1,0,0,1};
struct node{
int x, y, dist;
};
bool valid1(int x, int y){
if(x>=0 && x<n && y>=0 && y<n && (x!=srcx || y!=srcy) && (x!=destx || y!=desty)) return true;
return false;
}
bool valid(int x, int y){
if(x>=0 && x<n && y>=0 && y<n) return true;
return false;
}
bool check(int mins)
{
vector<pair<int,int>>hives;
int arr[n][n];
for(int i = 0 ; i < n ; i ++)
for(int j = 0 ; j < n ; j ++)
{
arr[i][j]=ar[i][j];
if(arr[i][j]==1) hives.push_back({i,j});
}
queue<pair<int,int>>q;
int vis[n][n];
memset(vis,0,sizeof vis);
for(auto e : hives){
q.push({e.first,e.second});
vis[e.first][e.second]=1;
}
while(!q.empty())
{
int curx=q.front().first;
int cury=q.front().second;
q.pop();
for(int i = 0 ; i < 4; i ++)
{
int gox = curx+dx[i], goy = cury+dy[i];
if(valid(gox,goy) && !vis[gox][goy] && !arr[gox][goy]){
vis[gox][goy]=1;
arr[gox][goy]=arr[curx][cury]+1;
q.push({gox,goy});
}
}
}
memset(vis,0,sizeof vis);
q.push({srcx,srcy});
vis[srcx][srcy]=1;
arr[srcx][srcy]=1;
while(!q.empty()){
int curx=q.front().first;
int cury=q.front().second;
if(curx==destx&&cury==desty) return true;
q.pop();
for(int i = 0 ; i < 4; i ++)
{
int gox = curx+dx[i], goy = cury+dy[i];
if(valid(gox,goy) && !vis[gox][goy] && arr[gox][goy]!=-1){
if(mins+((arr[curx][cury])/o)+1 < arr[gox][goy]){
vis[gox][goy]=1;
arr[gox][goy]=arr[curx][cury]+1;
q.push({gox,goy});
}
}
}
}
return false;
}
int32_t main()
{
//freopen("abc.in", "r", stdin);
cin >> n >> o;
int id[n][n];
for(int i = 0 ; i < n ; i ++){
for(int j = 0 ; j < n ; j ++){
char x; cin >> x;
ar[i][j] = (x=='H'?1:((x=='G'||x=='D'||x=='M')?0:-1));
if(x=='M') srcx=i, srcy=j;
if(x=='D') destx=i, desty=j;
}
}
int l = 0 , r = n*n;
int ans=-1;
while(l <= r)
{
int mid = (l+r)/2;
if(check(mid))
{
l = mid+1;
ans=mid;
}
else
{
r = mid-1;
}
}
cout<<ans<<endl;
}
Compilation message
mecho.cpp: In function 'int32_t main()':
mecho.cpp:85:7: warning: unused variable 'id' [-Wunused-variable]
85 | int id[n][n];
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
7 |
Incorrect |
483 ms |
15668 KB |
Output isn't correct |
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 |
2 ms |
596 KB |
Output isn't correct |
13 |
Incorrect |
2 ms |
468 KB |
Output isn't correct |
14 |
Correct |
2 ms |
596 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 |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
420 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
2 ms |
540 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
2 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
3 ms |
724 KB |
Output is correct |
32 |
Correct |
2 ms |
724 KB |
Output is correct |
33 |
Correct |
34 ms |
4300 KB |
Output is correct |
34 |
Correct |
42 ms |
4360 KB |
Output is correct |
35 |
Correct |
64 ms |
4412 KB |
Output is correct |
36 |
Correct |
46 ms |
5288 KB |
Output is correct |
37 |
Correct |
46 ms |
5296 KB |
Output is correct |
38 |
Correct |
76 ms |
5300 KB |
Output is correct |
39 |
Correct |
60 ms |
6228 KB |
Output is correct |
40 |
Correct |
77 ms |
6268 KB |
Output is correct |
41 |
Correct |
121 ms |
6280 KB |
Output is correct |
42 |
Correct |
87 ms |
7248 KB |
Output is correct |
43 |
Correct |
78 ms |
7252 KB |
Output is correct |
44 |
Correct |
158 ms |
7340 KB |
Output is correct |
45 |
Correct |
113 ms |
8460 KB |
Output is correct |
46 |
Correct |
99 ms |
8460 KB |
Output is correct |
47 |
Correct |
192 ms |
8492 KB |
Output is correct |
48 |
Correct |
126 ms |
9684 KB |
Output is correct |
49 |
Correct |
123 ms |
9804 KB |
Output is correct |
50 |
Correct |
194 ms |
9668 KB |
Output is correct |
51 |
Correct |
148 ms |
10964 KB |
Output is correct |
52 |
Correct |
167 ms |
10964 KB |
Output is correct |
53 |
Correct |
255 ms |
11144 KB |
Output is correct |
54 |
Correct |
187 ms |
12444 KB |
Output is correct |
55 |
Correct |
168 ms |
12392 KB |
Output is correct |
56 |
Correct |
392 ms |
12356 KB |
Output is correct |
57 |
Correct |
206 ms |
13772 KB |
Output is correct |
58 |
Correct |
195 ms |
13780 KB |
Output is correct |
59 |
Correct |
458 ms |
13804 KB |
Output is correct |
60 |
Correct |
230 ms |
15312 KB |
Output is correct |
61 |
Correct |
226 ms |
15308 KB |
Output is correct |
62 |
Correct |
489 ms |
15332 KB |
Output is correct |
63 |
Correct |
529 ms |
15332 KB |
Output is correct |
64 |
Correct |
667 ms |
15340 KB |
Output is correct |
65 |
Correct |
578 ms |
15332 KB |
Output is correct |
66 |
Correct |
521 ms |
15444 KB |
Output is correct |
67 |
Correct |
519 ms |
15376 KB |
Output is correct |
68 |
Correct |
567 ms |
15392 KB |
Output is correct |
69 |
Correct |
544 ms |
15392 KB |
Output is correct |
70 |
Correct |
537 ms |
15392 KB |
Output is correct |
71 |
Correct |
587 ms |
15384 KB |
Output is correct |
72 |
Incorrect |
536 ms |
15384 KB |
Output isn't correct |
73 |
Incorrect |
562 ms |
15940 KB |
Output isn't correct |
74 |
Correct |
613 ms |
16204 KB |
Output is correct |
75 |
Correct |
757 ms |
15952 KB |
Output is correct |
76 |
Correct |
643 ms |
16028 KB |
Output is correct |
77 |
Correct |
547 ms |
15928 KB |
Output is correct |
78 |
Correct |
518 ms |
15848 KB |
Output is correct |
79 |
Correct |
499 ms |
15920 KB |
Output is correct |
80 |
Correct |
522 ms |
15936 KB |
Output is correct |
81 |
Correct |
537 ms |
15864 KB |
Output is correct |
82 |
Correct |
493 ms |
15856 KB |
Output is correct |
83 |
Correct |
481 ms |
15740 KB |
Output is correct |
84 |
Correct |
508 ms |
15884 KB |
Output is correct |
85 |
Correct |
554 ms |
15776 KB |
Output is correct |
86 |
Correct |
506 ms |
15784 KB |
Output is correct |
87 |
Correct |
505 ms |
15872 KB |
Output is correct |
88 |
Correct |
496 ms |
15656 KB |
Output is correct |
89 |
Correct |
465 ms |
15652 KB |
Output is correct |
90 |
Correct |
472 ms |
15784 KB |
Output is correct |
91 |
Correct |
459 ms |
15660 KB |
Output is correct |
92 |
Correct |
499 ms |
15644 KB |
Output is correct |