#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <utility>
#include <cstring>
using namespace std;
typedef pair<int,int> pii;
bool is_g[805][805];
bool pushed[805][805];
bool bocc[805][805];
int sx, sy;
int fx, fy;
int n,s;
vector<pii> hv;
queue<pii> mq;
queue<pii> bq;
int ax[4] = {0,1,0,-1};
int ay[4] = {1,0,-1,0};
// can mecho make it back to the hive waiting k minutes?
bool decision(int k){
queue<pii> mq;
queue<pii> bq;
memset(pushed, 0, sizeof(pushed));
memset(bocc, 0, sizeof(bocc));
mq.push({sx,sy});
pushed[sx][sy] = true;
for (pii cur : hv){
bq.push(cur);
bocc[cur.first][cur.second] = true;
}
for (int vi = 0; vi < k; vi++){
int sz = bq.size();
for (int i = 0 ; i < sz; i++){
pii cur = bq.front();
bq.pop();
for (int j = 0; j < 4; j++){
int cx = cur.first + ax[j];
int cy = cur.second + ay[j];
if (!bocc[cx][cy] && is_g[cx][cy]){
bocc[cx][cy] = true;
bq.push({cx,cy});
}
}
}
}
while(true){
int sz;
for (int vi = 0; vi < s; vi++){
sz = mq.size();
for (int i = 0 ; i < sz; i++){
pii cur = mq.front();
mq.pop();
if (bocc[cur.first][cur.second]) continue;
for (int j = 0; j < 4; j++){
int cx = cur.first + ax[j];
int cy = cur.second + ay[j];
if (cx == fx && cy == fy) return true;
if (!bocc[cx][cy] && is_g[cx][cy] && !pushed[cx][cy]){
pushed[cx][cy] = true;
mq.push({cx,cy});
}
}
}
}
if (sz == 0) return false;
sz = bq.size();
for (int i = 0 ; i < sz; i++){
pii cur = bq.front();
bq.pop();
for (int j = 0; j < 4; j++){
int cx = cur.first + ax[j];
int cy = cur.second + ay[j];
if (!bocc[cx][cy] && is_g[cx][cy]){
bocc[cx][cy] = true;
bq.push({cx,cy});
}
}
}
}
}
int main(){
scanf("%d %d", &n, &s);
for (int y = 1; y <= n; y++){
for (int x = 1; x <= n; x++){
char inst;
scanf(" %c", &inst);
if (inst == 'G'){
is_g[x][y] = true;
}
else if (inst == 'H'){
hv.push_back({x,y});
}
else if (inst == 'M'){
sx = x;
sy = y;
is_g[x][y] = true;
}
else if (inst == 'D'){
fx = x;
fy = y;
}
}
}
int best = -1;
int lo = 0;
int hi = n * n/2 + 1;
while (lo <= hi){
int mid = (lo + hi)/2;
if (decision(mid)){
lo = mid + 1;
best = mid;
}
else{
hi = mid - 1;
}
}
printf("%d\n", best);
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:96:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | scanf("%d %d", &n, &s);
| ~~~~~^~~~~~~~~~~~~~~~~
mecho.cpp:100:22: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
100 | scanf(" %c", &inst);
| ~~~~~^~~~~~~~~~~~~~
mecho.cpp: In function 'bool decision(int)':
mecho.cpp:77:13: warning: 'sz' may be used uninitialized in this function [-Wmaybe-uninitialized]
77 | if (sz == 0) return false;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1716 KB |
Output is correct |
2 |
Correct |
1 ms |
1624 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1672 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
180 ms |
3056 KB |
Output is correct |
8 |
Correct |
1 ms |
1624 KB |
Output is correct |
9 |
Correct |
1 ms |
1624 KB |
Output is correct |
10 |
Correct |
1 ms |
1624 KB |
Output is correct |
11 |
Correct |
1 ms |
1628 KB |
Output is correct |
12 |
Correct |
2 ms |
1624 KB |
Output is correct |
13 |
Correct |
2 ms |
1628 KB |
Output is correct |
14 |
Correct |
2 ms |
1628 KB |
Output is correct |
15 |
Correct |
1 ms |
1628 KB |
Output is correct |
16 |
Correct |
1 ms |
1628 KB |
Output is correct |
17 |
Correct |
1 ms |
1624 KB |
Output is correct |
18 |
Correct |
1 ms |
1628 KB |
Output is correct |
19 |
Correct |
2 ms |
1624 KB |
Output is correct |
20 |
Correct |
1 ms |
1628 KB |
Output is correct |
21 |
Correct |
2 ms |
1628 KB |
Output is correct |
22 |
Correct |
1 ms |
1628 KB |
Output is correct |
23 |
Correct |
1 ms |
1628 KB |
Output is correct |
24 |
Correct |
1 ms |
1712 KB |
Output is correct |
25 |
Correct |
2 ms |
1628 KB |
Output is correct |
26 |
Correct |
1 ms |
1628 KB |
Output is correct |
27 |
Correct |
2 ms |
1628 KB |
Output is correct |
28 |
Correct |
2 ms |
1628 KB |
Output is correct |
29 |
Correct |
2 ms |
1628 KB |
Output is correct |
30 |
Correct |
3 ms |
1628 KB |
Output is correct |
31 |
Correct |
1 ms |
1628 KB |
Output is correct |
32 |
Correct |
2 ms |
1628 KB |
Output is correct |
33 |
Correct |
19 ms |
1888 KB |
Output is correct |
34 |
Correct |
17 ms |
2100 KB |
Output is correct |
35 |
Correct |
37 ms |
2112 KB |
Output is correct |
36 |
Correct |
24 ms |
2140 KB |
Output is correct |
37 |
Correct |
34 ms |
2392 KB |
Output is correct |
38 |
Correct |
26 ms |
2140 KB |
Output is correct |
39 |
Correct |
44 ms |
2020 KB |
Output is correct |
40 |
Correct |
34 ms |
2136 KB |
Output is correct |
41 |
Correct |
46 ms |
2136 KB |
Output is correct |
42 |
Correct |
47 ms |
2136 KB |
Output is correct |
43 |
Correct |
53 ms |
2140 KB |
Output is correct |
44 |
Correct |
46 ms |
2136 KB |
Output is correct |
45 |
Correct |
56 ms |
2412 KB |
Output is correct |
46 |
Correct |
40 ms |
2408 KB |
Output is correct |
47 |
Correct |
85 ms |
2408 KB |
Output is correct |
48 |
Correct |
62 ms |
2396 KB |
Output is correct |
49 |
Correct |
48 ms |
2396 KB |
Output is correct |
50 |
Correct |
107 ms |
2396 KB |
Output is correct |
51 |
Correct |
97 ms |
2396 KB |
Output is correct |
52 |
Correct |
56 ms |
2396 KB |
Output is correct |
53 |
Correct |
74 ms |
2392 KB |
Output is correct |
54 |
Correct |
89 ms |
2496 KB |
Output is correct |
55 |
Correct |
108 ms |
2644 KB |
Output is correct |
56 |
Correct |
90 ms |
2652 KB |
Output is correct |
57 |
Correct |
120 ms |
2644 KB |
Output is correct |
58 |
Correct |
83 ms |
2828 KB |
Output is correct |
59 |
Correct |
109 ms |
2828 KB |
Output is correct |
60 |
Correct |
106 ms |
2844 KB |
Output is correct |
61 |
Correct |
103 ms |
2900 KB |
Output is correct |
62 |
Correct |
148 ms |
2896 KB |
Output is correct |
63 |
Correct |
239 ms |
2940 KB |
Output is correct |
64 |
Correct |
209 ms |
2900 KB |
Output is correct |
65 |
Correct |
188 ms |
2844 KB |
Output is correct |
66 |
Correct |
215 ms |
2944 KB |
Output is correct |
67 |
Correct |
220 ms |
2944 KB |
Output is correct |
68 |
Correct |
204 ms |
2972 KB |
Output is correct |
69 |
Correct |
189 ms |
2972 KB |
Output is correct |
70 |
Correct |
186 ms |
2840 KB |
Output is correct |
71 |
Correct |
215 ms |
2956 KB |
Output is correct |
72 |
Correct |
229 ms |
2980 KB |
Output is correct |
73 |
Correct |
169 ms |
3244 KB |
Output is correct |
74 |
Correct |
169 ms |
3244 KB |
Output is correct |
75 |
Correct |
179 ms |
3228 KB |
Output is correct |
76 |
Correct |
174 ms |
3248 KB |
Output is correct |
77 |
Correct |
165 ms |
3020 KB |
Output is correct |
78 |
Correct |
224 ms |
3404 KB |
Output is correct |
79 |
Correct |
181 ms |
3216 KB |
Output is correct |
80 |
Correct |
172 ms |
3160 KB |
Output is correct |
81 |
Correct |
215 ms |
2976 KB |
Output is correct |
82 |
Correct |
181 ms |
3156 KB |
Output is correct |
83 |
Correct |
226 ms |
3156 KB |
Output is correct |
84 |
Correct |
172 ms |
3160 KB |
Output is correct |
85 |
Correct |
192 ms |
3164 KB |
Output is correct |
86 |
Correct |
173 ms |
3164 KB |
Output is correct |
87 |
Correct |
208 ms |
3164 KB |
Output is correct |
88 |
Correct |
182 ms |
2904 KB |
Output is correct |
89 |
Correct |
172 ms |
2896 KB |
Output is correct |
90 |
Correct |
194 ms |
2908 KB |
Output is correct |
91 |
Correct |
183 ms |
3112 KB |
Output is correct |
92 |
Correct |
198 ms |
2900 KB |
Output is correct |