#include <bits/stdc++.h>
#define int long long
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
using namespace std;
vector<vector<bool>> visited;
vector<vector<int>> dist;
vector<vector<char>> grid;
vector<vector<int>> beeT;
pair<int,int> home;
pair<int,int> bear;
int N,S;
bool f(int t){
priority_queue<pair<int,pair<int,int>>> q;
visited.clear();
dist.clear();
visited.resize(N,vector<bool>(N,false));
dist.resize(N,vector<int>(N,1e14));
dist[bear.first][bear.second]=t*S;
q.push({-t*S,bear});
while(!q.empty()){
int x=q.top().second.first,y=q.top().second.second; q.pop();
if((dist[x][y])/S>=beeT[x][y]&&grid[x][y]!='D') continue;
if(visited[x][y]) continue;
visited[x][y]=true;
if(x-1>=0&&(grid[x-1][y]=='G'||grid[x-1][y]=='M'||grid[x-1][y]=='D')&&dist[x][y]+1<dist[x-1][y]){
dist[x-1][y]=dist[x][y]+1;
q.push({-dist[x-1][y], {x-1,y}});
}
if(x+1<N&&(grid[x+1][y]=='G'||grid[x+1][y]=='M'||grid[x+1][y]=='D')&&dist[x][y]+1<dist[x+1][y]){
dist[x+1][y]=dist[x][y]+1;
q.push({-dist[x+1][y], {x+1,y}});
}
if(y-1>=0&&(grid[x][y-1]=='G'||grid[x][y-1]=='M'||grid[x][y-1]=='D')&&dist[x][y]+1<dist[x][y-1]){
dist[x][y-1]=dist[x][y]+1;
q.push({-dist[x][y-1], {x,y-1}});
}
if(y+1<N&&(grid[x][y+1]=='G'||grid[x][y+1]=='M'||grid[x][y+1]=='D')&&dist[x][y]+1<dist[x][y+1]){
dist[x][y+1]=dist[x][y]+1;
q.push({-dist[x][y+1], {x,y+1}});
}
}
return visited[home.first][home.second];
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
cin >> N >> S;
grid.resize(N,vector<char>(N));
visited.resize(N,vector<bool>(N,false));
for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) cin >> grid[i][j]; }
for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++){
if(grid[i][j]=='M') bear={i,j};
else if(grid[i][j]=='D') home={i,j};
}
}
beeT.resize(N,vector<int>(N,1e14));
priority_queue<pair<int,pair<int,int>>> q;
for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) if(grid[i][j]=='H') { q.push({0,{i,j}}); beeT[i][j]=0; }}
while(!q.empty()){
int x=q.top().second.first,y=q.top().second.second; q.pop();
if(visited[x][y]) continue;
visited[x][y]=true;
if(x-1>=0&&(grid[x-1][y]=='G'||grid[x-1][y]=='M')&&beeT[x][y]+1<beeT[x-1][y]){
beeT[x-1][y]=beeT[x][y]+1;
q.push({-beeT[x-1][y], {x-1,y}});
}
if(x+1<N&&(grid[x+1][y]=='G'||grid[x+1][y]=='M')&&beeT[x][y]+1<beeT[x+1][y]){
beeT[x+1][y]=beeT[x][y]+1;
q.push({-beeT[x+1][y], {x+1,y}});
}
if(y-1>=0&&(grid[x][y-1]=='G'||grid[x][y-1]=='M')&&beeT[x][y]+1<beeT[x][y-1]){
beeT[x][y-1]=beeT[x][y]+1;
q.push({-beeT[x][y-1], {x,y-1}});
}
if(y+1<N&&(grid[x][y+1]=='G'||grid[x][y+1]=='M')&&beeT[x][y]+1<beeT[x][y+1]){
beeT[x][y+1]=beeT[x][y]+1;
q.push({-beeT[x][y+1], {x,y+1}});
}
}
int l=0,r=1e9;
while(l<r){
int mid=(l+r+1)/2;
if(f(mid)){
l=mid;
}else{
r=mid-1;
}
}
if(!f(0)) cout << -1 << "\n";
else cout << l << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
318 ms |
11780 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
416 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
388 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
12 ms |
2688 KB |
Output is correct |
34 |
Correct |
14 ms |
2576 KB |
Output is correct |
35 |
Correct |
63 ms |
2924 KB |
Output is correct |
36 |
Correct |
13 ms |
3200 KB |
Output is correct |
37 |
Correct |
15 ms |
3284 KB |
Output is correct |
38 |
Correct |
88 ms |
3312 KB |
Output is correct |
39 |
Correct |
17 ms |
4040 KB |
Output is correct |
40 |
Correct |
21 ms |
4016 KB |
Output is correct |
41 |
Correct |
112 ms |
3944 KB |
Output is correct |
42 |
Correct |
23 ms |
4804 KB |
Output is correct |
43 |
Correct |
27 ms |
4848 KB |
Output is correct |
44 |
Correct |
146 ms |
4800 KB |
Output is correct |
45 |
Correct |
26 ms |
5704 KB |
Output is correct |
46 |
Correct |
39 ms |
5696 KB |
Output is correct |
47 |
Correct |
181 ms |
5696 KB |
Output is correct |
48 |
Correct |
35 ms |
6668 KB |
Output is correct |
49 |
Correct |
41 ms |
6672 KB |
Output is correct |
50 |
Correct |
216 ms |
6696 KB |
Output is correct |
51 |
Correct |
39 ms |
7876 KB |
Output is correct |
52 |
Correct |
47 ms |
7780 KB |
Output is correct |
53 |
Correct |
258 ms |
7752 KB |
Output is correct |
54 |
Correct |
52 ms |
8920 KB |
Output is correct |
55 |
Correct |
63 ms |
8912 KB |
Output is correct |
56 |
Correct |
301 ms |
8924 KB |
Output is correct |
57 |
Correct |
61 ms |
10204 KB |
Output is correct |
58 |
Correct |
63 ms |
10164 KB |
Output is correct |
59 |
Correct |
357 ms |
10132 KB |
Output is correct |
60 |
Correct |
59 ms |
11588 KB |
Output is correct |
61 |
Correct |
74 ms |
11432 KB |
Output is correct |
62 |
Correct |
387 ms |
11440 KB |
Output is correct |
63 |
Correct |
228 ms |
11428 KB |
Output is correct |
64 |
Correct |
426 ms |
11508 KB |
Output is correct |
65 |
Correct |
382 ms |
11520 KB |
Output is correct |
66 |
Correct |
302 ms |
11548 KB |
Output is correct |
67 |
Correct |
269 ms |
11412 KB |
Output is correct |
68 |
Correct |
136 ms |
11528 KB |
Output is correct |
69 |
Correct |
152 ms |
11592 KB |
Output is correct |
70 |
Correct |
132 ms |
11516 KB |
Output is correct |
71 |
Correct |
115 ms |
11472 KB |
Output is correct |
72 |
Correct |
117 ms |
11496 KB |
Output is correct |
73 |
Correct |
172 ms |
12292 KB |
Output is correct |
74 |
Correct |
275 ms |
12236 KB |
Output is correct |
75 |
Correct |
252 ms |
12208 KB |
Output is correct |
76 |
Correct |
228 ms |
12256 KB |
Output is correct |
77 |
Correct |
227 ms |
12244 KB |
Output is correct |
78 |
Correct |
255 ms |
12152 KB |
Output is correct |
79 |
Correct |
238 ms |
12160 KB |
Output is correct |
80 |
Correct |
220 ms |
12224 KB |
Output is correct |
81 |
Correct |
235 ms |
12224 KB |
Output is correct |
82 |
Correct |
219 ms |
12252 KB |
Output is correct |
83 |
Correct |
257 ms |
12104 KB |
Output is correct |
84 |
Correct |
280 ms |
12108 KB |
Output is correct |
85 |
Correct |
261 ms |
11996 KB |
Output is correct |
86 |
Correct |
270 ms |
12096 KB |
Output is correct |
87 |
Correct |
270 ms |
12052 KB |
Output is correct |
88 |
Correct |
258 ms |
11916 KB |
Output is correct |
89 |
Correct |
309 ms |
11936 KB |
Output is correct |
90 |
Correct |
305 ms |
11912 KB |
Output is correct |
91 |
Correct |
291 ms |
12048 KB |
Output is correct |
92 |
Correct |
301 ms |
11880 KB |
Output is correct |