#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};
bool valid(int x, int y){
if(x>=0 && x<n && y>=0 && y<n) return true;
return false;
}
bool check(int mins)
{
int arr[n][n],arr2[n][n];
queue<pair<int,int>>q;
int vis[n][n];
memset(vis,0,sizeof vis);
for(int i = 0 ; i < n ; i ++)
for(int j = 0 ; j < n ; j ++)
{
if(ar[i][j]==1){
q.push({i,j});
vis[i][j]=1;
arr[i][j]=0;
}
else {
arr[i][j]=1e9;
}
}
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] && !ar[gox][goy]){
vis[gox][goy]=1;
arr[gox][goy]=arr[curx][cury]+1;
q.push({gox,goy});
}
}
}
queue<pair<int,int>>qq;
memset(vis,0,sizeof vis);
memset(arr2,0,sizeof arr2);
qq.push({srcx,srcy});
vis[srcx][srcy]=1;
if(arr[srcx][srcy]<=mins) return false;
arr2[srcx][srcy]=0;
while(!qq.empty()){
int curx=qq.front().first;
int cury=qq.front().second;
qq.pop();
for(int i = 0 ; i < 4; i ++)
{
int gox = curx+dx[i], goy = cury+dy[i];
if(valid(gox,goy) && !vis[gox][goy] && ar[gox][goy]!=-1){
if(arr[gox][goy] > floor((double)(arr2[curx][cury]+1)/(double)o)+mins){
// cout<<curx<<" "<<cury<<" "<<gox<<" "<<goy<<" "<<arr2[curx][cury]<<" "<<arr[gox][goy]-mins<<" "<<c<<" "<<arr2[curx][cury]+(c%o==0&&c>0)<<endl;
vis[gox][goy]=1;
arr2[gox][goy]=arr2[curx][cury]+1;
if(gox==destx&&goy==desty) return true;
qq.push({gox,goy});
}
}
}
}
return false;
}
int32_t main()
{
//freopen("abc.in", "r", stdin);
cin >> n >> o;
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 = 1e9;
int ans=-1;
while(l <= r)
{
int mid = l + (r - l) / 2;;
// cout<<mid<<endl;
if(check(mid))
{
l = mid+1;
ans=mid;
}
else
{
r = mid-1;
}
}
cout<<ans<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
7 |
Incorrect |
857 ms |
20548 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 |
3 ms |
596 KB |
Output isn't correct |
13 |
Correct |
3 ms |
468 KB |
Output is correct |
14 |
Correct |
4 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 |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
2 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 |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
468 KB |
Output is correct |
25 |
Correct |
3 ms |
596 KB |
Output is correct |
26 |
Correct |
2 ms |
596 KB |
Output is correct |
27 |
Correct |
2 ms |
644 KB |
Output is correct |
28 |
Correct |
2 ms |
596 KB |
Output is correct |
29 |
Correct |
3 ms |
724 KB |
Output is correct |
30 |
Correct |
3 ms |
724 KB |
Output is correct |
31 |
Correct |
3 ms |
724 KB |
Output is correct |
32 |
Correct |
3 ms |
724 KB |
Output is correct |
33 |
Correct |
70 ms |
5356 KB |
Output is correct |
34 |
Correct |
65 ms |
5352 KB |
Output is correct |
35 |
Correct |
100 ms |
5332 KB |
Output is correct |
36 |
Correct |
76 ms |
6544 KB |
Output is correct |
37 |
Correct |
83 ms |
6552 KB |
Output is correct |
38 |
Correct |
134 ms |
6548 KB |
Output is correct |
39 |
Correct |
123 ms |
7764 KB |
Output is correct |
40 |
Correct |
107 ms |
7856 KB |
Output is correct |
41 |
Correct |
208 ms |
7864 KB |
Output is correct |
42 |
Correct |
132 ms |
9292 KB |
Output is correct |
43 |
Correct |
147 ms |
9180 KB |
Output is correct |
44 |
Correct |
310 ms |
9208 KB |
Output is correct |
45 |
Correct |
169 ms |
10836 KB |
Output is correct |
46 |
Correct |
170 ms |
10840 KB |
Output is correct |
47 |
Correct |
399 ms |
10840 KB |
Output is correct |
48 |
Correct |
206 ms |
12492 KB |
Output is correct |
49 |
Correct |
223 ms |
12456 KB |
Output is correct |
50 |
Correct |
552 ms |
12508 KB |
Output is correct |
51 |
Correct |
249 ms |
14276 KB |
Output is correct |
52 |
Correct |
249 ms |
14272 KB |
Output is correct |
53 |
Correct |
671 ms |
14288 KB |
Output is correct |
54 |
Correct |
287 ms |
16176 KB |
Output is correct |
55 |
Correct |
294 ms |
16060 KB |
Output is correct |
56 |
Correct |
821 ms |
16088 KB |
Output is correct |
57 |
Correct |
347 ms |
18104 KB |
Output is correct |
58 |
Correct |
343 ms |
18188 KB |
Output is correct |
59 |
Correct |
912 ms |
18212 KB |
Output is correct |
60 |
Correct |
385 ms |
20324 KB |
Output is correct |
61 |
Correct |
393 ms |
20320 KB |
Output is correct |
62 |
Execution timed out |
1078 ms |
20340 KB |
Time limit exceeded |
63 |
Correct |
934 ms |
20324 KB |
Output is correct |
64 |
Execution timed out |
1060 ms |
20332 KB |
Time limit exceeded |
65 |
Execution timed out |
1086 ms |
20324 KB |
Time limit exceeded |
66 |
Correct |
979 ms |
20328 KB |
Output is correct |
67 |
Execution timed out |
1092 ms |
20200 KB |
Time limit exceeded |
68 |
Execution timed out |
1027 ms |
20384 KB |
Time limit exceeded |
69 |
Execution timed out |
1088 ms |
20308 KB |
Time limit exceeded |
70 |
Execution timed out |
1018 ms |
20380 KB |
Time limit exceeded |
71 |
Execution timed out |
1090 ms |
20384 KB |
Time limit exceeded |
72 |
Execution timed out |
1086 ms |
20388 KB |
Time limit exceeded |
73 |
Execution timed out |
1092 ms |
20884 KB |
Time limit exceeded |
74 |
Execution timed out |
1060 ms |
20888 KB |
Time limit exceeded |
75 |
Execution timed out |
1069 ms |
21132 KB |
Time limit exceeded |
76 |
Execution timed out |
1084 ms |
20972 KB |
Time limit exceeded |
77 |
Execution timed out |
1090 ms |
20880 KB |
Time limit exceeded |
78 |
Execution timed out |
1077 ms |
20828 KB |
Time limit exceeded |
79 |
Execution timed out |
1039 ms |
20892 KB |
Time limit exceeded |
80 |
Execution timed out |
1073 ms |
21040 KB |
Time limit exceeded |
81 |
Execution timed out |
1082 ms |
20848 KB |
Time limit exceeded |
82 |
Execution timed out |
1083 ms |
20828 KB |
Time limit exceeded |
83 |
Execution timed out |
1038 ms |
20748 KB |
Time limit exceeded |
84 |
Execution timed out |
1088 ms |
20748 KB |
Time limit exceeded |
85 |
Execution timed out |
1014 ms |
20640 KB |
Time limit exceeded |
86 |
Correct |
872 ms |
20744 KB |
Output is correct |
87 |
Correct |
890 ms |
20744 KB |
Output is correct |
88 |
Correct |
960 ms |
20652 KB |
Output is correct |
89 |
Correct |
865 ms |
20772 KB |
Output is correct |
90 |
Correct |
808 ms |
20768 KB |
Output is correct |
91 |
Correct |
883 ms |
20776 KB |
Output is correct |
92 |
Correct |
820 ms |
20624 KB |
Output is correct |