#include <bits/stdc++.h>
using namespace std;
#define int long long
char grid[805][805];
bool vis[805][805];
int ti[805][805], dist[805][805];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int32_t main() {
int n, k;
cin >> n >> k;
pair<int, int> st, ed;
for (int i=0; i<n; i++) for (int j=0; j<n; j++) {
cin >> grid[i][j];
if (grid[i][j] == 'M') st = {i, j};
if (grid[i][j] == 'D') ed = {i, j};
}
queue<array<int, 3>> q;
for (int i=0; i<n; i++) for (int j=0; j<n; j++) if (grid[i][j] == 'H') q.push({i, j, 0});
while (!q.empty()) {
auto [x, y, d] = q.front(); q.pop();
if (0 <= x && x < n && 0 <= y && y < n && !vis[x][y] && grid[x][y] != 'T' && grid[x][y] != 'D') {
vis[x][y] = true; ti[x][y] = d;
for (int i=0; i<4; i++) q.push({x + dx[i], y + dy[i], d + 1});
}
}
int l=0, r=1e9, bst = -1;
while (l <= r) {
int m = (l + r) / 2;
for (int i=0; i<n; i++) for (int j=0; j<n; j++) vis[i][j] = dist[i][j] = 0;
q.push({st.first, st.second, m});
while (!q.empty()) {
auto [x, y, d] = q.front(); q.pop();
if (0 <= x && x < n && 0 <= y && y < n && !vis[x][y] && grid[x][y] != 'T' && (d / k < ti[x][y] || grid[x][y] == 'D')) {
vis[x][y] = true; dist[x][y] = d;
for (int i=0; i<4; i++) q.push({x + dx[i], y + dy[i], d + 1});
}
}
if (vis[ed.first][ed.second]) l = m + 1, bst = max(bst, m);
else r = m - 1;
}
cout << ((bst == -1) ? -1 : bst / k);
}
Compilation message
mecho.cpp: In function 'int32_t main()':
mecho.cpp:21:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
21 | auto [x, y, d] = q.front(); q.pop();
| ^
mecho.cpp:30:76: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
30 | for (int i=0; i<n; i++) for (int j=0; j<n; j++) vis[i][j] = dist[i][j] = 0;
| ~~~~~~~~~~~^~~
mecho.cpp:33:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
33 | auto [x, y, d] = q.front(); q.pop();
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
448 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
204 ms |
13572 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
400 KB |
Output is correct |
11 |
Correct |
0 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
1112 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
0 ms |
604 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
0 ms |
604 KB |
Output is correct |
21 |
Correct |
0 ms |
860 KB |
Output is correct |
22 |
Correct |
0 ms |
860 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
860 KB |
Output is correct |
25 |
Correct |
1 ms |
860 KB |
Output is correct |
26 |
Correct |
1 ms |
860 KB |
Output is correct |
27 |
Correct |
1 ms |
1116 KB |
Output is correct |
28 |
Correct |
1 ms |
1116 KB |
Output is correct |
29 |
Correct |
1 ms |
1116 KB |
Output is correct |
30 |
Correct |
1 ms |
1116 KB |
Output is correct |
31 |
Correct |
1 ms |
1008 KB |
Output is correct |
32 |
Correct |
1 ms |
1116 KB |
Output is correct |
33 |
Correct |
10 ms |
4956 KB |
Output is correct |
34 |
Correct |
11 ms |
5212 KB |
Output is correct |
35 |
Correct |
66 ms |
5548 KB |
Output is correct |
36 |
Correct |
13 ms |
5720 KB |
Output is correct |
37 |
Correct |
12 ms |
6144 KB |
Output is correct |
38 |
Correct |
87 ms |
6232 KB |
Output is correct |
39 |
Correct |
16 ms |
6488 KB |
Output is correct |
40 |
Correct |
17 ms |
6748 KB |
Output is correct |
41 |
Correct |
112 ms |
7000 KB |
Output is correct |
42 |
Correct |
18 ms |
7000 KB |
Output is correct |
43 |
Correct |
23 ms |
7516 KB |
Output is correct |
44 |
Correct |
143 ms |
7852 KB |
Output is correct |
45 |
Correct |
23 ms |
7760 KB |
Output is correct |
46 |
Correct |
24 ms |
8284 KB |
Output is correct |
47 |
Correct |
176 ms |
8528 KB |
Output is correct |
48 |
Correct |
26 ms |
8536 KB |
Output is correct |
49 |
Correct |
30 ms |
9036 KB |
Output is correct |
50 |
Correct |
210 ms |
9300 KB |
Output is correct |
51 |
Correct |
30 ms |
9304 KB |
Output is correct |
52 |
Correct |
29 ms |
9796 KB |
Output is correct |
53 |
Correct |
250 ms |
10068 KB |
Output is correct |
54 |
Correct |
43 ms |
9812 KB |
Output is correct |
55 |
Correct |
35 ms |
10584 KB |
Output is correct |
56 |
Correct |
299 ms |
10836 KB |
Output is correct |
57 |
Correct |
37 ms |
10708 KB |
Output is correct |
58 |
Correct |
56 ms |
11248 KB |
Output is correct |
59 |
Correct |
350 ms |
11708 KB |
Output is correct |
60 |
Correct |
48 ms |
11348 KB |
Output is correct |
61 |
Correct |
44 ms |
11860 KB |
Output is correct |
62 |
Correct |
388 ms |
12312 KB |
Output is correct |
63 |
Correct |
144 ms |
12368 KB |
Output is correct |
64 |
Correct |
334 ms |
12368 KB |
Output is correct |
65 |
Correct |
222 ms |
12240 KB |
Output is correct |
66 |
Correct |
179 ms |
12376 KB |
Output is correct |
67 |
Correct |
160 ms |
12368 KB |
Output is correct |
68 |
Correct |
99 ms |
12624 KB |
Output is correct |
69 |
Correct |
90 ms |
12628 KB |
Output is correct |
70 |
Correct |
72 ms |
12720 KB |
Output is correct |
71 |
Correct |
72 ms |
12628 KB |
Output is correct |
72 |
Correct |
66 ms |
12624 KB |
Output is correct |
73 |
Correct |
90 ms |
15192 KB |
Output is correct |
74 |
Correct |
168 ms |
14896 KB |
Output is correct |
75 |
Correct |
146 ms |
15356 KB |
Output is correct |
76 |
Correct |
148 ms |
15288 KB |
Output is correct |
77 |
Correct |
160 ms |
15848 KB |
Output is correct |
78 |
Correct |
173 ms |
15184 KB |
Output is correct |
79 |
Correct |
161 ms |
15136 KB |
Output is correct |
80 |
Correct |
158 ms |
15148 KB |
Output is correct |
81 |
Correct |
169 ms |
15188 KB |
Output is correct |
82 |
Correct |
153 ms |
15176 KB |
Output is correct |
83 |
Correct |
195 ms |
14584 KB |
Output is correct |
84 |
Correct |
238 ms |
14196 KB |
Output is correct |
85 |
Correct |
186 ms |
14744 KB |
Output is correct |
86 |
Correct |
185 ms |
14476 KB |
Output is correct |
87 |
Correct |
209 ms |
14676 KB |
Output is correct |
88 |
Correct |
198 ms |
14160 KB |
Output is correct |
89 |
Correct |
247 ms |
14000 KB |
Output is correct |
90 |
Correct |
210 ms |
14076 KB |
Output is correct |
91 |
Correct |
222 ms |
14116 KB |
Output is correct |
92 |
Correct |
193 ms |
14160 KB |
Output is correct |