#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:10: 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:18: 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:9: 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 |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1656 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
165 ms |
2976 KB |
Output is correct |
8 |
Correct |
1 ms |
1624 KB |
Output is correct |
9 |
Correct |
1 ms |
1628 KB |
Output is correct |
10 |
Correct |
1 ms |
1624 KB |
Output is correct |
11 |
Correct |
1 ms |
1628 KB |
Output is correct |
12 |
Correct |
1 ms |
1628 KB |
Output is correct |
13 |
Correct |
2 ms |
1688 KB |
Output is correct |
14 |
Correct |
3 ms |
1756 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 |
1628 KB |
Output is correct |
18 |
Correct |
1 ms |
1560 KB |
Output is correct |
19 |
Correct |
1 ms |
1696 KB |
Output is correct |
20 |
Correct |
1 ms |
1628 KB |
Output is correct |
21 |
Correct |
1 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 |
1628 KB |
Output is correct |
25 |
Correct |
1 ms |
1628 KB |
Output is correct |
26 |
Correct |
2 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 |
2 ms |
1728 KB |
Output is correct |
31 |
Correct |
2 ms |
1628 KB |
Output is correct |
32 |
Correct |
2 ms |
1624 KB |
Output is correct |
33 |
Correct |
19 ms |
1880 KB |
Output is correct |
34 |
Correct |
15 ms |
1884 KB |
Output is correct |
35 |
Correct |
20 ms |
1884 KB |
Output is correct |
36 |
Correct |
24 ms |
2140 KB |
Output is correct |
37 |
Correct |
21 ms |
2136 KB |
Output is correct |
38 |
Correct |
26 ms |
2136 KB |
Output is correct |
39 |
Correct |
32 ms |
2140 KB |
Output is correct |
40 |
Correct |
26 ms |
2260 KB |
Output is correct |
41 |
Correct |
34 ms |
2136 KB |
Output is correct |
42 |
Correct |
41 ms |
2148 KB |
Output is correct |
43 |
Correct |
33 ms |
2476 KB |
Output is correct |
44 |
Correct |
44 ms |
2148 KB |
Output is correct |
45 |
Correct |
48 ms |
2400 KB |
Output is correct |
46 |
Correct |
40 ms |
2408 KB |
Output is correct |
47 |
Correct |
51 ms |
2404 KB |
Output is correct |
48 |
Correct |
58 ms |
2396 KB |
Output is correct |
49 |
Correct |
44 ms |
2392 KB |
Output is correct |
50 |
Correct |
62 ms |
2396 KB |
Output is correct |
51 |
Correct |
74 ms |
2604 KB |
Output is correct |
52 |
Correct |
54 ms |
2392 KB |
Output is correct |
53 |
Correct |
74 ms |
2392 KB |
Output is correct |
54 |
Correct |
79 ms |
2640 KB |
Output is correct |
55 |
Correct |
65 ms |
2500 KB |
Output is correct |
56 |
Correct |
85 ms |
2724 KB |
Output is correct |
57 |
Correct |
98 ms |
2652 KB |
Output is correct |
58 |
Correct |
78 ms |
2900 KB |
Output is correct |
59 |
Correct |
112 ms |
2648 KB |
Output is correct |
60 |
Correct |
104 ms |
2936 KB |
Output is correct |
61 |
Correct |
105 ms |
2900 KB |
Output is correct |
62 |
Correct |
126 ms |
2940 KB |
Output is correct |
63 |
Correct |
187 ms |
2708 KB |
Output is correct |
64 |
Correct |
204 ms |
2904 KB |
Output is correct |
65 |
Correct |
185 ms |
2960 KB |
Output is correct |
66 |
Correct |
196 ms |
2940 KB |
Output is correct |
67 |
Correct |
212 ms |
3160 KB |
Output is correct |
68 |
Correct |
200 ms |
2968 KB |
Output is correct |
69 |
Correct |
180 ms |
2976 KB |
Output is correct |
70 |
Correct |
183 ms |
2900 KB |
Output is correct |
71 |
Correct |
219 ms |
2976 KB |
Output is correct |
72 |
Correct |
217 ms |
2904 KB |
Output is correct |
73 |
Correct |
156 ms |
3024 KB |
Output is correct |
74 |
Correct |
160 ms |
3232 KB |
Output is correct |
75 |
Correct |
181 ms |
3152 KB |
Output is correct |
76 |
Correct |
173 ms |
3244 KB |
Output is correct |
77 |
Correct |
172 ms |
3252 KB |
Output is correct |
78 |
Correct |
170 ms |
3008 KB |
Output is correct |
79 |
Correct |
163 ms |
3212 KB |
Output is correct |
80 |
Correct |
181 ms |
3360 KB |
Output is correct |
81 |
Correct |
177 ms |
3204 KB |
Output is correct |
82 |
Correct |
167 ms |
3160 KB |
Output is correct |
83 |
Correct |
168 ms |
3152 KB |
Output is correct |
84 |
Correct |
169 ms |
3164 KB |
Output is correct |
85 |
Correct |
180 ms |
3156 KB |
Output is correct |
86 |
Correct |
166 ms |
3164 KB |
Output is correct |
87 |
Correct |
164 ms |
3160 KB |
Output is correct |
88 |
Correct |
167 ms |
2904 KB |
Output is correct |
89 |
Correct |
162 ms |
2904 KB |
Output is correct |
90 |
Correct |
168 ms |
2992 KB |
Output is correct |
91 |
Correct |
194 ms |
3104 KB |
Output is correct |
92 |
Correct |
211 ms |
2900 KB |
Output is correct |