#include <bits/stdc++.h>
using namespace std;
const int N = 800;
int n,s;
int mx[]={1,-1,0,0},my[]={0,0,1,-1};
vector<vector<char>> grid(N,vector<char>(N));
bool valid_sit(int x,int y){
return (x>=0&&y>=0&&x<n&&y<n&&(grid[x][y]=='G'||grid[x][y]=='M'));
}
bool mecho_reached(int m,int b){
return (m/s)<b;
}
int main(){
//ios::sync_with_stdio(false);
//cin.tie(0);
cin >> n >> s;
vector<pair<int,int>> bees;
pair<int,int> mecho,home;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin >> grid[i][j];
if(grid[i][j]=='H'){
bees.push_back({i,j});
}
if(grid[i][j]=='M'){
mecho = {i,j};
}
if(grid[i][j]=='D'){
home = {i,j};
}
}
}
int l=0,r=n*n;
while(l<=r){
vector<vector<bool>> visb(n,vector<bool>(n)),vism(n,vector<bool>(n));
vector<vector<int>> distb(n,vector<int>(n)),distm(n,vector<int>(n));
queue<pair<int,int>> q;
int eating_time=l+(r-l)/2;
for(auto i : bees){
q.push(i);
visb[i.first][i.second]=true;
}
while(!q.empty()){
int x,y;
tie(x,y)=q.front();
q.pop();
for(int i=0;i<4;i++){
int newx=x+mx[i];
int newy=y+my[i];
if(valid_sit(newx,newy)&&!visb[newx][newy]){
distb[newx][newy]=distb[x][y]+1;
q.push({newx,newy});
visb[newx][newy]=true;
}
}
}
q.push(mecho);
vism[mecho.first][mecho.second]=true;
if(distb[mecho.first][mecho.second]<=eating_time){q.pop();}
while(!q.empty()){
int x,y;
tie(x,y)=q.front();
q.pop();
for(int i=0;i<4;i++){
int newx=x+mx[i];
int newy=y+my[i];
if(valid_sit(newx,newy)&&!vism[newx][newy]&&(mecho_reached(distm[x][y]+1,distb[newx][newy]-eating_time))){
distm[newx][newy]=distm[x][y]+1;
q.push({newx,newy});
vism[newx][newy]=true;
}
}
}
bool reached=false;
for(int i=0;i<4;i++){
int newx=home.first+mx[i];
int newy=home.second+my[i];
if(valid_sit(newx,newy)&&(mecho_reached(distm[newx][newy],distb[newx][newy]-eating_time)&&vism[newx][newy])){
reached=true;
}
}
if(reached){
l=eating_time+1;
}
else{
r=eating_time-1;
}
}
cout << l-1 << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
860 KB |
Output is correct |
2 |
Correct |
1 ms |
1012 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
856 KB |
Output is correct |
6 |
Correct |
1 ms |
856 KB |
Output is correct |
7 |
Correct |
457 ms |
6524 KB |
Output is correct |
8 |
Correct |
2 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
860 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
2 ms |
1116 KB |
Output is correct |
13 |
Correct |
2 ms |
1116 KB |
Output is correct |
14 |
Correct |
2 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
860 KB |
Output is correct |
16 |
Correct |
1 ms |
860 KB |
Output is correct |
17 |
Correct |
1 ms |
860 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
2 ms |
856 KB |
Output is correct |
20 |
Correct |
1 ms |
1116 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
860 KB |
Output is correct |
23 |
Correct |
2 ms |
1116 KB |
Output is correct |
24 |
Correct |
1 ms |
1116 KB |
Output is correct |
25 |
Correct |
2 ms |
1072 KB |
Output is correct |
26 |
Correct |
3 ms |
1116 KB |
Output is correct |
27 |
Correct |
2 ms |
1116 KB |
Output is correct |
28 |
Correct |
3 ms |
1112 KB |
Output is correct |
29 |
Correct |
2 ms |
1112 KB |
Output is correct |
30 |
Correct |
2 ms |
1116 KB |
Output is correct |
31 |
Correct |
2 ms |
1116 KB |
Output is correct |
32 |
Correct |
2 ms |
1116 KB |
Output is correct |
33 |
Correct |
35 ms |
2072 KB |
Output is correct |
34 |
Correct |
33 ms |
2148 KB |
Output is correct |
35 |
Correct |
63 ms |
2140 KB |
Output is correct |
36 |
Correct |
46 ms |
2488 KB |
Output is correct |
37 |
Correct |
50 ms |
2784 KB |
Output is correct |
38 |
Correct |
80 ms |
2396 KB |
Output is correct |
39 |
Correct |
65 ms |
2824 KB |
Output is correct |
40 |
Correct |
55 ms |
2784 KB |
Output is correct |
41 |
Correct |
109 ms |
2792 KB |
Output is correct |
42 |
Correct |
79 ms |
3260 KB |
Output is correct |
43 |
Correct |
69 ms |
3196 KB |
Output is correct |
44 |
Correct |
136 ms |
3184 KB |
Output is correct |
45 |
Correct |
102 ms |
3624 KB |
Output is correct |
46 |
Correct |
82 ms |
3616 KB |
Output is correct |
47 |
Correct |
162 ms |
3420 KB |
Output is correct |
48 |
Correct |
106 ms |
4140 KB |
Output is correct |
49 |
Correct |
101 ms |
4108 KB |
Output is correct |
50 |
Correct |
198 ms |
4100 KB |
Output is correct |
51 |
Correct |
133 ms |
4592 KB |
Output is correct |
52 |
Correct |
119 ms |
4608 KB |
Output is correct |
53 |
Correct |
240 ms |
4444 KB |
Output is correct |
54 |
Correct |
186 ms |
5156 KB |
Output is correct |
55 |
Correct |
137 ms |
5172 KB |
Output is correct |
56 |
Correct |
300 ms |
5204 KB |
Output is correct |
57 |
Correct |
173 ms |
5768 KB |
Output is correct |
58 |
Correct |
162 ms |
5764 KB |
Output is correct |
59 |
Correct |
324 ms |
5716 KB |
Output is correct |
60 |
Correct |
221 ms |
6412 KB |
Output is correct |
61 |
Correct |
181 ms |
6388 KB |
Output is correct |
62 |
Correct |
378 ms |
6236 KB |
Output is correct |
63 |
Correct |
408 ms |
6224 KB |
Output is correct |
64 |
Correct |
529 ms |
6392 KB |
Output is correct |
65 |
Correct |
458 ms |
6224 KB |
Output is correct |
66 |
Correct |
426 ms |
6392 KB |
Output is correct |
67 |
Correct |
414 ms |
6484 KB |
Output is correct |
68 |
Correct |
380 ms |
6228 KB |
Output is correct |
69 |
Correct |
422 ms |
6480 KB |
Output is correct |
70 |
Correct |
374 ms |
6416 KB |
Output is correct |
71 |
Correct |
403 ms |
6480 KB |
Output is correct |
72 |
Correct |
368 ms |
6416 KB |
Output is correct |
73 |
Correct |
387 ms |
6692 KB |
Output is correct |
74 |
Correct |
442 ms |
6684 KB |
Output is correct |
75 |
Correct |
455 ms |
6596 KB |
Output is correct |
76 |
Correct |
463 ms |
6740 KB |
Output is correct |
77 |
Correct |
420 ms |
6484 KB |
Output is correct |
78 |
Correct |
434 ms |
6552 KB |
Output is correct |
79 |
Correct |
419 ms |
6656 KB |
Output is correct |
80 |
Correct |
468 ms |
6652 KB |
Output is correct |
81 |
Correct |
465 ms |
6492 KB |
Output is correct |
82 |
Correct |
469 ms |
6656 KB |
Output is correct |
83 |
Correct |
496 ms |
6608 KB |
Output is correct |
84 |
Correct |
439 ms |
6616 KB |
Output is correct |
85 |
Correct |
492 ms |
6616 KB |
Output is correct |
86 |
Correct |
489 ms |
6492 KB |
Output is correct |
87 |
Correct |
450 ms |
6612 KB |
Output is correct |
88 |
Correct |
486 ms |
6552 KB |
Output is correct |
89 |
Correct |
462 ms |
6740 KB |
Output is correct |
90 |
Correct |
452 ms |
6556 KB |
Output is correct |
91 |
Correct |
509 ms |
6556 KB |
Output is correct |
92 |
Correct |
522 ms |
6552 KB |
Output is correct |