#include <bits/stdc++.h>
#define pii pair<int, int>
#define x first
#define y second
using namespace std;
const int N = 805;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
int n, s;
int sx, sy, bee[N][N], d[N][N];
char A[N][N];
void gen_bee() {
queue<pii> Q;
for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) {
if(A[i][j] == 'M') sx = i, sy = j;
if(A[i][j] == 'H') {
bee[i][j] = 0;
Q.emplace(i, j);
}
}
while(!Q.empty()) {
pii u = Q.front(); Q.pop();
for(int i = 0; i < 4; i++) {
int nr = u.x + dx[i], nc = u.y + dy[i];
if(nr < 1 || nr > n || nc < 1 || nc > n) continue;
if(A[nr][nc] != 'G' && A[nr][nc] != 'M') continue;
if(bee[u.x][u.y] + 1 < bee[nr][nc]) {
bee[nr][nc] = bee[u.x][u.y] + 1;
Q.emplace(nr, nc);
}
}
}
}
bool chk(int mid) {
if(1ll * mid >= bee[sx][sy]) return false;
fill_n(d[0], N*N, 1e9+1);
queue<pii> Q;
d[sx][sy] = 0;
Q.emplace(sx, sy);
while(!Q.empty()) {
pii u = Q.front(); Q.pop();
if(A[u.x][u.y] == 'D') return true;
for(int i = 0; i < 4; i++) {
int nr = u.x + dx[i], nc = u.y + dy[i];
if(nr < 1 || nr > n || nc < 1 || nc > n) continue;
if(A[nr][nc] != 'M' && A[nr][nc] != 'G' && A[nr][nc] != 'D') continue;
if(d[u.x][u.y] + 1 < d[nr][nc] && 1ll * d[u.x][u.y] + 1 < 1ll * s * (bee[nr][nc] - mid)) {
d[nr][nc] = d[u.x][u.y] + 1;
Q.emplace(nr, nc);
}
}
}
return false;
}
int main() {
fill_n(bee[0], N*N, 1e9+1);
scanf("%d %d", &n, &s);
for(int i = 1; i <= n; i++) scanf(" %s", A[i] + 1);
gen_bee();
int l = 0, r = n * n;
while(l < r) {
int mid = (l + r + 1) >> 1;
if(chk(mid)) l = mid;
else r = mid-1;
}
if(chk(l)) printf("%d\n", l);
else printf("-1\n");
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:64:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &s);
~~~~~^~~~~~~~~~~~~~~~~
mecho.cpp:65:38: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i = 1; i <= n; i++) scanf(" %s", A[i] + 1);
~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5368 KB |
Output is correct |
2 |
Correct |
9 ms |
5368 KB |
Output is correct |
3 |
Correct |
8 ms |
5368 KB |
Output is correct |
4 |
Correct |
9 ms |
5496 KB |
Output is correct |
5 |
Correct |
8 ms |
5368 KB |
Output is correct |
6 |
Correct |
9 ms |
5368 KB |
Output is correct |
7 |
Correct |
100 ms |
6904 KB |
Output is correct |
8 |
Correct |
8 ms |
5368 KB |
Output is correct |
9 |
Correct |
8 ms |
5368 KB |
Output is correct |
10 |
Correct |
9 ms |
5368 KB |
Output is correct |
11 |
Correct |
8 ms |
5240 KB |
Output is correct |
12 |
Correct |
8 ms |
5372 KB |
Output is correct |
13 |
Correct |
8 ms |
5368 KB |
Output is correct |
14 |
Correct |
9 ms |
5368 KB |
Output is correct |
15 |
Correct |
9 ms |
5372 KB |
Output is correct |
16 |
Correct |
9 ms |
5368 KB |
Output is correct |
17 |
Correct |
10 ms |
5496 KB |
Output is correct |
18 |
Correct |
9 ms |
5368 KB |
Output is correct |
19 |
Correct |
10 ms |
5240 KB |
Output is correct |
20 |
Correct |
10 ms |
5368 KB |
Output is correct |
21 |
Correct |
10 ms |
5496 KB |
Output is correct |
22 |
Correct |
9 ms |
5368 KB |
Output is correct |
23 |
Correct |
10 ms |
5368 KB |
Output is correct |
24 |
Correct |
10 ms |
5368 KB |
Output is correct |
25 |
Correct |
11 ms |
5368 KB |
Output is correct |
26 |
Correct |
10 ms |
5368 KB |
Output is correct |
27 |
Correct |
9 ms |
5496 KB |
Output is correct |
28 |
Correct |
10 ms |
5496 KB |
Output is correct |
29 |
Correct |
10 ms |
5496 KB |
Output is correct |
30 |
Correct |
10 ms |
5496 KB |
Output is correct |
31 |
Correct |
10 ms |
5496 KB |
Output is correct |
32 |
Correct |
10 ms |
5496 KB |
Output is correct |
33 |
Correct |
14 ms |
5752 KB |
Output is correct |
34 |
Correct |
13 ms |
5752 KB |
Output is correct |
35 |
Correct |
32 ms |
5752 KB |
Output is correct |
36 |
Correct |
14 ms |
5880 KB |
Output is correct |
37 |
Correct |
15 ms |
5880 KB |
Output is correct |
38 |
Correct |
38 ms |
5884 KB |
Output is correct |
39 |
Correct |
15 ms |
6008 KB |
Output is correct |
40 |
Correct |
15 ms |
5880 KB |
Output is correct |
41 |
Correct |
49 ms |
5880 KB |
Output is correct |
42 |
Correct |
16 ms |
6008 KB |
Output is correct |
43 |
Correct |
17 ms |
6008 KB |
Output is correct |
44 |
Correct |
56 ms |
6008 KB |
Output is correct |
45 |
Correct |
18 ms |
6136 KB |
Output is correct |
46 |
Correct |
18 ms |
6136 KB |
Output is correct |
47 |
Correct |
63 ms |
6136 KB |
Output is correct |
48 |
Correct |
18 ms |
6180 KB |
Output is correct |
49 |
Correct |
20 ms |
6264 KB |
Output is correct |
50 |
Correct |
78 ms |
6264 KB |
Output is correct |
51 |
Correct |
20 ms |
6392 KB |
Output is correct |
52 |
Correct |
20 ms |
6264 KB |
Output is correct |
53 |
Correct |
94 ms |
6392 KB |
Output is correct |
54 |
Correct |
21 ms |
6392 KB |
Output is correct |
55 |
Correct |
22 ms |
6520 KB |
Output is correct |
56 |
Correct |
111 ms |
6392 KB |
Output is correct |
57 |
Correct |
22 ms |
6520 KB |
Output is correct |
58 |
Correct |
24 ms |
6648 KB |
Output is correct |
59 |
Correct |
121 ms |
6692 KB |
Output is correct |
60 |
Correct |
25 ms |
6648 KB |
Output is correct |
61 |
Correct |
27 ms |
6648 KB |
Output is correct |
62 |
Correct |
139 ms |
6648 KB |
Output is correct |
63 |
Correct |
119 ms |
6648 KB |
Output is correct |
64 |
Correct |
218 ms |
6648 KB |
Output is correct |
65 |
Correct |
168 ms |
6648 KB |
Output is correct |
66 |
Correct |
143 ms |
6648 KB |
Output is correct |
67 |
Correct |
132 ms |
6648 KB |
Output is correct |
68 |
Correct |
62 ms |
6776 KB |
Output is correct |
69 |
Correct |
52 ms |
6648 KB |
Output is correct |
70 |
Correct |
52 ms |
6648 KB |
Output is correct |
71 |
Correct |
48 ms |
6648 KB |
Output is correct |
72 |
Correct |
35 ms |
6648 KB |
Output is correct |
73 |
Correct |
45 ms |
6752 KB |
Output is correct |
74 |
Correct |
68 ms |
7008 KB |
Output is correct |
75 |
Correct |
86 ms |
6752 KB |
Output is correct |
76 |
Correct |
83 ms |
6752 KB |
Output is correct |
77 |
Correct |
84 ms |
6752 KB |
Output is correct |
78 |
Correct |
95 ms |
6780 KB |
Output is correct |
79 |
Correct |
60 ms |
6780 KB |
Output is correct |
80 |
Correct |
71 ms |
6780 KB |
Output is correct |
81 |
Correct |
82 ms |
6780 KB |
Output is correct |
82 |
Correct |
71 ms |
6780 KB |
Output is correct |
83 |
Correct |
92 ms |
6816 KB |
Output is correct |
84 |
Correct |
90 ms |
6816 KB |
Output is correct |
85 |
Correct |
97 ms |
6816 KB |
Output is correct |
86 |
Correct |
96 ms |
6816 KB |
Output is correct |
87 |
Correct |
94 ms |
6868 KB |
Output is correct |
88 |
Correct |
94 ms |
6740 KB |
Output is correct |
89 |
Correct |
103 ms |
6868 KB |
Output is correct |
90 |
Correct |
109 ms |
6740 KB |
Output is correct |
91 |
Correct |
103 ms |
6740 KB |
Output is correct |
92 |
Correct |
104 ms |
6868 KB |
Output is correct |