#include <bits/stdc++.h>
#define se second
#define fi first
using namespace std;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N, S; cin >> N >> S;
vector<vector<char>> grid(N, vector<char>(N));
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) cin >> grid[i][j];
}
int start, end;
vector<int> dist(N * N, INT_MAX);
vector<vector<int>> adj(N * N);
queue<pair<int,int>> hives;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (grid[i][j] == 'H') {
hives.push({i * N + j, 1});
dist[i * N + j] = 0;
}
else if (grid[i][j] == 'M') start = i * N + j;
else if (grid[i][j] == 'D') end = i * N + j;
if (grid[i][j] == 'H' || grid[i][j] == 'T') continue;
if (i && (grid[i - 1][j] == 'D' || grid[i - 1][j] == 'H' || grid[i - 1][j] == 'M' || grid[i - 1][j] == 'G')) adj[i * N - N + j].push_back(i * N + j);
if (j && (grid[i][j-1] == 'D' || grid[i][j-1] == 'H' || grid[i][j-1] == 'M' || grid[i][j-1] == 'G')) adj[i * N + j-1].push_back(i * N + j);
if (i != N - 1 && (grid[i + 1][j] == 'D' || grid[i + 1][j] == 'H' || grid[i + 1][j] == 'M' || grid[i + 1][j] == 'G')) adj[i * N + N + j].push_back(i * N + j);
if (j != N - 1 && (grid[i][j+1] == 'D' || grid[i][j+1] == 'H' || grid[i][j+1] == 'M' || grid[i][j+1] == 'G')) adj[i * N + j+1].push_back(i * N + j);
}
}
while (!hives.empty()) {
auto x = hives.front(); hives.pop();
for (int y : adj[x.fi]) {
if (dist[y] == INT_MAX && grid[y / N][y % N] != 'D'){
hives.push({y, x.se + 1});
dist[y] = x.se;
}
}
}
int l = 0, r = N * N / 2 + N;
while (l <= r) {
int mid = l + (r - l) / 2;
vector<bool> pro(N * N, false);
queue<pair<int,int>> q;
q.push({mid * S, start});
bool won = false;
while (!q.empty()) {
auto u = q.front(); q.pop();
if (u.se == end) {
won = true;
break;
}
pro[u.se] = true;
if (u.fi / S >= dist[u.se]) continue;
for (int x : adj[u.se]) if (!pro[x]) q.push({u.fi + 1, x});
}
if (won) l = mid + 1;
else r = mid - 1;
}
cout << l - 1 << '\n';
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:57:4: warning: 'end' may be used uninitialized in this function [-Wmaybe-uninitialized]
57 | if (u.se == end) {
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 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 |
Runtime error |
177 ms |
65536 KB |
Execution killed with signal 9 |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
456 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Runtime error |
253 ms |
65536 KB |
Execution killed with signal 9 |
14 |
Runtime error |
176 ms |
65536 KB |
Execution killed with signal 9 |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
456 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
8 ms |
5820 KB |
Output is correct |
34 |
Correct |
8 ms |
5832 KB |
Output is correct |
35 |
Runtime error |
166 ms |
65536 KB |
Execution killed with signal 9 |
36 |
Correct |
12 ms |
7512 KB |
Output is correct |
37 |
Correct |
10 ms |
7700 KB |
Output is correct |
38 |
Runtime error |
176 ms |
65536 KB |
Execution killed with signal 9 |
39 |
Correct |
15 ms |
9564 KB |
Output is correct |
40 |
Correct |
14 ms |
9564 KB |
Output is correct |
41 |
Runtime error |
173 ms |
65536 KB |
Execution killed with signal 9 |
42 |
Correct |
16 ms |
11608 KB |
Output is correct |
43 |
Correct |
15 ms |
11612 KB |
Output is correct |
44 |
Runtime error |
170 ms |
65536 KB |
Execution killed with signal 9 |
45 |
Correct |
22 ms |
13916 KB |
Output is correct |
46 |
Correct |
25 ms |
13912 KB |
Output is correct |
47 |
Runtime error |
169 ms |
65536 KB |
Execution killed with signal 9 |
48 |
Correct |
23 ms |
16720 KB |
Output is correct |
49 |
Correct |
26 ms |
16732 KB |
Output is correct |
50 |
Runtime error |
167 ms |
65536 KB |
Execution killed with signal 9 |
51 |
Correct |
30 ms |
19436 KB |
Output is correct |
52 |
Correct |
28 ms |
19544 KB |
Output is correct |
53 |
Runtime error |
161 ms |
65536 KB |
Execution killed with signal 9 |
54 |
Correct |
34 ms |
22356 KB |
Output is correct |
55 |
Correct |
34 ms |
22620 KB |
Output is correct |
56 |
Runtime error |
164 ms |
65536 KB |
Execution killed with signal 9 |
57 |
Correct |
36 ms |
25692 KB |
Output is correct |
58 |
Correct |
41 ms |
25940 KB |
Output is correct |
59 |
Runtime error |
165 ms |
65536 KB |
Execution killed with signal 9 |
60 |
Correct |
46 ms |
29176 KB |
Output is correct |
61 |
Correct |
39 ms |
29276 KB |
Output is correct |
62 |
Runtime error |
162 ms |
65536 KB |
Execution killed with signal 9 |
63 |
Correct |
114 ms |
31060 KB |
Output is correct |
64 |
Correct |
178 ms |
31056 KB |
Output is correct |
65 |
Correct |
133 ms |
31056 KB |
Output is correct |
66 |
Correct |
115 ms |
31312 KB |
Output is correct |
67 |
Correct |
107 ms |
31108 KB |
Output is correct |
68 |
Correct |
89 ms |
31312 KB |
Output is correct |
69 |
Correct |
86 ms |
31312 KB |
Output is correct |
70 |
Correct |
79 ms |
31264 KB |
Output is correct |
71 |
Correct |
75 ms |
31268 KB |
Output is correct |
72 |
Correct |
69 ms |
31280 KB |
Output is correct |
73 |
Runtime error |
156 ms |
65536 KB |
Execution killed with signal 9 |
74 |
Runtime error |
171 ms |
65536 KB |
Execution killed with signal 9 |
75 |
Runtime error |
165 ms |
65536 KB |
Execution killed with signal 9 |
76 |
Runtime error |
161 ms |
65536 KB |
Execution killed with signal 9 |
77 |
Runtime error |
178 ms |
65536 KB |
Execution killed with signal 9 |
78 |
Runtime error |
168 ms |
65536 KB |
Execution killed with signal 9 |
79 |
Runtime error |
178 ms |
65536 KB |
Execution killed with signal 9 |
80 |
Runtime error |
168 ms |
65536 KB |
Execution killed with signal 9 |
81 |
Runtime error |
156 ms |
65536 KB |
Execution killed with signal 9 |
82 |
Runtime error |
161 ms |
65536 KB |
Execution killed with signal 9 |
83 |
Runtime error |
169 ms |
65536 KB |
Execution killed with signal 9 |
84 |
Runtime error |
181 ms |
65536 KB |
Execution killed with signal 9 |
85 |
Runtime error |
173 ms |
65536 KB |
Execution killed with signal 9 |
86 |
Runtime error |
166 ms |
65536 KB |
Execution killed with signal 9 |
87 |
Runtime error |
181 ms |
65536 KB |
Execution killed with signal 9 |
88 |
Runtime error |
172 ms |
65536 KB |
Execution killed with signal 9 |
89 |
Runtime error |
167 ms |
65536 KB |
Execution killed with signal 9 |
90 |
Runtime error |
179 ms |
65536 KB |
Execution killed with signal 9 |
91 |
Runtime error |
173 ms |
65536 KB |
Execution killed with signal 9 |
92 |
Runtime error |
197 ms |
65536 KB |
Execution killed with signal 9 |