#include <bits/stdc++.h>
using namespace std;
char t[1024][1024];
char test[1024][1024];
int dist[1024][1024];
int n, s;
class state{
public:
int x,y,cw;
state(int a, int b, int c): x(a), y(b), cw(c) {}
};
void propagateBees(queue<state>& q, int limit){
while(!q.empty() && q.front().cw < limit){
state cur = q.front();
q.pop();
int x = cur.x;
int y = cur.y;
int w = cur.cw+1;
if(x > 1 && (test[x-1][y] == 'G' && dist[x-1][y] > w) || test[x-1][y] == 'M'){
dist[x-1][y] = w;
test[x-1][y] = 'H';
q.emplace(x-1, y, w);
}
if(x < n && (test[x+1][y] == 'G' && dist[x+1][y] > w) || test[x+1][y] == 'M'){
dist[x+1][y] = w;
test[x+1][y] = 'H';
q.emplace(x+1, y, w);
}
if(y > 1 && (test[x][y-1] == 'G' && dist[x][y-1] > w) || test[x][y-1] == 'M'){
dist[x][y-1] = w;
test[x][y-1] = 'H';
q.emplace(x, y-1, w);
}
if(y < n && (test[x][y+1] == 'G' && dist[x][y+1] > w) || test[x][y+1] == 'M'){
dist[x][y+1] = w;
test[x][y+1] = 'H';
q.emplace(x, y+1, w);
}
}
}
void propagateMecho(queue<state>& q, int limit){
while(!q.empty() && q.front().cw < limit){
state cur = q.front();
q.pop();
int x = cur.x;
int y = cur.y;
if(test[x][y] != 'M') continue;
int w = cur.cw+1;
if(x > 1 && (test[x-1][y] == 'G' || test[x-1][y] == 'D') && dist[x-1][y] > w){
dist[x-1][y] = w;
test[x-1][y] = 'M';
q.emplace(x-1, y, w);
}
if(x < n && (test[x+1][y] == 'G' || test[x+1][y] == 'D') && dist[x+1][y] > w){
dist[x+1][y] = w;
test[x+1][y] = 'M';
q.emplace(x+1, y, w);
}
if(y > 1 && (test[x][y-1] == 'G' || test[x][y-1] == 'D') && dist[x][y-1] > w){
dist[x][y-1] = w;
test[x][y-1] = 'M';
q.emplace(x, y-1, w);
}
if(y < n && (test[x][y+1] == 'G' || test[x][y+1] == 'D') && dist[x][y+1] > w){
dist[x][y+1] = w;
test[x][y+1] = 'M';
q.emplace(x, y+1, w);
}
}
}
bool escapePossible(int days){
if(days == -1) return true;
queue<state> q, mq;
int dx, dy;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
test[i][j] = t[i][j];
dist[i][j] = 1e9;
if(t[i][j] == 'H'){
q.emplace(i,j,0);
dist[i][j] = 0;
}else if(t[i][j] == 'D'){
dx = i;
dy = j;
}else if(t[i][j] == 'M'){
mq.emplace(i,j,0);
dist[i][j] = 0;
}
}
}
propagateBees(q, days);
int lm = 0;
while(!q.empty() || !mq.empty()){
lm += s;
propagateMecho(mq, lm);
if(test[dx][dy] == 'M') return true;
days++;
propagateBees(q, days);
}
return false;
}
int main(){
scanf("%d%d",&n,&s);
for(int i = 1; i <= n; i++){
scanf("%s", t[i]+1);
}
int lo = -1;
int hi = n*n;
int mid;
while(lo < hi){
mid = (lo+hi+1)/2;
if(escapePossible(mid)){
lo = mid;
}else{
hi = mid-1;
}
}
printf("%d\n", lo);
return 0;
}
Compilation message
mecho.cpp: In function 'void propagateBees(std::queue<state>&, int)':
mecho.cpp:19:12: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
19 | if(x > 1 && (test[x-1][y] == 'G' && dist[x-1][y] > w) || test[x-1][y] == 'M'){
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:24:12: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
24 | if(x < n && (test[x+1][y] == 'G' && dist[x+1][y] > w) || test[x+1][y] == 'M'){
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:29:12: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
29 | if(y > 1 && (test[x][y-1] == 'G' && dist[x][y-1] > w) || test[x][y-1] == 'M'){
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:34:12: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
34 | if(y < n && (test[x][y+1] == 'G' && dist[x][y+1] > w) || test[x][y+1] == 'M'){
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp: In function 'int main()':
mecho.cpp:103:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
103 | scanf("%d%d",&n,&s);
| ~~~~~^~~~~~~~~~~~~~
mecho.cpp:105:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
105 | scanf("%s", t[i]+1);
| ~~~~~^~~~~~~~~~~~~~
mecho.cpp: In function 'bool escapePossible(int)':
mecho.cpp:96:17: warning: 'dy' may be used uninitialized in this function [-Wmaybe-uninitialized]
96 | if(test[dx][dy] == 'M') return true;
| ~~~~~~~~~~~^
mecho.cpp:96:17: warning: 'dx' may be used uninitialized in this function [-Wmaybe-uninitialized]
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
332 ms |
5896 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
380 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
640 KB |
Output is correct |
13 |
Correct |
1 ms |
640 KB |
Output is correct |
14 |
Correct |
2 ms |
640 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
544 KB |
Output is correct |
18 |
Correct |
1 ms |
512 KB |
Output is correct |
19 |
Correct |
1 ms |
512 KB |
Output is correct |
20 |
Correct |
1 ms |
512 KB |
Output is correct |
21 |
Correct |
1 ms |
512 KB |
Output is correct |
22 |
Correct |
1 ms |
512 KB |
Output is correct |
23 |
Correct |
1 ms |
640 KB |
Output is correct |
24 |
Correct |
1 ms |
640 KB |
Output is correct |
25 |
Correct |
1 ms |
640 KB |
Output is correct |
26 |
Correct |
1 ms |
640 KB |
Output is correct |
27 |
Correct |
2 ms |
672 KB |
Output is correct |
28 |
Correct |
1 ms |
768 KB |
Output is correct |
29 |
Correct |
1 ms |
768 KB |
Output is correct |
30 |
Correct |
1 ms |
768 KB |
Output is correct |
31 |
Correct |
1 ms |
768 KB |
Output is correct |
32 |
Correct |
1 ms |
768 KB |
Output is correct |
33 |
Correct |
25 ms |
2680 KB |
Output is correct |
34 |
Correct |
24 ms |
2560 KB |
Output is correct |
35 |
Correct |
46 ms |
2560 KB |
Output is correct |
36 |
Correct |
31 ms |
2944 KB |
Output is correct |
37 |
Correct |
33 ms |
2944 KB |
Output is correct |
38 |
Correct |
59 ms |
2944 KB |
Output is correct |
39 |
Correct |
40 ms |
3200 KB |
Output is correct |
40 |
Correct |
41 ms |
3200 KB |
Output is correct |
41 |
Correct |
79 ms |
3200 KB |
Output is correct |
42 |
Correct |
50 ms |
3584 KB |
Output is correct |
43 |
Correct |
51 ms |
3712 KB |
Output is correct |
44 |
Correct |
100 ms |
3584 KB |
Output is correct |
45 |
Correct |
64 ms |
3968 KB |
Output is correct |
46 |
Correct |
63 ms |
3968 KB |
Output is correct |
47 |
Correct |
124 ms |
3968 KB |
Output is correct |
48 |
Correct |
73 ms |
4332 KB |
Output is correct |
49 |
Correct |
78 ms |
4344 KB |
Output is correct |
50 |
Correct |
157 ms |
4344 KB |
Output is correct |
51 |
Correct |
90 ms |
4608 KB |
Output is correct |
52 |
Correct |
89 ms |
4608 KB |
Output is correct |
53 |
Correct |
184 ms |
4608 KB |
Output is correct |
54 |
Correct |
104 ms |
4992 KB |
Output is correct |
55 |
Correct |
106 ms |
5064 KB |
Output is correct |
56 |
Correct |
220 ms |
5112 KB |
Output is correct |
57 |
Correct |
125 ms |
5528 KB |
Output is correct |
58 |
Correct |
125 ms |
5376 KB |
Output is correct |
59 |
Correct |
237 ms |
5376 KB |
Output is correct |
60 |
Correct |
140 ms |
5632 KB |
Output is correct |
61 |
Correct |
134 ms |
5632 KB |
Output is correct |
62 |
Correct |
272 ms |
5632 KB |
Output is correct |
63 |
Correct |
319 ms |
5632 KB |
Output is correct |
64 |
Correct |
307 ms |
5736 KB |
Output is correct |
65 |
Correct |
316 ms |
5632 KB |
Output is correct |
66 |
Correct |
313 ms |
5632 KB |
Output is correct |
67 |
Correct |
329 ms |
5736 KB |
Output is correct |
68 |
Correct |
312 ms |
5760 KB |
Output is correct |
69 |
Correct |
289 ms |
5760 KB |
Output is correct |
70 |
Correct |
309 ms |
5776 KB |
Output is correct |
71 |
Correct |
321 ms |
5884 KB |
Output is correct |
72 |
Correct |
302 ms |
5760 KB |
Output is correct |
73 |
Correct |
342 ms |
6152 KB |
Output is correct |
74 |
Correct |
364 ms |
6144 KB |
Output is correct |
75 |
Correct |
432 ms |
6144 KB |
Output is correct |
76 |
Correct |
368 ms |
6144 KB |
Output is correct |
77 |
Correct |
388 ms |
6308 KB |
Output is correct |
78 |
Correct |
400 ms |
6256 KB |
Output is correct |
79 |
Correct |
378 ms |
6156 KB |
Output is correct |
80 |
Correct |
349 ms |
6104 KB |
Output is correct |
81 |
Correct |
338 ms |
6228 KB |
Output is correct |
82 |
Correct |
337 ms |
6108 KB |
Output is correct |
83 |
Correct |
375 ms |
6116 KB |
Output is correct |
84 |
Correct |
342 ms |
6032 KB |
Output is correct |
85 |
Correct |
356 ms |
6180 KB |
Output is correct |
86 |
Correct |
347 ms |
6036 KB |
Output is correct |
87 |
Correct |
334 ms |
6276 KB |
Output is correct |
88 |
Correct |
341 ms |
5860 KB |
Output is correct |
89 |
Correct |
348 ms |
6000 KB |
Output is correct |
90 |
Correct |
339 ms |
5836 KB |
Output is correct |
91 |
Correct |
344 ms |
5896 KB |
Output is correct |
92 |
Correct |
353 ms |
5876 KB |
Output is correct |