#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, s, mx, my, hx, hy;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
bool check(int t, vector<vector<char>> grid, vector<vector<int>> btime) {
priority_queue<pair<int, pair<int, int>>> q;
q.push({0, {mx, my}});
vector<vector<int>> dist(n, vector<int>(n, 0x3f3f3f3f));
dist[mx][my] = 0;
while (!q.empty()) {
auto tp = q.top();
q.pop();
if (grid[tp.second.first][tp.second.second] == 'D') {
return true;
}
for (int i = 0; i < 4; i++) {
int nx = dx[i] + tp.second.first;
int ny = dy[i] + tp.second.second;
if (nx < 0 || nx >= n || ny < 0 || ny >= n) continue;
if (grid[nx][ny] == 'T' || (btime[nx][ny] - t) * s <= -tp.first) continue;
if (dist[nx][ny] > 1 - tp.first) {
dist[nx][ny] = 1 - tp.first;
q.push({tp.first - 1, {nx, ny}});
}
}
}
for (int i = 0; i < 4; i++) {
int nx = dx[i] + hx;
int ny = dy[i] + hy;
if (nx < 0 || nx >= n || ny < 0 || ny >= n) continue;
if (dist[nx][ny] != 0x3f3f3f3f) return true;
}
return false;
}
signed main() {
cin >> n >> s;
vector<vector<char>> grid(n, vector<char>(n));
priority_queue<pair<int, pair<int, int>>> q;
vector<vector<int>> btime(n, vector<int>(n, 0x3f3f3f3f));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> grid[i][j];
if (grid[i][j] == 'H') {
btime[i][j] = 0;
q.push({0, {i, j}});
}
if (grid[i][j] == 'M') mx = i, my = j;
if (grid[i][j] == 'D') hx = i, hy = j;
}
}
while (!q.empty()) {
auto tp = q.top();
q.pop();
if (btime[tp.second.first][tp.second.second] != -tp.first) continue;
for (int i = 0; i < 4; i++) {
int nx = dx[i] + tp.second.first;
int ny = dy[i] + tp.second.second;
if (nx < 0 || nx >= n || ny < 0 || ny >= n) continue;
if (grid[nx][ny] != 'G' && grid[nx][ny] != 'M') continue;
if (-tp.first + 1 < btime[nx][ny]) {
btime[nx][ny] = 1 - tp.first;
q.push({-1 + tp.first, {nx, ny}});
}
}
}
int l = 0;
int r = 1e9 + 1;
while (l != r) {
int mid = (l + r + 1)/2;
if (check(mid, grid, btime)) {
l = mid;
}
else {
r = mid - 1;
}
}
cout << l << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
432 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 |
0 ms |
348 KB |
Output isn't correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
365 ms |
17824 KB |
Output is correct |
8 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
14 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
15 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
20 |
Correct |
0 ms |
460 KB |
Output is correct |
21 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
22 |
Correct |
1 ms |
420 KB |
Output is correct |
23 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
30 |
Correct |
1 ms |
424 KB |
Output is correct |
31 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Incorrect |
21 ms |
3792 KB |
Output isn't correct |
34 |
Correct |
23 ms |
3772 KB |
Output is correct |
35 |
Correct |
75 ms |
3836 KB |
Output is correct |
36 |
Incorrect |
38 ms |
4804 KB |
Output isn't correct |
37 |
Correct |
25 ms |
4804 KB |
Output is correct |
38 |
Correct |
99 ms |
4828 KB |
Output is correct |
39 |
Incorrect |
36 ms |
6048 KB |
Output isn't correct |
40 |
Correct |
46 ms |
5928 KB |
Output is correct |
41 |
Correct |
141 ms |
5924 KB |
Output is correct |
42 |
Incorrect |
45 ms |
7572 KB |
Output isn't correct |
43 |
Correct |
40 ms |
7240 KB |
Output is correct |
44 |
Correct |
169 ms |
7252 KB |
Output is correct |
45 |
Incorrect |
56 ms |
8792 KB |
Output isn't correct |
46 |
Correct |
58 ms |
8640 KB |
Output is correct |
47 |
Correct |
226 ms |
8608 KB |
Output is correct |
48 |
Incorrect |
84 ms |
10160 KB |
Output isn't correct |
49 |
Correct |
72 ms |
10180 KB |
Output is correct |
50 |
Correct |
240 ms |
10140 KB |
Output is correct |
51 |
Incorrect |
92 ms |
11864 KB |
Output isn't correct |
52 |
Correct |
66 ms |
11924 KB |
Output is correct |
53 |
Correct |
308 ms |
11924 KB |
Output is correct |
54 |
Incorrect |
92 ms |
13732 KB |
Output isn't correct |
55 |
Correct |
106 ms |
13704 KB |
Output is correct |
56 |
Correct |
402 ms |
13592 KB |
Output is correct |
57 |
Incorrect |
136 ms |
15676 KB |
Output isn't correct |
58 |
Correct |
96 ms |
15596 KB |
Output is correct |
59 |
Correct |
451 ms |
15940 KB |
Output is correct |
60 |
Incorrect |
134 ms |
17660 KB |
Output isn't correct |
61 |
Correct |
146 ms |
17660 KB |
Output is correct |
62 |
Correct |
516 ms |
17652 KB |
Output is correct |
63 |
Correct |
313 ms |
17700 KB |
Output is correct |
64 |
Correct |
570 ms |
17596 KB |
Output is correct |
65 |
Correct |
478 ms |
17668 KB |
Output is correct |
66 |
Incorrect |
373 ms |
17664 KB |
Output isn't correct |
67 |
Incorrect |
352 ms |
17636 KB |
Output isn't correct |
68 |
Correct |
248 ms |
17660 KB |
Output is correct |
69 |
Correct |
219 ms |
17700 KB |
Output is correct |
70 |
Correct |
218 ms |
17956 KB |
Output is correct |
71 |
Correct |
169 ms |
17744 KB |
Output is correct |
72 |
Incorrect |
197 ms |
17684 KB |
Output isn't correct |
73 |
Incorrect |
208 ms |
18396 KB |
Output isn't correct |
74 |
Correct |
270 ms |
18352 KB |
Output is correct |
75 |
Correct |
290 ms |
18516 KB |
Output is correct |
76 |
Correct |
302 ms |
18352 KB |
Output is correct |
77 |
Correct |
285 ms |
18380 KB |
Output is correct |
78 |
Incorrect |
329 ms |
18348 KB |
Output isn't correct |
79 |
Correct |
281 ms |
18304 KB |
Output is correct |
80 |
Correct |
284 ms |
18340 KB |
Output is correct |
81 |
Correct |
315 ms |
18340 KB |
Output is correct |
82 |
Correct |
305 ms |
18304 KB |
Output is correct |
83 |
Correct |
323 ms |
18188 KB |
Output is correct |
84 |
Correct |
318 ms |
18268 KB |
Output is correct |
85 |
Correct |
342 ms |
18332 KB |
Output is correct |
86 |
Correct |
334 ms |
18164 KB |
Output is correct |
87 |
Correct |
343 ms |
18184 KB |
Output is correct |
88 |
Correct |
331 ms |
18144 KB |
Output is correct |
89 |
Correct |
357 ms |
18060 KB |
Output is correct |
90 |
Correct |
373 ms |
18108 KB |
Output is correct |
91 |
Correct |
391 ms |
18092 KB |
Output is correct |
92 |
Correct |
344 ms |
18064 KB |
Output is correct |