#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],arr[N][N],arr2[N][N];
int dx[4]={0,-1,1,0};
int dy[4]={-1,0,0,1};
bool vis[N][N];
bool valid(int x, int y){
if(x>=0 && x<n && y>=0 && y<n) return true;
return false;
}
bool check(int mins)
{
memset(vis,0,sizeof vis);
memset(arr2,0,sizeof arr2);
queue<pair<int,int>>qq;
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;
}
void bfs(){
queue<pair<int,int>>q;
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;
}
}
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]!=-1 && (destx!=gox || desty!=goy)){
vis[gox][goy]=1;
arr[gox][goy]=arr[curx][cury]+1;
q.push({gox,goy});
}
}
}
}
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;
arr[i][j]=1e9;
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;
}
}
bfs();
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
5972 KB |
Output is correct |
2 |
Correct |
10 ms |
5992 KB |
Output is correct |
3 |
Correct |
11 ms |
5972 KB |
Output is correct |
4 |
Correct |
15 ms |
6012 KB |
Output is correct |
5 |
Correct |
14 ms |
6004 KB |
Output is correct |
6 |
Correct |
12 ms |
5980 KB |
Output is correct |
7 |
Correct |
165 ms |
16184 KB |
Output is correct |
8 |
Correct |
8 ms |
5972 KB |
Output is correct |
9 |
Correct |
9 ms |
6080 KB |
Output is correct |
10 |
Correct |
9 ms |
5972 KB |
Output is correct |
11 |
Correct |
19 ms |
6080 KB |
Output is correct |
12 |
Correct |
15 ms |
6488 KB |
Output is correct |
13 |
Correct |
14 ms |
6356 KB |
Output is correct |
14 |
Correct |
14 ms |
6484 KB |
Output is correct |
15 |
Correct |
12 ms |
6100 KB |
Output is correct |
16 |
Correct |
11 ms |
6100 KB |
Output is correct |
17 |
Correct |
11 ms |
6164 KB |
Output is correct |
18 |
Correct |
12 ms |
6100 KB |
Output is correct |
19 |
Correct |
19 ms |
6100 KB |
Output is correct |
20 |
Correct |
10 ms |
6212 KB |
Output is correct |
21 |
Correct |
9 ms |
6228 KB |
Output is correct |
22 |
Correct |
9 ms |
6228 KB |
Output is correct |
23 |
Correct |
11 ms |
6392 KB |
Output is correct |
24 |
Correct |
10 ms |
6356 KB |
Output is correct |
25 |
Correct |
10 ms |
6476 KB |
Output is correct |
26 |
Correct |
9 ms |
6488 KB |
Output is correct |
27 |
Correct |
12 ms |
6536 KB |
Output is correct |
28 |
Correct |
9 ms |
6484 KB |
Output is correct |
29 |
Correct |
9 ms |
6484 KB |
Output is correct |
30 |
Correct |
9 ms |
6484 KB |
Output is correct |
31 |
Correct |
10 ms |
6612 KB |
Output is correct |
32 |
Correct |
9 ms |
6616 KB |
Output is correct |
33 |
Correct |
20 ms |
10316 KB |
Output is correct |
34 |
Correct |
20 ms |
10452 KB |
Output is correct |
35 |
Correct |
48 ms |
10316 KB |
Output is correct |
36 |
Correct |
28 ms |
10896 KB |
Output is correct |
37 |
Correct |
26 ms |
10964 KB |
Output is correct |
38 |
Correct |
59 ms |
10964 KB |
Output is correct |
39 |
Correct |
30 ms |
11476 KB |
Output is correct |
40 |
Correct |
26 ms |
11488 KB |
Output is correct |
41 |
Correct |
62 ms |
11580 KB |
Output is correct |
42 |
Correct |
26 ms |
12116 KB |
Output is correct |
43 |
Correct |
28 ms |
12116 KB |
Output is correct |
44 |
Correct |
74 ms |
12216 KB |
Output is correct |
45 |
Correct |
40 ms |
12768 KB |
Output is correct |
46 |
Correct |
35 ms |
12748 KB |
Output is correct |
47 |
Correct |
94 ms |
12884 KB |
Output is correct |
48 |
Correct |
36 ms |
13552 KB |
Output is correct |
49 |
Correct |
36 ms |
13356 KB |
Output is correct |
50 |
Correct |
108 ms |
13476 KB |
Output is correct |
51 |
Correct |
40 ms |
14188 KB |
Output is correct |
52 |
Correct |
40 ms |
14036 KB |
Output is correct |
53 |
Correct |
131 ms |
14104 KB |
Output is correct |
54 |
Correct |
49 ms |
14668 KB |
Output is correct |
55 |
Correct |
53 ms |
14712 KB |
Output is correct |
56 |
Correct |
145 ms |
14732 KB |
Output is correct |
57 |
Correct |
49 ms |
15340 KB |
Output is correct |
58 |
Correct |
51 ms |
15328 KB |
Output is correct |
59 |
Correct |
179 ms |
15356 KB |
Output is correct |
60 |
Correct |
63 ms |
15944 KB |
Output is correct |
61 |
Correct |
59 ms |
15964 KB |
Output is correct |
62 |
Correct |
184 ms |
15952 KB |
Output is correct |
63 |
Correct |
160 ms |
15968 KB |
Output is correct |
64 |
Correct |
246 ms |
16056 KB |
Output is correct |
65 |
Correct |
270 ms |
15968 KB |
Output is correct |
66 |
Correct |
177 ms |
15892 KB |
Output is correct |
67 |
Correct |
174 ms |
16092 KB |
Output is correct |
68 |
Correct |
110 ms |
16032 KB |
Output is correct |
69 |
Correct |
119 ms |
15960 KB |
Output is correct |
70 |
Correct |
85 ms |
15948 KB |
Output is correct |
71 |
Correct |
85 ms |
16140 KB |
Output is correct |
72 |
Correct |
81 ms |
15948 KB |
Output is correct |
73 |
Correct |
118 ms |
16444 KB |
Output is correct |
74 |
Correct |
131 ms |
16424 KB |
Output is correct |
75 |
Correct |
119 ms |
16532 KB |
Output is correct |
76 |
Correct |
140 ms |
16460 KB |
Output is correct |
77 |
Correct |
146 ms |
16336 KB |
Output is correct |
78 |
Correct |
135 ms |
16432 KB |
Output is correct |
79 |
Correct |
119 ms |
16444 KB |
Output is correct |
80 |
Correct |
111 ms |
16348 KB |
Output is correct |
81 |
Correct |
141 ms |
16572 KB |
Output is correct |
82 |
Correct |
121 ms |
16460 KB |
Output is correct |
83 |
Correct |
133 ms |
16332 KB |
Output is correct |
84 |
Correct |
123 ms |
16332 KB |
Output is correct |
85 |
Correct |
123 ms |
16364 KB |
Output is correct |
86 |
Correct |
129 ms |
16268 KB |
Output is correct |
87 |
Correct |
136 ms |
16376 KB |
Output is correct |
88 |
Correct |
125 ms |
16176 KB |
Output is correct |
89 |
Correct |
134 ms |
16192 KB |
Output is correct |
90 |
Correct |
140 ms |
16204 KB |
Output is correct |
91 |
Correct |
138 ms |
16284 KB |
Output is correct |
92 |
Correct |
149 ms |
16288 KB |
Output is correct |