#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(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 = 1 , 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 |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
6 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
7 |
Incorrect |
482 ms |
15476 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 |
0 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 |
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 |
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 |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
2 ms |
620 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 |
38 ms |
4352 KB |
Output is correct |
34 |
Correct |
41 ms |
4308 KB |
Output is correct |
35 |
Correct |
66 ms |
4400 KB |
Output is correct |
36 |
Correct |
51 ms |
5204 KB |
Output is correct |
37 |
Correct |
49 ms |
5204 KB |
Output is correct |
38 |
Correct |
85 ms |
5304 KB |
Output is correct |
39 |
Correct |
64 ms |
6276 KB |
Output is correct |
40 |
Correct |
65 ms |
6272 KB |
Output is correct |
41 |
Correct |
149 ms |
6248 KB |
Output is correct |
42 |
Correct |
94 ms |
7256 KB |
Output is correct |
43 |
Correct |
79 ms |
7320 KB |
Output is correct |
44 |
Correct |
153 ms |
7276 KB |
Output is correct |
45 |
Correct |
115 ms |
8464 KB |
Output is correct |
46 |
Correct |
108 ms |
8464 KB |
Output is correct |
47 |
Correct |
177 ms |
8476 KB |
Output is correct |
48 |
Correct |
115 ms |
9676 KB |
Output is correct |
49 |
Correct |
132 ms |
9644 KB |
Output is correct |
50 |
Correct |
259 ms |
9696 KB |
Output is correct |
51 |
Correct |
144 ms |
10972 KB |
Output is correct |
52 |
Correct |
147 ms |
10956 KB |
Output is correct |
53 |
Correct |
274 ms |
10980 KB |
Output is correct |
54 |
Correct |
161 ms |
12236 KB |
Output is correct |
55 |
Correct |
179 ms |
12340 KB |
Output is correct |
56 |
Correct |
399 ms |
12352 KB |
Output is correct |
57 |
Correct |
183 ms |
13888 KB |
Output is correct |
58 |
Correct |
197 ms |
13668 KB |
Output is correct |
59 |
Correct |
513 ms |
13800 KB |
Output is correct |
60 |
Correct |
212 ms |
15308 KB |
Output is correct |
61 |
Correct |
253 ms |
15400 KB |
Output is correct |
62 |
Correct |
563 ms |
15320 KB |
Output is correct |
63 |
Incorrect |
537 ms |
15336 KB |
Output isn't correct |
64 |
Correct |
628 ms |
15336 KB |
Output is correct |
65 |
Correct |
580 ms |
15340 KB |
Output is correct |
66 |
Correct |
567 ms |
15332 KB |
Output is correct |
67 |
Correct |
560 ms |
15328 KB |
Output is correct |
68 |
Incorrect |
604 ms |
15384 KB |
Output isn't correct |
69 |
Correct |
561 ms |
15388 KB |
Output is correct |
70 |
Correct |
549 ms |
15384 KB |
Output is correct |
71 |
Correct |
605 ms |
15512 KB |
Output is correct |
72 |
Incorrect |
566 ms |
15348 KB |
Output isn't correct |
73 |
Incorrect |
554 ms |
16020 KB |
Output isn't correct |
74 |
Correct |
683 ms |
15948 KB |
Output is correct |
75 |
Correct |
717 ms |
16044 KB |
Output is correct |
76 |
Correct |
636 ms |
15932 KB |
Output is correct |
77 |
Correct |
793 ms |
15936 KB |
Output is correct |
78 |
Correct |
595 ms |
15856 KB |
Output is correct |
79 |
Correct |
569 ms |
15852 KB |
Output is correct |
80 |
Correct |
599 ms |
16008 KB |
Output is correct |
81 |
Correct |
563 ms |
15880 KB |
Output is correct |
82 |
Correct |
615 ms |
15852 KB |
Output is correct |
83 |
Incorrect |
564 ms |
15780 KB |
Output isn't correct |
84 |
Correct |
589 ms |
15892 KB |
Output is correct |
85 |
Correct |
553 ms |
15820 KB |
Output is correct |
86 |
Correct |
543 ms |
15848 KB |
Output is correct |
87 |
Correct |
572 ms |
15984 KB |
Output is correct |
88 |
Incorrect |
583 ms |
15708 KB |
Output isn't correct |
89 |
Correct |
527 ms |
15660 KB |
Output is correct |
90 |
Correct |
537 ms |
15720 KB |
Output is correct |
91 |
Correct |
477 ms |
15632 KB |
Output is correct |
92 |
Correct |
505 ms |
15656 KB |
Output is correct |