#include <iostream>
#include <vector>
#include <queue>
#define pii pair<int, int>
using namespace std;
int N, S;
int mR, mC, dR, dC;
char grid[801][801];
int minHiveDist[801][801], minBearDist[801][801];
const int dx[4] = { -1, 1, 0, 0 };
const int dy[4] = { 0, 0, -1, 1 };
bool isValid(int r, int c) { return 0 <= r && r < N && 0 <= c && c < N; }
vector<vector<bool>> visited;
void bfs(vector<pii> initialCoords) {
queue<pair<pii, int>> q;
visited.clear(); visited.resize(N, vector<bool>(N));
for (pii p : initialCoords) {
q.push({ p, 0 }); visited[p.first][p.second] = true;
}
while (!q.empty()) {
pii loc = q.front().first; int dist = q.front().second; q.pop();
minHiveDist[loc.first][loc.second] = dist;
for (int i = 0; i < 4; i++) {
int r = loc.first + dx[i], c = loc.second + dy[i];
if (isValid(r, c) && !visited[r][c] && grid[r][c] != 'T') {
visited[r][c] = true;
q.push({ {r, c}, dist + 1 });
}
}
}
}
void bfs2(int startR, int startC, int wait) {
if (wait >= minHiveDist[startR][startC]) return;
visited.clear(); visited.resize(N, vector<bool>(N));
queue<pair<pii, int>> q; q.push({ {startR, startC}, 0 });
visited[startR][startC] = true;
while (!q.empty()) {
pii loc = q.front().first; int dist = q.front().second; q.pop();
minBearDist[loc.first][loc.second] = dist;
for (int i = 0; i < 4; i++) {
int r = loc.first + dx[i], c = loc.second + dy[i];
if (isValid(r, c) && !visited[r][c] && grid[r][c] != 'T') {
if ((dist + 1) / S + wait < minHiveDist[r][c]) {
visited[r][c] = true;
q.push({ {r, c}, dist + 1 });
}
}
}
}
}
bool works(int wait) {
bfs2(mR, mC, wait);
return visited[dR][dC];
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> N >> S;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
cin >> grid[i][j];
vector<pii> hives;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (grid[i][j] == 'H') hives.push_back({ i, j });
if (grid[i][j] == 'M') { mR = i; mC = j; }
if (grid[i][j] == 'D') { dR = i; dC = j; }
}
}
bfs(hives); minHiveDist[dR][dC] = (int)1e9;
int low = 0, high = S;
while (low < high) {
int mid = (low + high + 1) / 2;
if (works(mid)) low = mid;
else high = mid - 1;
} cout << (works(low) ? low : -1) << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
5 |
Incorrect |
0 ms |
356 KB |
Output isn't correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
61 ms |
5124 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
11 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
12 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
16 |
Incorrect |
0 ms |
468 KB |
Output isn't correct |
17 |
Incorrect |
0 ms |
468 KB |
Output isn't correct |
18 |
Incorrect |
0 ms |
468 KB |
Output isn't correct |
19 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
20 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
21 |
Incorrect |
0 ms |
468 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
24 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
25 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
27 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
29 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
30 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
31 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
32 |
Incorrect |
1 ms |
852 KB |
Output isn't correct |
33 |
Incorrect |
5 ms |
2260 KB |
Output isn't correct |
34 |
Incorrect |
13 ms |
2728 KB |
Output isn't correct |
35 |
Correct |
24 ms |
2736 KB |
Output is correct |
36 |
Incorrect |
6 ms |
2516 KB |
Output isn't correct |
37 |
Incorrect |
17 ms |
3196 KB |
Output isn't correct |
38 |
Correct |
25 ms |
3156 KB |
Output is correct |
39 |
Incorrect |
7 ms |
2772 KB |
Output isn't correct |
40 |
Incorrect |
22 ms |
3560 KB |
Output isn't correct |
41 |
Correct |
39 ms |
3568 KB |
Output is correct |
42 |
Incorrect |
9 ms |
3028 KB |
Output isn't correct |
43 |
Incorrect |
26 ms |
3924 KB |
Output isn't correct |
44 |
Correct |
41 ms |
3820 KB |
Output is correct |
45 |
Incorrect |
11 ms |
3412 KB |
Output isn't correct |
46 |
Incorrect |
38 ms |
4256 KB |
Output isn't correct |
47 |
Correct |
53 ms |
4232 KB |
Output is correct |
48 |
Incorrect |
13 ms |
3632 KB |
Output isn't correct |
49 |
Incorrect |
43 ms |
4624 KB |
Output isn't correct |
50 |
Correct |
57 ms |
4636 KB |
Output is correct |
51 |
Incorrect |
14 ms |
3972 KB |
Output isn't correct |
52 |
Incorrect |
46 ms |
4900 KB |
Output isn't correct |
53 |
Correct |
86 ms |
4996 KB |
Output is correct |
54 |
Incorrect |
17 ms |
4228 KB |
Output isn't correct |
55 |
Incorrect |
66 ms |
5244 KB |
Output isn't correct |
56 |
Correct |
82 ms |
5356 KB |
Output is correct |
57 |
Incorrect |
19 ms |
4516 KB |
Output isn't correct |
58 |
Incorrect |
68 ms |
5720 KB |
Output isn't correct |
59 |
Correct |
107 ms |
5728 KB |
Output is correct |
60 |
Incorrect |
23 ms |
4820 KB |
Output isn't correct |
61 |
Incorrect |
70 ms |
6072 KB |
Output isn't correct |
62 |
Correct |
109 ms |
6100 KB |
Output is correct |
63 |
Correct |
39 ms |
6084 KB |
Output is correct |
64 |
Correct |
158 ms |
6092 KB |
Output is correct |
65 |
Incorrect |
75 ms |
6088 KB |
Output isn't correct |
66 |
Incorrect |
57 ms |
6012 KB |
Output isn't correct |
67 |
Correct |
42 ms |
5964 KB |
Output is correct |
68 |
Correct |
38 ms |
6080 KB |
Output is correct |
69 |
Correct |
39 ms |
6128 KB |
Output is correct |
70 |
Correct |
33 ms |
6072 KB |
Output is correct |
71 |
Correct |
27 ms |
6068 KB |
Output is correct |
72 |
Incorrect |
34 ms |
6072 KB |
Output isn't correct |
73 |
Incorrect |
20 ms |
4048 KB |
Output isn't correct |
74 |
Correct |
50 ms |
6480 KB |
Output is correct |
75 |
Incorrect |
50 ms |
6488 KB |
Output isn't correct |
76 |
Incorrect |
40 ms |
6484 KB |
Output isn't correct |
77 |
Incorrect |
52 ms |
6528 KB |
Output isn't correct |
78 |
Correct |
41 ms |
5192 KB |
Output is correct |
79 |
Correct |
45 ms |
4816 KB |
Output is correct |
80 |
Incorrect |
46 ms |
5076 KB |
Output isn't correct |
81 |
Incorrect |
43 ms |
5136 KB |
Output isn't correct |
82 |
Incorrect |
52 ms |
5208 KB |
Output isn't correct |
83 |
Correct |
42 ms |
5048 KB |
Output is correct |
84 |
Correct |
57 ms |
4956 KB |
Output is correct |
85 |
Incorrect |
52 ms |
5100 KB |
Output isn't correct |
86 |
Incorrect |
53 ms |
5060 KB |
Output isn't correct |
87 |
Incorrect |
57 ms |
5048 KB |
Output isn't correct |
88 |
Correct |
49 ms |
5204 KB |
Output is correct |
89 |
Incorrect |
20 ms |
3740 KB |
Output isn't correct |
90 |
Correct |
53 ms |
5068 KB |
Output is correct |
91 |
Correct |
75 ms |
5204 KB |
Output is correct |
92 |
Correct |
64 ms |
5188 KB |
Output is correct |