#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,1e9));
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,1e9));
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;
}
}
cout << l << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 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 |
309 ms |
12456 KB |
Output is correct |
8 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
472 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
15 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 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 |
348 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 |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 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 |
2704 KB |
Output is correct |
34 |
Correct |
16 ms |
2692 KB |
Output is correct |
35 |
Correct |
64 ms |
2700 KB |
Output is correct |
36 |
Correct |
14 ms |
3364 KB |
Output is correct |
37 |
Correct |
15 ms |
3372 KB |
Output is correct |
38 |
Correct |
87 ms |
3372 KB |
Output is correct |
39 |
Correct |
18 ms |
4216 KB |
Output is correct |
40 |
Correct |
19 ms |
4228 KB |
Output is correct |
41 |
Correct |
111 ms |
4252 KB |
Output is correct |
42 |
Correct |
24 ms |
5040 KB |
Output is correct |
43 |
Correct |
25 ms |
5076 KB |
Output is correct |
44 |
Correct |
148 ms |
5032 KB |
Output is correct |
45 |
Correct |
29 ms |
6004 KB |
Output is correct |
46 |
Correct |
31 ms |
6008 KB |
Output is correct |
47 |
Correct |
176 ms |
6000 KB |
Output is correct |
48 |
Correct |
35 ms |
7148 KB |
Output is correct |
49 |
Correct |
37 ms |
7028 KB |
Output is correct |
50 |
Correct |
215 ms |
6992 KB |
Output is correct |
51 |
Correct |
45 ms |
8124 KB |
Output is correct |
52 |
Correct |
43 ms |
8240 KB |
Output is correct |
53 |
Correct |
268 ms |
8216 KB |
Output is correct |
54 |
Correct |
51 ms |
9420 KB |
Output is correct |
55 |
Correct |
58 ms |
9316 KB |
Output is correct |
56 |
Correct |
313 ms |
9448 KB |
Output is correct |
57 |
Correct |
53 ms |
10684 KB |
Output is correct |
58 |
Correct |
55 ms |
10672 KB |
Output is correct |
59 |
Correct |
359 ms |
10736 KB |
Output is correct |
60 |
Correct |
71 ms |
12104 KB |
Output is correct |
61 |
Correct |
65 ms |
12104 KB |
Output is correct |
62 |
Correct |
383 ms |
12128 KB |
Output is correct |
63 |
Correct |
221 ms |
12020 KB |
Output is correct |
64 |
Correct |
403 ms |
12048 KB |
Output is correct |
65 |
Correct |
369 ms |
12108 KB |
Output is correct |
66 |
Correct |
279 ms |
12004 KB |
Output is correct |
67 |
Incorrect |
267 ms |
12052 KB |
Output isn't correct |
68 |
Correct |
135 ms |
12120 KB |
Output is correct |
69 |
Correct |
137 ms |
12172 KB |
Output is correct |
70 |
Correct |
128 ms |
12164 KB |
Output is correct |
71 |
Correct |
109 ms |
12180 KB |
Output is correct |
72 |
Correct |
109 ms |
12268 KB |
Output is correct |
73 |
Correct |
150 ms |
12876 KB |
Output is correct |
74 |
Correct |
229 ms |
12848 KB |
Output is correct |
75 |
Correct |
249 ms |
12832 KB |
Output is correct |
76 |
Correct |
241 ms |
12860 KB |
Output is correct |
77 |
Correct |
239 ms |
12932 KB |
Output is correct |
78 |
Incorrect |
264 ms |
12800 KB |
Output isn't correct |
79 |
Correct |
224 ms |
12836 KB |
Output is correct |
80 |
Correct |
244 ms |
12756 KB |
Output is correct |
81 |
Correct |
253 ms |
12756 KB |
Output is correct |
82 |
Correct |
232 ms |
12784 KB |
Output is correct |
83 |
Correct |
270 ms |
12596 KB |
Output is correct |
84 |
Correct |
270 ms |
12680 KB |
Output is correct |
85 |
Correct |
261 ms |
12652 KB |
Output is correct |
86 |
Correct |
299 ms |
12648 KB |
Output is correct |
87 |
Correct |
270 ms |
12600 KB |
Output is correct |
88 |
Correct |
284 ms |
12596 KB |
Output is correct |
89 |
Correct |
302 ms |
12568 KB |
Output is correct |
90 |
Correct |
309 ms |
12508 KB |
Output is correct |
91 |
Correct |
312 ms |
12540 KB |
Output is correct |
92 |
Correct |
308 ms |
12676 KB |
Output is correct |