#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' || test[x-1][y] == 'M') && dist[x-1][y] > w){
dist[x-1][y] = w;
test[x-1][y] = 'H';
q.emplace(x-1, y, w);
}
if(x < n && (test[x+1][y] == 'G' || test[x+1][y] == 'M') && dist[x+1][y] > w){
dist[x+1][y] = w;
test[x+1][y] = 'H';
q.emplace(x+1, y, w);
}
if(y > 1 && (test[x][y-1] == 'G' || test[x][y-1] == 'M') && dist[x][y-1] > w){
dist[x][y-1] = w;
test[x][y-1] = 'H';
q.emplace(x, y-1, w);
}
if(y < n && (test[x][y+1] == 'G' || test[x][y+1] == 'M') && dist[x][y+1] > w){
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){
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;
int oldDays = days;
while(!mq.empty() && !q.empty() && test[dx][dy] != 'M'){
lm += s;
propagateMecho(mq, lm);
days++;
propagateBees(q, days);
}
return test[dx][dy] == 'M';
}
int main(){
scanf("%d%d",&n,&s);
for(int i = 1; i <= n; i++){
scanf("%s", t[i]+1);
}
int lo = 0;
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 'bool escapePossible(int)':
mecho.cpp:92:6: warning: unused variable 'oldDays' [-Wunused-variable]
92 | int oldDays = days;
| ^~~~~~~
mecho.cpp: In function 'int main()':
mecho.cpp:102:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
102 | scanf("%d%d",&n,&s);
| ~~~~~^~~~~~~~~~~~~~
mecho.cpp:104:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
104 | scanf("%s", t[i]+1);
| ~~~~~^~~~~~~~~~~~~~
mecho.cpp: In function 'bool escapePossible(int)':
mecho.cpp:73:10: warning: 'dy' may be used uninitialized in this function [-Wmaybe-uninitialized]
73 | int dx, dy;
| ^~
mecho.cpp:73:6: warning: 'dx' may be used uninitialized in this function [-Wmaybe-uninitialized]
73 | int dx, dy;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
2 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Incorrect |
325 ms |
6080 KB |
Output isn't correct |
8 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
11 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
12 |
Correct |
1 ms |
640 KB |
Output is correct |
13 |
Incorrect |
1 ms |
640 KB |
Output isn't correct |
14 |
Incorrect |
2 ms |
640 KB |
Output isn't correct |
15 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Incorrect |
1 ms |
512 KB |
Output isn't correct |
18 |
Correct |
1 ms |
512 KB |
Output is correct |
19 |
Incorrect |
1 ms |
512 KB |
Output isn't correct |
20 |
Correct |
1 ms |
512 KB |
Output is correct |
21 |
Incorrect |
1 ms |
512 KB |
Output isn't correct |
22 |
Correct |
1 ms |
512 KB |
Output is correct |
23 |
Incorrect |
1 ms |
640 KB |
Output isn't correct |
24 |
Correct |
1 ms |
640 KB |
Output is correct |
25 |
Incorrect |
1 ms |
640 KB |
Output isn't correct |
26 |
Correct |
1 ms |
640 KB |
Output is correct |
27 |
Incorrect |
1 ms |
768 KB |
Output isn't correct |
28 |
Correct |
1 ms |
768 KB |
Output is correct |
29 |
Incorrect |
2 ms |
768 KB |
Output isn't correct |
30 |
Correct |
2 ms |
768 KB |
Output is correct |
31 |
Incorrect |
1 ms |
768 KB |
Output isn't correct |
32 |
Correct |
2 ms |
768 KB |
Output is correct |
33 |
Incorrect |
23 ms |
2560 KB |
Output isn't correct |
34 |
Correct |
23 ms |
2560 KB |
Output is correct |
35 |
Correct |
36 ms |
2560 KB |
Output is correct |
36 |
Incorrect |
31 ms |
2944 KB |
Output isn't correct |
37 |
Correct |
34 ms |
2936 KB |
Output is correct |
38 |
Correct |
47 ms |
2944 KB |
Output is correct |
39 |
Incorrect |
42 ms |
3200 KB |
Output isn't correct |
40 |
Correct |
42 ms |
3200 KB |
Output is correct |
41 |
Correct |
65 ms |
3320 KB |
Output is correct |
42 |
Incorrect |
53 ms |
3704 KB |
Output isn't correct |
43 |
Correct |
58 ms |
3704 KB |
Output is correct |
44 |
Correct |
82 ms |
3636 KB |
Output is correct |
45 |
Incorrect |
66 ms |
3968 KB |
Output isn't correct |
46 |
Correct |
70 ms |
3968 KB |
Output is correct |
47 |
Correct |
99 ms |
4088 KB |
Output is correct |
48 |
Incorrect |
87 ms |
4472 KB |
Output isn't correct |
49 |
Correct |
81 ms |
4352 KB |
Output is correct |
50 |
Correct |
125 ms |
4472 KB |
Output is correct |
51 |
Incorrect |
95 ms |
4728 KB |
Output isn't correct |
52 |
Correct |
94 ms |
4608 KB |
Output is correct |
53 |
Correct |
143 ms |
4728 KB |
Output is correct |
54 |
Incorrect |
107 ms |
4992 KB |
Output isn't correct |
55 |
Correct |
119 ms |
5112 KB |
Output is correct |
56 |
Correct |
173 ms |
5112 KB |
Output is correct |
57 |
Incorrect |
122 ms |
5376 KB |
Output isn't correct |
58 |
Correct |
127 ms |
5376 KB |
Output is correct |
59 |
Correct |
191 ms |
5376 KB |
Output is correct |
60 |
Incorrect |
145 ms |
5760 KB |
Output isn't correct |
61 |
Correct |
147 ms |
5836 KB |
Output is correct |
62 |
Correct |
247 ms |
5848 KB |
Output is correct |
63 |
Correct |
233 ms |
5880 KB |
Output is correct |
64 |
Correct |
275 ms |
5836 KB |
Output is correct |
65 |
Correct |
241 ms |
5836 KB |
Output is correct |
66 |
Incorrect |
266 ms |
5760 KB |
Output isn't correct |
67 |
Incorrect |
249 ms |
5832 KB |
Output isn't correct |
68 |
Correct |
270 ms |
5888 KB |
Output is correct |
69 |
Correct |
253 ms |
5760 KB |
Output is correct |
70 |
Correct |
277 ms |
5888 KB |
Output is correct |
71 |
Correct |
263 ms |
5888 KB |
Output is correct |
72 |
Incorrect |
310 ms |
5880 KB |
Output isn't correct |
73 |
Incorrect |
377 ms |
6308 KB |
Output isn't correct |
74 |
Correct |
358 ms |
6252 KB |
Output is correct |
75 |
Correct |
428 ms |
6400 KB |
Output is correct |
76 |
Correct |
413 ms |
6352 KB |
Output is correct |
77 |
Correct |
445 ms |
6340 KB |
Output is correct |
78 |
Incorrect |
339 ms |
6204 KB |
Output isn't correct |
79 |
Correct |
328 ms |
6328 KB |
Output is correct |
80 |
Correct |
345 ms |
6204 KB |
Output is correct |
81 |
Correct |
358 ms |
6200 KB |
Output is correct |
82 |
Correct |
352 ms |
6328 KB |
Output is correct |
83 |
Correct |
329 ms |
6144 KB |
Output is correct |
84 |
Correct |
343 ms |
6216 KB |
Output is correct |
85 |
Correct |
355 ms |
6304 KB |
Output is correct |
86 |
Correct |
346 ms |
6144 KB |
Output is correct |
87 |
Correct |
339 ms |
6224 KB |
Output is correct |
88 |
Correct |
327 ms |
6140 KB |
Output is correct |
89 |
Correct |
337 ms |
6104 KB |
Output is correct |
90 |
Correct |
331 ms |
6192 KB |
Output is correct |
91 |
Correct |
317 ms |
6072 KB |
Output is correct |
92 |
Correct |
329 ms |
6072 KB |
Output is correct |