#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};
int n,s;
char str[805][805];
int vis[805][805];
int dep[805][805];
int sx, sy;
void bee(){
queue<int> qx, qy, d;
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
if(str[i][j] == 'H'){
qx.push(i);
qy.push(j);
d.push(0);
vis[i][j] = 1;
}
}
}
while (!qx.empty()){
int xf = qx.front();
int yf = qy.front();
int df = d.front();
qx.pop();
qy.pop();
d.pop();
dep[xf][yf] = df;
for (int i=0; i<4; i++) {
int nx = xf + dx[i];
int ny = yf + dy[i];
if(nx < 0 || ny < 0 || nx >= n || ny >= n) continue;
if(vis[nx][ny]) continue;
if(str[nx][ny] != 'G' && str[nx][ny] != 'M') continue;
vis[nx][ny] = 1;
qx.push(nx);
qy.push(ny);
d.push(df+1);
}
}
}
bool trial(int x){
memset(vis,0,sizeof(vis));
queue<int> qx, qy, d;
qx.push(sx);
qy.push(sy);
d.push(x * s);
vis[sx][sy] = 1;
while (!qx.empty()) {
int xf = qx.front();
int yf = qy.front();
int df = d.front();
qx.pop();
qy.pop();
d.pop();
if(str[xf][yf] == 'D') return 1;
for (int i=0; i<4; i++) {
int nx = xf + dx[i];
int ny = yf + dy[i];
if(nx < 0 || ny < 0 || nx >= n || ny >= n) continue;
if(vis[nx][ny]) continue;
if(str[nx][ny] != 'G' && str[nx][ny] !='D') continue;
if(str[nx][ny] != 'D' && (df+1) / s >= dep[nx][ny]) continue;
vis[nx][ny] = 1;
qx.push(nx);
qy.push(ny);
d.push(df+1);
}
}
return 0;
}
int main(){
scanf("%d %d",&n,&s);
for (int i=0; i<n; i++) {
scanf("%s",str[i]);
for (int j=0; j<n; j++) {
if(str[i][j] == 'M') sx = i, sy = j;
}
}
bee();
int s = 0, e = dep[sx][sy]-1;
while (s != e) {
int m = (s+e+1)/2;
if(trial(m)) s = m;
else e = m-1;
}
if(trial(s)) printf("%d",s);
else printf("-1");
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:82:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
82 | scanf("%d %d",&n,&s);
| ~~~~~^~~~~~~~~~~~~~~
mecho.cpp:84:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
84 | scanf("%s",str[i]);
| ~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4596 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
55 ms |
6180 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
4700 KB |
Output is correct |
14 |
Correct |
1 ms |
4700 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
2 ms |
4444 KB |
Output is correct |
22 |
Correct |
3 ms |
4440 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
2 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4696 KB |
Output is correct |
26 |
Correct |
1 ms |
4524 KB |
Output is correct |
27 |
Correct |
1 ms |
4700 KB |
Output is correct |
28 |
Correct |
1 ms |
4700 KB |
Output is correct |
29 |
Correct |
1 ms |
4700 KB |
Output is correct |
30 |
Correct |
1 ms |
4528 KB |
Output is correct |
31 |
Correct |
1 ms |
4700 KB |
Output is correct |
32 |
Correct |
1 ms |
4700 KB |
Output is correct |
33 |
Correct |
3 ms |
5724 KB |
Output is correct |
34 |
Correct |
4 ms |
5724 KB |
Output is correct |
35 |
Correct |
19 ms |
5720 KB |
Output is correct |
36 |
Correct |
4 ms |
5724 KB |
Output is correct |
37 |
Correct |
4 ms |
5724 KB |
Output is correct |
38 |
Correct |
25 ms |
5720 KB |
Output is correct |
39 |
Correct |
4 ms |
5884 KB |
Output is correct |
40 |
Correct |
4 ms |
5976 KB |
Output is correct |
41 |
Correct |
24 ms |
5980 KB |
Output is correct |
42 |
Correct |
5 ms |
5980 KB |
Output is correct |
43 |
Correct |
6 ms |
5980 KB |
Output is correct |
44 |
Correct |
29 ms |
5980 KB |
Output is correct |
45 |
Correct |
6 ms |
6032 KB |
Output is correct |
46 |
Correct |
6 ms |
5980 KB |
Output is correct |
47 |
Correct |
36 ms |
5980 KB |
Output is correct |
48 |
Correct |
6 ms |
5976 KB |
Output is correct |
49 |
Correct |
6 ms |
5980 KB |
Output is correct |
50 |
Correct |
41 ms |
5980 KB |
Output is correct |
51 |
Correct |
7 ms |
6232 KB |
Output is correct |
52 |
Correct |
7 ms |
6204 KB |
Output is correct |
53 |
Correct |
53 ms |
6320 KB |
Output is correct |
54 |
Correct |
8 ms |
6232 KB |
Output is correct |
55 |
Correct |
8 ms |
6236 KB |
Output is correct |
56 |
Correct |
64 ms |
6360 KB |
Output is correct |
57 |
Correct |
8 ms |
6348 KB |
Output is correct |
58 |
Correct |
10 ms |
6236 KB |
Output is correct |
59 |
Correct |
69 ms |
6440 KB |
Output is correct |
60 |
Correct |
9 ms |
6492 KB |
Output is correct |
61 |
Correct |
10 ms |
6492 KB |
Output is correct |
62 |
Correct |
82 ms |
6320 KB |
Output is correct |
63 |
Correct |
73 ms |
6504 KB |
Output is correct |
64 |
Correct |
122 ms |
6484 KB |
Output is correct |
65 |
Correct |
120 ms |
6408 KB |
Output is correct |
66 |
Correct |
106 ms |
6492 KB |
Output is correct |
67 |
Correct |
86 ms |
6772 KB |
Output is correct |
68 |
Correct |
35 ms |
6488 KB |
Output is correct |
69 |
Correct |
30 ms |
6488 KB |
Output is correct |
70 |
Correct |
29 ms |
6548 KB |
Output is correct |
71 |
Correct |
28 ms |
6492 KB |
Output is correct |
72 |
Correct |
22 ms |
6492 KB |
Output is correct |
73 |
Correct |
24 ms |
6744 KB |
Output is correct |
74 |
Correct |
42 ms |
6792 KB |
Output is correct |
75 |
Correct |
44 ms |
7004 KB |
Output is correct |
76 |
Correct |
52 ms |
6880 KB |
Output is correct |
77 |
Correct |
43 ms |
6744 KB |
Output is correct |
78 |
Correct |
49 ms |
6748 KB |
Output is correct |
79 |
Correct |
37 ms |
6752 KB |
Output is correct |
80 |
Correct |
41 ms |
6744 KB |
Output is correct |
81 |
Correct |
51 ms |
6840 KB |
Output is correct |
82 |
Correct |
39 ms |
6748 KB |
Output is correct |
83 |
Correct |
54 ms |
6740 KB |
Output is correct |
84 |
Correct |
47 ms |
6492 KB |
Output is correct |
85 |
Correct |
49 ms |
6748 KB |
Output is correct |
86 |
Correct |
54 ms |
6736 KB |
Output is correct |
87 |
Correct |
55 ms |
6712 KB |
Output is correct |
88 |
Correct |
56 ms |
6788 KB |
Output is correct |
89 |
Correct |
54 ms |
6748 KB |
Output is correct |
90 |
Correct |
59 ms |
6740 KB |
Output is correct |
91 |
Correct |
58 ms |
6748 KB |
Output is correct |
92 |
Correct |
65 ms |
6772 KB |
Output is correct |