#include <bits/stdc++.h>
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(){
cin >> n >> s ;
for (int i=0; i<n; i++) {
cin >> 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)) cout << s << "\n";
else cout << "-1\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3160 KB |
Output is correct |
2 |
Correct |
1 ms |
3164 KB |
Output is correct |
3 |
Correct |
1 ms |
3164 KB |
Output is correct |
4 |
Correct |
1 ms |
3164 KB |
Output is correct |
5 |
Correct |
1 ms |
3164 KB |
Output is correct |
6 |
Correct |
1 ms |
3164 KB |
Output is correct |
7 |
Correct |
59 ms |
6328 KB |
Output is correct |
8 |
Correct |
1 ms |
3572 KB |
Output is correct |
9 |
Correct |
2 ms |
3160 KB |
Output is correct |
10 |
Correct |
1 ms |
3164 KB |
Output is correct |
11 |
Correct |
1 ms |
3164 KB |
Output is correct |
12 |
Correct |
2 ms |
5468 KB |
Output is correct |
13 |
Correct |
1 ms |
3420 KB |
Output is correct |
14 |
Correct |
2 ms |
5464 KB |
Output is correct |
15 |
Correct |
1 ms |
3164 KB |
Output is correct |
16 |
Correct |
1 ms |
3164 KB |
Output is correct |
17 |
Correct |
1 ms |
3164 KB |
Output is correct |
18 |
Correct |
1 ms |
3164 KB |
Output is correct |
19 |
Correct |
1 ms |
3164 KB |
Output is correct |
20 |
Correct |
1 ms |
3164 KB |
Output is correct |
21 |
Correct |
1 ms |
3420 KB |
Output is correct |
22 |
Correct |
1 ms |
3256 KB |
Output is correct |
23 |
Correct |
2 ms |
3420 KB |
Output is correct |
24 |
Correct |
2 ms |
3420 KB |
Output is correct |
25 |
Correct |
2 ms |
5468 KB |
Output is correct |
26 |
Correct |
3 ms |
5464 KB |
Output is correct |
27 |
Correct |
2 ms |
5468 KB |
Output is correct |
28 |
Correct |
2 ms |
5468 KB |
Output is correct |
29 |
Correct |
3 ms |
5468 KB |
Output is correct |
30 |
Correct |
2 ms |
5468 KB |
Output is correct |
31 |
Correct |
2 ms |
5568 KB |
Output is correct |
32 |
Correct |
2 ms |
5464 KB |
Output is correct |
33 |
Correct |
5 ms |
5724 KB |
Output is correct |
34 |
Correct |
5 ms |
5860 KB |
Output is correct |
35 |
Correct |
15 ms |
5724 KB |
Output is correct |
36 |
Correct |
5 ms |
5724 KB |
Output is correct |
37 |
Correct |
6 ms |
5724 KB |
Output is correct |
38 |
Correct |
20 ms |
5860 KB |
Output is correct |
39 |
Correct |
7 ms |
5920 KB |
Output is correct |
40 |
Correct |
7 ms |
5980 KB |
Output is correct |
41 |
Correct |
26 ms |
5976 KB |
Output is correct |
42 |
Correct |
7 ms |
5976 KB |
Output is correct |
43 |
Correct |
8 ms |
5980 KB |
Output is correct |
44 |
Correct |
31 ms |
6080 KB |
Output is correct |
45 |
Correct |
9 ms |
5980 KB |
Output is correct |
46 |
Correct |
9 ms |
5980 KB |
Output is correct |
47 |
Correct |
37 ms |
6164 KB |
Output is correct |
48 |
Correct |
10 ms |
5724 KB |
Output is correct |
49 |
Correct |
10 ms |
6188 KB |
Output is correct |
50 |
Correct |
50 ms |
6052 KB |
Output is correct |
51 |
Correct |
12 ms |
5980 KB |
Output is correct |
52 |
Correct |
12 ms |
6236 KB |
Output is correct |
53 |
Correct |
63 ms |
6332 KB |
Output is correct |
54 |
Correct |
13 ms |
5980 KB |
Output is correct |
55 |
Correct |
13 ms |
6352 KB |
Output is correct |
56 |
Correct |
63 ms |
6236 KB |
Output is correct |
57 |
Correct |
14 ms |
6224 KB |
Output is correct |
58 |
Correct |
15 ms |
6236 KB |
Output is correct |
59 |
Correct |
73 ms |
6312 KB |
Output is correct |
60 |
Correct |
16 ms |
6480 KB |
Output is correct |
61 |
Correct |
16 ms |
6480 KB |
Output is correct |
62 |
Correct |
83 ms |
6540 KB |
Output is correct |
63 |
Correct |
80 ms |
6292 KB |
Output is correct |
64 |
Correct |
133 ms |
6492 KB |
Output is correct |
65 |
Correct |
136 ms |
6736 KB |
Output is correct |
66 |
Correct |
112 ms |
6504 KB |
Output is correct |
67 |
Correct |
93 ms |
6536 KB |
Output is correct |
68 |
Correct |
43 ms |
6812 KB |
Output is correct |
69 |
Correct |
35 ms |
6556 KB |
Output is correct |
70 |
Correct |
35 ms |
6492 KB |
Output is correct |
71 |
Correct |
36 ms |
6608 KB |
Output is correct |
72 |
Correct |
28 ms |
6744 KB |
Output is correct |
73 |
Correct |
31 ms |
6744 KB |
Output is correct |
74 |
Correct |
47 ms |
6744 KB |
Output is correct |
75 |
Correct |
51 ms |
6744 KB |
Output is correct |
76 |
Correct |
47 ms |
6896 KB |
Output is correct |
77 |
Correct |
50 ms |
6736 KB |
Output is correct |
78 |
Correct |
75 ms |
6736 KB |
Output is correct |
79 |
Correct |
44 ms |
6736 KB |
Output is correct |
80 |
Correct |
49 ms |
6756 KB |
Output is correct |
81 |
Correct |
59 ms |
6740 KB |
Output is correct |
82 |
Correct |
46 ms |
6736 KB |
Output is correct |
83 |
Correct |
58 ms |
6740 KB |
Output is correct |
84 |
Correct |
51 ms |
6488 KB |
Output is correct |
85 |
Correct |
56 ms |
6740 KB |
Output is correct |
86 |
Correct |
61 ms |
6748 KB |
Output is correct |
87 |
Correct |
53 ms |
6740 KB |
Output is correct |
88 |
Correct |
58 ms |
6992 KB |
Output is correct |
89 |
Correct |
60 ms |
6756 KB |
Output is correct |
90 |
Correct |
65 ms |
6744 KB |
Output is correct |
91 |
Correct |
62 ms |
6740 KB |
Output is correct |
92 |
Correct |
70 ms |
6736 KB |
Output is correct |