#include <bits/stdc++.h>
#define int long long
#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);
}
int l = 1, r = N * N;
while (l <= r) {
int mid = l + (r - l) / 2;
map<int, int> mecho;
mecho[start] = mid * S;
priority_queue<pair<double,int>> q;
q.push({-mid * S, start});
set<int> processed;
while(!q.empty()) {
auto u = q.top(); q.pop();
if (processed.find(u.se) != processed.end()) continue;
processed.insert(u.se);
for (int y : adj[u.se]) {
if ((-u.fi + 1) / S < dist[y] && (!mecho.count(y) || -u.fi + 1 < mecho[y]) ) {
mecho[y] = -u.fi + 1;
q.push({-mecho[y], y});
}
}
}
bool won = false;
for (int y : adj[end]) if (mecho.count(y)) won = true;
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:74:23: warning: 'end' may be used uninitialized in this function [-Wmaybe-uninitialized]
74 | for (int y : adj[end]) if (mecho.count(y)) won = true;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 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 |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Runtime error |
225 ms |
65536 KB |
Execution killed with signal 9 |
8 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
456 KB |
Output is correct |
12 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
14 |
Incorrect |
3 ms |
860 KB |
Output isn't correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
704 KB |
Output is correct |
26 |
Correct |
2 ms |
604 KB |
Output is correct |
27 |
Correct |
2 ms |
860 KB |
Output is correct |
28 |
Correct |
2 ms |
860 KB |
Output is correct |
29 |
Correct |
1 ms |
860 KB |
Output is correct |
30 |
Correct |
2 ms |
860 KB |
Output is correct |
31 |
Correct |
2 ms |
860 KB |
Output is correct |
32 |
Correct |
3 ms |
860 KB |
Output is correct |
33 |
Correct |
29 ms |
11096 KB |
Output is correct |
34 |
Correct |
40 ms |
12480 KB |
Output is correct |
35 |
Correct |
744 ms |
22608 KB |
Output is correct |
36 |
Correct |
36 ms |
14164 KB |
Output is correct |
37 |
Correct |
55 ms |
16212 KB |
Output is correct |
38 |
Execution timed out |
1032 ms |
29308 KB |
Time limit exceeded |
39 |
Correct |
47 ms |
17748 KB |
Output is correct |
40 |
Correct |
69 ms |
20560 KB |
Output is correct |
41 |
Execution timed out |
1077 ms |
36948 KB |
Time limit exceeded |
42 |
Correct |
60 ms |
21988 KB |
Output is correct |
43 |
Correct |
87 ms |
25164 KB |
Output is correct |
44 |
Execution timed out |
1068 ms |
45396 KB |
Time limit exceeded |
45 |
Correct |
73 ms |
26452 KB |
Output is correct |
46 |
Correct |
106 ms |
30544 KB |
Output is correct |
47 |
Execution timed out |
1031 ms |
55120 KB |
Time limit exceeded |
48 |
Correct |
91 ms |
31568 KB |
Output is correct |
49 |
Correct |
156 ms |
36156 KB |
Output is correct |
50 |
Execution timed out |
1012 ms |
64848 KB |
Time limit exceeded |
51 |
Correct |
104 ms |
36732 KB |
Output is correct |
52 |
Correct |
146 ms |
42544 KB |
Output is correct |
53 |
Runtime error |
469 ms |
65536 KB |
Execution killed with signal 9 |
54 |
Correct |
128 ms |
42580 KB |
Output is correct |
55 |
Correct |
187 ms |
49156 KB |
Output is correct |
56 |
Runtime error |
392 ms |
65536 KB |
Execution killed with signal 9 |
57 |
Correct |
160 ms |
48976 KB |
Output is correct |
58 |
Correct |
206 ms |
56400 KB |
Output is correct |
59 |
Runtime error |
365 ms |
65536 KB |
Execution killed with signal 9 |
60 |
Correct |
166 ms |
55636 KB |
Output is correct |
61 |
Correct |
237 ms |
64304 KB |
Output is correct |
62 |
Runtime error |
343 ms |
65536 KB |
Execution killed with signal 9 |
63 |
Runtime error |
476 ms |
65536 KB |
Execution killed with signal 9 |
64 |
Runtime error |
460 ms |
65536 KB |
Execution killed with signal 9 |
65 |
Runtime error |
366 ms |
65536 KB |
Execution killed with signal 9 |
66 |
Runtime error |
380 ms |
65536 KB |
Execution killed with signal 9 |
67 |
Runtime error |
432 ms |
65536 KB |
Execution killed with signal 9 |
68 |
Correct |
603 ms |
62428 KB |
Output is correct |
69 |
Correct |
529 ms |
64084 KB |
Output is correct |
70 |
Correct |
474 ms |
60956 KB |
Output is correct |
71 |
Correct |
415 ms |
60244 KB |
Output is correct |
72 |
Incorrect |
323 ms |
53584 KB |
Output isn't correct |
73 |
Runtime error |
210 ms |
65536 KB |
Execution killed with signal 9 |
74 |
Runtime error |
214 ms |
65536 KB |
Execution killed with signal 9 |
75 |
Runtime error |
213 ms |
65536 KB |
Execution killed with signal 9 |
76 |
Runtime error |
216 ms |
65536 KB |
Execution killed with signal 9 |
77 |
Runtime error |
208 ms |
65536 KB |
Execution killed with signal 9 |
78 |
Runtime error |
212 ms |
65536 KB |
Execution killed with signal 9 |
79 |
Runtime error |
210 ms |
65536 KB |
Execution killed with signal 9 |
80 |
Runtime error |
212 ms |
65536 KB |
Execution killed with signal 9 |
81 |
Runtime error |
224 ms |
65536 KB |
Execution killed with signal 9 |
82 |
Runtime error |
219 ms |
65536 KB |
Execution killed with signal 9 |
83 |
Runtime error |
225 ms |
65536 KB |
Execution killed with signal 9 |
84 |
Runtime error |
222 ms |
65536 KB |
Execution killed with signal 9 |
85 |
Runtime error |
215 ms |
65536 KB |
Execution killed with signal 9 |
86 |
Runtime error |
222 ms |
65536 KB |
Execution killed with signal 9 |
87 |
Runtime error |
232 ms |
65536 KB |
Execution killed with signal 9 |
88 |
Runtime error |
216 ms |
65536 KB |
Execution killed with signal 9 |
89 |
Runtime error |
221 ms |
65536 KB |
Execution killed with signal 9 |
90 |
Runtime error |
237 ms |
65536 KB |
Execution killed with signal 9 |
91 |
Runtime error |
223 ms |
65536 KB |
Execution killed with signal 9 |
92 |
Runtime error |
218 ms |
65536 KB |
Execution killed with signal 9 |