#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
int dx[4]{1,0,-1,0};
int dy[4]{0,1,0,-1};
char direc[4]{'R','U','L','D'};
void setIO(string filename=""){
ios_base::sync_with_stdio(0); cin.tie(0);
if(filename.size()){
freopen((filename + ".in").c_str(), "r", stdin);
freopen((filename + ".out").c_str(), "w", stdout);
}
}
const int INF=(int) 1e9;
const int M= 1e9+7;
const ll MAXD = 1e18;
//
const int MAXN = 1e4;
int main() {
setIO();
int n,s;
cin >> n >> s;
string grid[n];
for(int i=0;i<n;++i)
cin >> grid[i];
vector<pair<int,int>> hive;
int mechox,mechoy,homex,homey;
for(int i=0;i<n;++i)
for(int j=0;j<n;++j){
if(grid[i][j]=='M'){
mechox=i;
mechoy=j;
} else if(grid[i][j]=='H'){
hive.push_back({i,j});
} else if(grid[i][j]=='D'){
homex=i;
homey=j;
}
}
int l=0;
int r=n*n;
while(l<=r){
bool bee_vis[n][n]{};
bool me_vis[n][n]{};
int bee_time[n][n];
int me_time[n][n];
memset(bee_time, 0 , sizeof(bee_time));
memset(me_time, 0 , sizeof(me_time));
int time=(l+r)/2;
queue<pair<int,int>> q;
for(auto i : hive){
q.push({i.fi,i.se});
bee_vis[i.fi][i.se]=1;
}
while(!q.empty()){
int x=q.front().fi;
int y=q.front().se;
q.pop();
for(int i=0;i<4;++i){
int nx=x+dx[i],ny=y+dy[i];
if(nx<0 || nx>=n || ny<0 || ny>=n)
continue;
if(!(grid[nx][ny]=='G' || grid[nx][ny]=='M') || bee_vis[nx][ny])
continue;
bee_time[nx][ny]=bee_time[x][y]+1;
q.push({nx,ny});
bee_vis[nx][ny]=1;
}
}
q.push({mechox,mechoy});
me_vis[mechox][mechoy]=1;
if(bee_time[mechox][mechoy]<=time)
q.pop();
while(!q.empty()){
int x=q.front().fi;
int y=q.front().se;
q.pop();
for(int i=0;i<4;++i){
int nx=x+dx[i],ny=y+dy[i];
if(nx<0 || nx>=n || ny<0 || ny>=n)
continue;
if(!(grid[nx][ny]=='G' || grid[nx][ny]=='M') || me_vis[nx][ny])
continue;
if((me_time[x][y]+1)/s < bee_time[nx][ny]-time){
me_vis[nx][ny]=1;
q.push({nx,ny});
me_time[nx][ny]=me_time[x][y]+1;
}
}
}
bool ans=0;
for(int i=0;i<4;++i){
int nx=homex+dx[i],ny=homey+dy[i];
if(nx<0 || nx>=n || ny<0 || ny>=n)
continue;
if((me_time[nx][ny]/s)<bee_time[nx][ny]-time && me_vis[nx][ny]){
ans=1;
break;
}
}
if(ans)
l=time+1;
else
r=time-1;
}
cout << l-1 << "\n";
}
Compilation message
mecho.cpp: In function 'void setIO(std::string)':
mecho.cpp:15:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | freopen((filename + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:16:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | freopen((filename + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp: In function 'int main()':
mecho.cpp:94:29: warning: 'mechoy' may be used uninitialized in this function [-Wmaybe-uninitialized]
94 | if(bee_time[mechox][mechoy]<=time)
| ~~~~~~~~~~~~~~~~~~~~~~~^
mecho.cpp:94:29: warning: 'mechox' may be used uninitialized in this function [-Wmaybe-uninitialized]
mecho.cpp:118:8: warning: 'homex' may be used uninitialized in this function [-Wmaybe-uninitialized]
118 | int nx=homex+dx[i],ny=homey+dy[i];
| ^~
mecho.cpp:118:23: warning: 'homey' may be used uninitialized in this function [-Wmaybe-uninitialized]
118 | int nx=homex+dx[i],ny=homey+dy[i];
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
414 ms |
7380 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
29 ms |
1620 KB |
Output is correct |
34 |
Correct |
29 ms |
1620 KB |
Output is correct |
35 |
Correct |
60 ms |
1620 KB |
Output is correct |
36 |
Correct |
35 ms |
2064 KB |
Output is correct |
37 |
Correct |
38 ms |
2004 KB |
Output is correct |
38 |
Correct |
91 ms |
2044 KB |
Output is correct |
39 |
Correct |
44 ms |
2516 KB |
Output is correct |
40 |
Correct |
58 ms |
2516 KB |
Output is correct |
41 |
Correct |
127 ms |
2516 KB |
Output is correct |
42 |
Correct |
55 ms |
3028 KB |
Output is correct |
43 |
Correct |
61 ms |
3028 KB |
Output is correct |
44 |
Correct |
129 ms |
3028 KB |
Output is correct |
45 |
Correct |
79 ms |
3540 KB |
Output is correct |
46 |
Correct |
85 ms |
3592 KB |
Output is correct |
47 |
Correct |
152 ms |
3608 KB |
Output is correct |
48 |
Correct |
84 ms |
4224 KB |
Output is correct |
49 |
Correct |
100 ms |
4220 KB |
Output is correct |
50 |
Correct |
198 ms |
4236 KB |
Output is correct |
51 |
Correct |
100 ms |
4820 KB |
Output is correct |
52 |
Correct |
107 ms |
4820 KB |
Output is correct |
53 |
Correct |
245 ms |
4904 KB |
Output is correct |
54 |
Correct |
117 ms |
5588 KB |
Output is correct |
55 |
Correct |
138 ms |
5632 KB |
Output is correct |
56 |
Correct |
299 ms |
5636 KB |
Output is correct |
57 |
Correct |
135 ms |
6420 KB |
Output is correct |
58 |
Correct |
148 ms |
6416 KB |
Output is correct |
59 |
Correct |
346 ms |
6356 KB |
Output is correct |
60 |
Correct |
160 ms |
7256 KB |
Output is correct |
61 |
Correct |
173 ms |
7252 KB |
Output is correct |
62 |
Correct |
378 ms |
7260 KB |
Output is correct |
63 |
Correct |
422 ms |
7268 KB |
Output is correct |
64 |
Correct |
534 ms |
7264 KB |
Output is correct |
65 |
Correct |
472 ms |
7252 KB |
Output is correct |
66 |
Correct |
452 ms |
7264 KB |
Output is correct |
67 |
Correct |
437 ms |
7252 KB |
Output is correct |
68 |
Correct |
413 ms |
7300 KB |
Output is correct |
69 |
Correct |
430 ms |
7252 KB |
Output is correct |
70 |
Correct |
388 ms |
7288 KB |
Output is correct |
71 |
Correct |
412 ms |
7288 KB |
Output is correct |
72 |
Correct |
378 ms |
7288 KB |
Output is correct |
73 |
Correct |
392 ms |
7648 KB |
Output is correct |
74 |
Correct |
380 ms |
7692 KB |
Output is correct |
75 |
Correct |
434 ms |
7564 KB |
Output is correct |
76 |
Correct |
409 ms |
7560 KB |
Output is correct |
77 |
Correct |
386 ms |
7564 KB |
Output is correct |
78 |
Correct |
432 ms |
7528 KB |
Output is correct |
79 |
Correct |
396 ms |
7508 KB |
Output is correct |
80 |
Correct |
410 ms |
7528 KB |
Output is correct |
81 |
Correct |
415 ms |
7508 KB |
Output is correct |
82 |
Correct |
417 ms |
7540 KB |
Output is correct |
83 |
Correct |
414 ms |
7380 KB |
Output is correct |
84 |
Correct |
402 ms |
7500 KB |
Output is correct |
85 |
Correct |
452 ms |
7508 KB |
Output is correct |
86 |
Correct |
414 ms |
7484 KB |
Output is correct |
87 |
Correct |
421 ms |
7488 KB |
Output is correct |
88 |
Correct |
432 ms |
7380 KB |
Output is correct |
89 |
Correct |
432 ms |
7444 KB |
Output is correct |
90 |
Correct |
419 ms |
7428 KB |
Output is correct |
91 |
Correct |
452 ms |
7432 KB |
Output is correct |
92 |
Correct |
444 ms |
7428 KB |
Output is correct |