#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;
if(arr[srcx][srcy]<=mins) return false;
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(((arr[curx][cury])/o+1) < arr[gox][goy] - mins){
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:86:7: warning: unused variable 'id' [-Wunused-variable]
86 | int id[n][n];
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
1 ms |
340 KB |
Output isn't correct |
7 |
Incorrect |
496 ms |
15556 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 |
1 ms |
596 KB |
Output isn't correct |
13 |
Incorrect |
2 ms |
468 KB |
Output isn't correct |
14 |
Correct |
3 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 |
468 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 |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
2 ms |
596 KB |
Output is correct |
27 |
Correct |
2 ms |
664 KB |
Output is correct |
28 |
Correct |
2 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
2 ms |
596 KB |
Output is correct |
31 |
Correct |
2 ms |
724 KB |
Output is correct |
32 |
Correct |
2 ms |
724 KB |
Output is correct |
33 |
Correct |
44 ms |
4316 KB |
Output is correct |
34 |
Correct |
44 ms |
4392 KB |
Output is correct |
35 |
Correct |
79 ms |
4308 KB |
Output is correct |
36 |
Correct |
46 ms |
5212 KB |
Output is correct |
37 |
Correct |
57 ms |
5292 KB |
Output is correct |
38 |
Correct |
77 ms |
5204 KB |
Output is correct |
39 |
Correct |
65 ms |
6316 KB |
Output is correct |
40 |
Correct |
73 ms |
6268 KB |
Output is correct |
41 |
Correct |
122 ms |
6296 KB |
Output is correct |
42 |
Correct |
77 ms |
7256 KB |
Output is correct |
43 |
Correct |
78 ms |
7324 KB |
Output is correct |
44 |
Correct |
178 ms |
7348 KB |
Output is correct |
45 |
Correct |
99 ms |
8396 KB |
Output is correct |
46 |
Correct |
99 ms |
8460 KB |
Output is correct |
47 |
Correct |
173 ms |
8480 KB |
Output is correct |
48 |
Correct |
118 ms |
9684 KB |
Output is correct |
49 |
Correct |
134 ms |
9676 KB |
Output is correct |
50 |
Correct |
215 ms |
9612 KB |
Output is correct |
51 |
Correct |
146 ms |
10968 KB |
Output is correct |
52 |
Correct |
176 ms |
10968 KB |
Output is correct |
53 |
Correct |
341 ms |
10984 KB |
Output is correct |
54 |
Correct |
159 ms |
12340 KB |
Output is correct |
55 |
Correct |
186 ms |
12336 KB |
Output is correct |
56 |
Correct |
334 ms |
12352 KB |
Output is correct |
57 |
Correct |
204 ms |
13780 KB |
Output is correct |
58 |
Correct |
196 ms |
13788 KB |
Output is correct |
59 |
Correct |
413 ms |
13920 KB |
Output is correct |
60 |
Correct |
238 ms |
15308 KB |
Output is correct |
61 |
Correct |
218 ms |
15316 KB |
Output is correct |
62 |
Correct |
587 ms |
15328 KB |
Output is correct |
63 |
Correct |
501 ms |
15332 KB |
Output is correct |
64 |
Correct |
626 ms |
15336 KB |
Output is correct |
65 |
Correct |
571 ms |
15436 KB |
Output is correct |
66 |
Correct |
552 ms |
15336 KB |
Output is correct |
67 |
Correct |
508 ms |
15456 KB |
Output is correct |
68 |
Correct |
597 ms |
15392 KB |
Output is correct |
69 |
Correct |
646 ms |
15388 KB |
Output is correct |
70 |
Correct |
628 ms |
15392 KB |
Output is correct |
71 |
Correct |
645 ms |
15512 KB |
Output is correct |
72 |
Incorrect |
590 ms |
15512 KB |
Output isn't correct |
73 |
Incorrect |
572 ms |
15940 KB |
Output isn't correct |
74 |
Correct |
661 ms |
15940 KB |
Output is correct |
75 |
Correct |
688 ms |
15940 KB |
Output is correct |
76 |
Correct |
558 ms |
15940 KB |
Output is correct |
77 |
Correct |
743 ms |
15932 KB |
Output is correct |
78 |
Correct |
526 ms |
16012 KB |
Output is correct |
79 |
Correct |
593 ms |
15968 KB |
Output is correct |
80 |
Correct |
525 ms |
15936 KB |
Output is correct |
81 |
Correct |
492 ms |
15848 KB |
Output is correct |
82 |
Correct |
537 ms |
15856 KB |
Output is correct |
83 |
Correct |
560 ms |
15772 KB |
Output is correct |
84 |
Correct |
605 ms |
15820 KB |
Output is correct |
85 |
Correct |
516 ms |
15808 KB |
Output is correct |
86 |
Correct |
691 ms |
15772 KB |
Output is correct |
87 |
Correct |
560 ms |
15776 KB |
Output is correct |
88 |
Correct |
552 ms |
15624 KB |
Output is correct |
89 |
Correct |
618 ms |
15656 KB |
Output is correct |
90 |
Correct |
536 ms |
15600 KB |
Output is correct |
91 |
Correct |
577 ms |
15792 KB |
Output is correct |
92 |
Correct |
577 ms |
15664 KB |
Output is correct |