#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<vector<int>> adj(N * N);
vector<int> hives;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (grid[i][j] == 'H') hives.push_back(i * N + j);
else if (grid[i][j] == 'M') start = i * N + j;
else if (grid[i][j] == 'D') end = i * N + j;
if (grid[i][j] != 'G') 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);
}
}
vector<int> dist(N * N, INT_MAX);
set<int> processed;
int cnt = 1;
while (!hives.empty()) {
vector<int> newHives;
for (int x : hives) {
for (int y : adj[x]) {
if (processed.count(y)) continue;
dist[y] = cnt;
processed.insert(y);
newHives.push_back(y);
}
}
cnt++;
swap(hives, newHives);
}
vector<int> c(N * N, 0);
priority_queue<vector<int>> q;
set<int> st;
q.push({-INT_MAX, start, 0});
while (!q.empty()) {
auto u = q.top(); q.pop();
if (st.count(u[1])) continue;
st.insert(u[1]);
for (int y : adj[u[1]]) {
if (min(-u[0], dist[y] - u[2] / S - 1) > c[y]) {
c[y] = min(-u[0], dist[y] - u[2] / S - 1);
q.push({-c[y], y, u[2] + 1});
}
}
}
int ans = 0;
for (int x : adj[end]) ans = max(ans, c[x]);
cout << ans << '\n';
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:66:22: warning: 'end' may be used uninitialized in this function [-Wmaybe-uninitialized]
66 | for (int x : adj[end]) ans = max(ans, c[x]);
| ^
mecho.cpp:53:29: warning: 'start' may be used uninitialized in this function [-Wmaybe-uninitialized]
53 | q.push({-INT_MAX, start, 0});
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
5 |
Correct |
0 ms |
600 KB |
Output is correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Runtime error |
378 ms |
65536 KB |
Execution killed with signal 9 |
8 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
9 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
10 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
11 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
12 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
14 |
Incorrect |
2 ms |
716 KB |
Output isn't correct |
15 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Incorrect |
1 ms |
456 KB |
Output isn't correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
26 |
Correct |
2 ms |
604 KB |
Output is correct |
27 |
Incorrect |
2 ms |
604 KB |
Output isn't correct |
28 |
Correct |
2 ms |
856 KB |
Output is correct |
29 |
Incorrect |
2 ms |
860 KB |
Output isn't correct |
30 |
Correct |
1 ms |
928 KB |
Output is correct |
31 |
Incorrect |
2 ms |
936 KB |
Output isn't correct |
32 |
Correct |
2 ms |
856 KB |
Output is correct |
33 |
Incorrect |
27 ms |
10684 KB |
Output isn't correct |
34 |
Correct |
33 ms |
12120 KB |
Output is correct |
35 |
Incorrect |
78 ms |
15956 KB |
Output isn't correct |
36 |
Incorrect |
33 ms |
13908 KB |
Output isn't correct |
37 |
Correct |
47 ms |
15700 KB |
Output is correct |
38 |
Incorrect |
133 ms |
20692 KB |
Output isn't correct |
39 |
Incorrect |
45 ms |
17488 KB |
Output isn't correct |
40 |
Correct |
63 ms |
19652 KB |
Output is correct |
41 |
Incorrect |
145 ms |
25940 KB |
Output isn't correct |
42 |
Incorrect |
58 ms |
21328 KB |
Output isn't correct |
43 |
Correct |
80 ms |
24180 KB |
Output is correct |
44 |
Incorrect |
192 ms |
32080 KB |
Output isn't correct |
45 |
Incorrect |
64 ms |
25936 KB |
Output isn't correct |
46 |
Correct |
115 ms |
29408 KB |
Output is correct |
47 |
Incorrect |
265 ms |
38624 KB |
Output isn't correct |
48 |
Incorrect |
85 ms |
30800 KB |
Output isn't correct |
49 |
Correct |
102 ms |
34896 KB |
Output is correct |
50 |
Incorrect |
269 ms |
46108 KB |
Output isn't correct |
51 |
Incorrect |
89 ms |
35924 KB |
Output isn't correct |
52 |
Correct |
118 ms |
40752 KB |
Output is correct |
53 |
Incorrect |
323 ms |
54080 KB |
Output isn't correct |
54 |
Incorrect |
110 ms |
41624 KB |
Output isn't correct |
55 |
Correct |
140 ms |
47188 KB |
Output is correct |
56 |
Incorrect |
418 ms |
62544 KB |
Output isn't correct |
57 |
Incorrect |
128 ms |
47764 KB |
Output isn't correct |
58 |
Correct |
159 ms |
54216 KB |
Output is correct |
59 |
Runtime error |
357 ms |
65536 KB |
Execution killed with signal 9 |
60 |
Incorrect |
142 ms |
54236 KB |
Output isn't correct |
61 |
Correct |
186 ms |
61780 KB |
Output is correct |
62 |
Runtime error |
376 ms |
65536 KB |
Execution killed with signal 9 |
63 |
Correct |
342 ms |
59476 KB |
Output is correct |
64 |
Runtime error |
344 ms |
65536 KB |
Execution killed with signal 9 |
65 |
Runtime error |
349 ms |
65536 KB |
Execution killed with signal 9 |
66 |
Incorrect |
402 ms |
65536 KB |
Output isn't correct |
67 |
Incorrect |
330 ms |
60896 KB |
Output isn't correct |
68 |
Correct |
296 ms |
55640 KB |
Output is correct |
69 |
Correct |
308 ms |
57424 KB |
Output is correct |
70 |
Correct |
298 ms |
55120 KB |
Output is correct |
71 |
Correct |
317 ms |
54612 KB |
Output is correct |
72 |
Incorrect |
315 ms |
57168 KB |
Output isn't correct |
73 |
Runtime error |
382 ms |
65536 KB |
Execution killed with signal 9 |
74 |
Runtime error |
390 ms |
65536 KB |
Execution killed with signal 9 |
75 |
Runtime error |
424 ms |
65536 KB |
Execution killed with signal 9 |
76 |
Runtime error |
396 ms |
65536 KB |
Execution killed with signal 9 |
77 |
Runtime error |
380 ms |
65536 KB |
Execution killed with signal 9 |
78 |
Runtime error |
349 ms |
65536 KB |
Execution killed with signal 9 |
79 |
Runtime error |
337 ms |
65536 KB |
Execution killed with signal 9 |
80 |
Runtime error |
343 ms |
65536 KB |
Execution killed with signal 9 |
81 |
Runtime error |
331 ms |
65536 KB |
Execution killed with signal 9 |
82 |
Runtime error |
343 ms |
65536 KB |
Execution killed with signal 9 |
83 |
Runtime error |
342 ms |
65536 KB |
Execution killed with signal 9 |
84 |
Runtime error |
356 ms |
65536 KB |
Execution killed with signal 9 |
85 |
Runtime error |
367 ms |
65536 KB |
Execution killed with signal 9 |
86 |
Runtime error |
341 ms |
65536 KB |
Execution killed with signal 9 |
87 |
Runtime error |
344 ms |
65536 KB |
Execution killed with signal 9 |
88 |
Runtime error |
363 ms |
65536 KB |
Execution killed with signal 9 |
89 |
Runtime error |
410 ms |
65536 KB |
Execution killed with signal 9 |
90 |
Runtime error |
348 ms |
65536 KB |
Execution killed with signal 9 |
91 |
Runtime error |
348 ms |
65536 KB |
Execution killed with signal 9 |
92 |
Runtime error |
361 ms |
65536 KB |
Execution killed with signal 9 |