#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)&&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 |
1112 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 |
860 KB |
Output is correct |
6 |
Correct |
1 ms |
860 KB |
Output is correct |
7 |
Correct |
438 ms |
6500 KB |
Output is correct |
8 |
Correct |
1 ms |
1096 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 |
Incorrect |
2 ms |
1112 KB |
Output isn't correct |
13 |
Correct |
3 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 |
1 ms |
932 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
1 ms |
860 KB |
Output is correct |
23 |
Correct |
1 ms |
1116 KB |
Output is correct |
24 |
Correct |
1 ms |
1116 KB |
Output is correct |
25 |
Correct |
2 ms |
1116 KB |
Output is correct |
26 |
Correct |
2 ms |
1116 KB |
Output is correct |
27 |
Correct |
2 ms |
1116 KB |
Output is correct |
28 |
Correct |
2 ms |
1116 KB |
Output is correct |
29 |
Correct |
2 ms |
1116 KB |
Output is correct |
30 |
Correct |
2 ms |
1116 KB |
Output is correct |
31 |
Correct |
3 ms |
1116 KB |
Output is correct |
32 |
Correct |
2 ms |
1116 KB |
Output is correct |
33 |
Correct |
36 ms |
2184 KB |
Output is correct |
34 |
Correct |
32 ms |
2356 KB |
Output is correct |
35 |
Correct |
65 ms |
2140 KB |
Output is correct |
36 |
Correct |
50 ms |
2508 KB |
Output is correct |
37 |
Correct |
44 ms |
2488 KB |
Output is correct |
38 |
Correct |
81 ms |
2396 KB |
Output is correct |
39 |
Correct |
56 ms |
2812 KB |
Output is correct |
40 |
Correct |
55 ms |
2808 KB |
Output is correct |
41 |
Correct |
118 ms |
2808 KB |
Output is correct |
42 |
Correct |
74 ms |
3204 KB |
Output is correct |
43 |
Correct |
69 ms |
3232 KB |
Output is correct |
44 |
Correct |
136 ms |
3188 KB |
Output is correct |
45 |
Correct |
94 ms |
3608 KB |
Output is correct |
46 |
Correct |
82 ms |
3624 KB |
Output is correct |
47 |
Correct |
159 ms |
3608 KB |
Output is correct |
48 |
Correct |
104 ms |
4100 KB |
Output is correct |
49 |
Correct |
101 ms |
4100 KB |
Output is correct |
50 |
Correct |
188 ms |
3932 KB |
Output is correct |
51 |
Correct |
131 ms |
4628 KB |
Output is correct |
52 |
Correct |
119 ms |
4608 KB |
Output is correct |
53 |
Correct |
243 ms |
4688 KB |
Output is correct |
54 |
Correct |
151 ms |
5180 KB |
Output is correct |
55 |
Correct |
138 ms |
5144 KB |
Output is correct |
56 |
Correct |
305 ms |
5292 KB |
Output is correct |
57 |
Correct |
172 ms |
5760 KB |
Output is correct |
58 |
Correct |
158 ms |
5784 KB |
Output is correct |
59 |
Correct |
328 ms |
5972 KB |
Output is correct |
60 |
Correct |
208 ms |
6388 KB |
Output is correct |
61 |
Correct |
187 ms |
6428 KB |
Output is correct |
62 |
Correct |
361 ms |
6208 KB |
Output is correct |
63 |
Correct |
409 ms |
6480 KB |
Output is correct |
64 |
Correct |
503 ms |
6228 KB |
Output is correct |
65 |
Correct |
450 ms |
6480 KB |
Output is correct |
66 |
Correct |
425 ms |
6392 KB |
Output is correct |
67 |
Correct |
412 ms |
6460 KB |
Output is correct |
68 |
Correct |
394 ms |
6232 KB |
Output is correct |
69 |
Correct |
402 ms |
6432 KB |
Output is correct |
70 |
Correct |
377 ms |
6420 KB |
Output is correct |
71 |
Correct |
394 ms |
6416 KB |
Output is correct |
72 |
Correct |
371 ms |
6232 KB |
Output is correct |
73 |
Correct |
408 ms |
6600 KB |
Output is correct |
74 |
Correct |
425 ms |
7200 KB |
Output is correct |
75 |
Correct |
445 ms |
6688 KB |
Output is correct |
76 |
Correct |
416 ms |
6700 KB |
Output is correct |
77 |
Correct |
407 ms |
6480 KB |
Output is correct |
78 |
Correct |
429 ms |
6784 KB |
Output is correct |
79 |
Correct |
407 ms |
6624 KB |
Output is correct |
80 |
Correct |
404 ms |
6652 KB |
Output is correct |
81 |
Correct |
411 ms |
6484 KB |
Output is correct |
82 |
Correct |
410 ms |
6848 KB |
Output is correct |
83 |
Correct |
419 ms |
6612 KB |
Output is correct |
84 |
Correct |
419 ms |
6480 KB |
Output is correct |
85 |
Correct |
442 ms |
6620 KB |
Output is correct |
86 |
Correct |
417 ms |
6480 KB |
Output is correct |
87 |
Correct |
429 ms |
6872 KB |
Output is correct |
88 |
Correct |
424 ms |
6488 KB |
Output is correct |
89 |
Correct |
432 ms |
6740 KB |
Output is correct |
90 |
Correct |
435 ms |
6552 KB |
Output is correct |
91 |
Correct |
434 ms |
6492 KB |
Output is correct |
92 |
Correct |
458 ms |
6552 KB |
Output is correct |