#include <bits/stdc++.h>
int n, s;
std::vector <std::string> g;
std::vector <std::vector <int>> bee;
bool ff(int val) {
std::vector <std::vector <int>> bear(n, std::vector <int> (n, 1e9));
int endX, endY;
std::queue <std::pair <int, int>> q;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (g[i][j] == 'D') endX = j, endY = i;
if (g[i][j] == 'M') {
bear[i][j] = 0;
q.push({ j, i });
}
}
}
if (val >= bee[q.front().second][q.front().first]) return false;
while (q.size()) {
auto now = q.front(); q.pop();
int x = now.first, y = now.second;
if (x + 1 >= 0 && y >= 0 && x + 1 < n && y < n && g[y][x + 1] != 'T' && bear[y][x + 1] == int(1e9) && ((bear[y][x] + 1) / s) + val < bee[y][x + 1]) q.push({ x + 1, y }), bear[y][x + 1] = bear[y][x] + 1;
if (x >= 0 && y + 1 >= 0 && x < n && y + 1 < n && g[y + 1][x] != 'T' && bear[y + 1][x] == int(1e9) && ((bear[y][x] + 1) / s) + val < bee[y + 1][x]) q.push({ x, y + 1 }), bear[y + 1][x] = bear[y][x] + 1;
if (x - 1 >= 0 && y >= 0 && x - 1 < n && y < n && g[y][x - 1] != 'T' && bear[y][x - 1] == int(1e9) && ((bear[y][x] + 1) / s) + val < bee[y][x - 1]) q.push({ x - 1, y }), bear[y][x - 1] = bear[y][x] + 1;
if (x >= 0 && y - 1 >= 0 && x < n && y - 1 < n && g[y - 1][x] != 'T' && bear[y - 1][x] == int(1e9) && ((bear[y][x] + 1) / s) + val < bee[y - 1][x]) q.push({ x, y - 1 }), bear[y - 1][x] = bear[y][x] + 1;
}
return bear[endY][endX] < int(1e9);
}
int main() {
std::ios::sync_with_stdio(0); std::cin.tie(0);
std::cin >> n >> s;
g.resize(n);
for (auto& x : g) std::cin >> x;
bee.resize(n, std::vector <int> (n, 1e9));
std::queue <std::pair <int, int>> q;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (g[i][j] == 'H') {
q.push({ j, i });
bee[i][j] = 0;
}
}
}
while (q.size()) {
auto now = q.front(); q.pop();
int x = now.first, y = now.second;
if (x + 1 >= 0 && y >= 0 && x + 1 < n && y < n && bee[y][x + 1] == int(1e9) && g[y][x + 1] != 'T' && g[y][x + 1] != 'D') q.push({ x + 1, y }), bee[y][x + 1] = bee[y][x] + 1;
if (x >= 0 && y + 1 >= 0 && x < n && y + 1 < n && bee[y + 1][x] == int(1e9) && g[y + 1][x] != 'T' && g[y + 1][x] != 'D') q.push({ x, y + 1 }), bee[y + 1][x] = bee[y][x] + 1;
if (x - 1 >= 0 && y >= 0 && x - 1 < n && y < n && bee[y][x - 1] == int(1e9) && g[y][x - 1] != 'T' && g[y][x - 1] != 'D') q.push({ x - 1, y }), bee[y][x - 1] = bee[y][x] + 1;
if (x >= 0 && y - 1 >= 0 && x < n && y - 1 < n && bee[y - 1][x] == int(1e9) && g[y - 1][x] != 'T' && g[y - 1][x] != 'D') q.push({ x, y - 1 }), bee[y - 1][x] = bee[y][x] + 1;
}
int l = -1, r = n * n;
while (l + 1 < r) {
int mid = (l + r) >> 1;
if (ff(mid)) l = mid;
else r = mid;
}
std::cout << l << "\n";
}
Compilation message
mecho.cpp: In function 'bool ff(int)':
mecho.cpp:29:18: warning: 'endY' may be used uninitialized in this function [-Wmaybe-uninitialized]
29 | return bear[endY][endX] < int(1e9);
| ^
mecho.cpp:29:24: warning: 'endX' may be used uninitialized in this function [-Wmaybe-uninitialized]
29 | return bear[endY][endX] < int(1e9);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 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 |
133 ms |
6180 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
1 ms |
384 KB |
Output is correct |
31 |
Correct |
1 ms |
384 KB |
Output is correct |
32 |
Correct |
1 ms |
384 KB |
Output is correct |
33 |
Correct |
9 ms |
1540 KB |
Output is correct |
34 |
Correct |
10 ms |
1560 KB |
Output is correct |
35 |
Correct |
29 ms |
1508 KB |
Output is correct |
36 |
Correct |
12 ms |
1792 KB |
Output is correct |
37 |
Correct |
13 ms |
1852 KB |
Output is correct |
38 |
Correct |
38 ms |
1852 KB |
Output is correct |
39 |
Correct |
15 ms |
2224 KB |
Output is correct |
40 |
Correct |
16 ms |
2232 KB |
Output is correct |
41 |
Correct |
52 ms |
2224 KB |
Output is correct |
42 |
Correct |
20 ms |
2712 KB |
Output is correct |
43 |
Correct |
20 ms |
2656 KB |
Output is correct |
44 |
Correct |
69 ms |
2680 KB |
Output is correct |
45 |
Correct |
24 ms |
3104 KB |
Output is correct |
46 |
Correct |
24 ms |
3120 KB |
Output is correct |
47 |
Correct |
80 ms |
3260 KB |
Output is correct |
48 |
Correct |
28 ms |
3700 KB |
Output is correct |
49 |
Correct |
30 ms |
3696 KB |
Output is correct |
50 |
Correct |
91 ms |
3724 KB |
Output is correct |
51 |
Correct |
36 ms |
4360 KB |
Output is correct |
52 |
Correct |
38 ms |
4224 KB |
Output is correct |
53 |
Correct |
121 ms |
4208 KB |
Output is correct |
54 |
Correct |
43 ms |
4832 KB |
Output is correct |
55 |
Correct |
42 ms |
4852 KB |
Output is correct |
56 |
Correct |
139 ms |
4952 KB |
Output is correct |
57 |
Correct |
47 ms |
5604 KB |
Output is correct |
58 |
Correct |
50 ms |
5476 KB |
Output is correct |
59 |
Correct |
162 ms |
5480 KB |
Output is correct |
60 |
Correct |
56 ms |
6336 KB |
Output is correct |
61 |
Correct |
56 ms |
6288 KB |
Output is correct |
62 |
Correct |
185 ms |
6160 KB |
Output is correct |
63 |
Correct |
149 ms |
6176 KB |
Output is correct |
64 |
Correct |
262 ms |
6148 KB |
Output is correct |
65 |
Correct |
200 ms |
6252 KB |
Output is correct |
66 |
Correct |
168 ms |
6160 KB |
Output is correct |
67 |
Correct |
163 ms |
6220 KB |
Output is correct |
68 |
Correct |
82 ms |
6232 KB |
Output is correct |
69 |
Correct |
83 ms |
6192 KB |
Output is correct |
70 |
Correct |
68 ms |
6184 KB |
Output is correct |
71 |
Correct |
75 ms |
6192 KB |
Output is correct |
72 |
Correct |
64 ms |
6352 KB |
Output is correct |
73 |
Correct |
66 ms |
6188 KB |
Output is correct |
74 |
Correct |
96 ms |
6184 KB |
Output is correct |
75 |
Correct |
114 ms |
6156 KB |
Output is correct |
76 |
Correct |
101 ms |
6180 KB |
Output is correct |
77 |
Correct |
99 ms |
6180 KB |
Output is correct |
78 |
Correct |
118 ms |
6252 KB |
Output is correct |
79 |
Correct |
85 ms |
6192 KB |
Output is correct |
80 |
Correct |
105 ms |
6264 KB |
Output is correct |
81 |
Correct |
111 ms |
6244 KB |
Output is correct |
82 |
Correct |
102 ms |
6172 KB |
Output is correct |
83 |
Correct |
124 ms |
6200 KB |
Output is correct |
84 |
Correct |
112 ms |
6180 KB |
Output is correct |
85 |
Correct |
123 ms |
6176 KB |
Output is correct |
86 |
Correct |
116 ms |
6184 KB |
Output is correct |
87 |
Correct |
120 ms |
6184 KB |
Output is correct |
88 |
Correct |
127 ms |
6260 KB |
Output is correct |
89 |
Correct |
126 ms |
6188 KB |
Output is correct |
90 |
Correct |
133 ms |
6168 KB |
Output is correct |
91 |
Correct |
126 ms |
6224 KB |
Output is correct |
92 |
Correct |
137 ms |
6224 KB |
Output is correct |