#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, int minDist[801][801]) {
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();
minDist[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] == 'G' || grid[r][c] == 'M')) {
visited[r][c] = true;
q.push({ {r, c}, dist + 1 });
}
}
}
}
void bfs2(int startR, int startC, int wait) {
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] == 'G' || grid[r][c] == 'M')) {
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);
for (int i = 0; i < 4; i++) {
int r = dR + dx[i], c = dC + dy[i];
if (isValid(r, c) && visited[r][c] && minBearDist[r][c] / S + wait < minHiveDist[r][c]) return true;
} return false;
}
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); minHiveDist[dR][dC] = (int)1e9;
int low = -1, high = N * N;
while (low < high) {
int mid = (low + high + 1) / 2;
if (works(mid)) low = mid;
else high = mid - 1;
} cout << low << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
63 ms |
5252 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
0 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
468 KB |
Output is correct |
21 |
Correct |
0 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
724 KB |
Output is correct |
33 |
Correct |
4 ms |
2004 KB |
Output is correct |
34 |
Correct |
5 ms |
2260 KB |
Output is correct |
35 |
Correct |
18 ms |
2772 KB |
Output is correct |
36 |
Correct |
5 ms |
2168 KB |
Output is correct |
37 |
Correct |
6 ms |
2516 KB |
Output is correct |
38 |
Correct |
22 ms |
3200 KB |
Output is correct |
39 |
Correct |
6 ms |
2388 KB |
Output is correct |
40 |
Correct |
8 ms |
2788 KB |
Output is correct |
41 |
Correct |
30 ms |
3572 KB |
Output is correct |
42 |
Correct |
8 ms |
2644 KB |
Output is correct |
43 |
Correct |
9 ms |
3136 KB |
Output is correct |
44 |
Correct |
37 ms |
3908 KB |
Output is correct |
45 |
Correct |
10 ms |
2936 KB |
Output is correct |
46 |
Correct |
14 ms |
3412 KB |
Output is correct |
47 |
Correct |
42 ms |
4276 KB |
Output is correct |
48 |
Correct |
11 ms |
3248 KB |
Output is correct |
49 |
Correct |
11 ms |
3652 KB |
Output is correct |
50 |
Correct |
54 ms |
4564 KB |
Output is correct |
51 |
Correct |
12 ms |
3380 KB |
Output is correct |
52 |
Correct |
14 ms |
3884 KB |
Output is correct |
53 |
Correct |
62 ms |
5004 KB |
Output is correct |
54 |
Correct |
15 ms |
3672 KB |
Output is correct |
55 |
Correct |
16 ms |
4180 KB |
Output is correct |
56 |
Correct |
83 ms |
5368 KB |
Output is correct |
57 |
Correct |
17 ms |
3892 KB |
Output is correct |
58 |
Correct |
18 ms |
4468 KB |
Output is correct |
59 |
Correct |
86 ms |
5724 KB |
Output is correct |
60 |
Correct |
18 ms |
4096 KB |
Output is correct |
61 |
Correct |
20 ms |
4784 KB |
Output is correct |
62 |
Correct |
96 ms |
6080 KB |
Output is correct |
63 |
Correct |
83 ms |
6092 KB |
Output is correct |
64 |
Correct |
156 ms |
6088 KB |
Output is correct |
65 |
Correct |
116 ms |
6072 KB |
Output is correct |
66 |
Correct |
98 ms |
6084 KB |
Output is correct |
67 |
Correct |
93 ms |
6080 KB |
Output is correct |
68 |
Correct |
41 ms |
6220 KB |
Output is correct |
69 |
Correct |
38 ms |
6100 KB |
Output is correct |
70 |
Correct |
29 ms |
6076 KB |
Output is correct |
71 |
Correct |
33 ms |
6068 KB |
Output is correct |
72 |
Incorrect |
26 ms |
4144 KB |
Output isn't correct |
73 |
Incorrect |
31 ms |
6548 KB |
Output isn't correct |
74 |
Correct |
44 ms |
6532 KB |
Output is correct |
75 |
Correct |
54 ms |
6532 KB |
Output is correct |
76 |
Correct |
50 ms |
6528 KB |
Output is correct |
77 |
Correct |
46 ms |
6536 KB |
Output is correct |
78 |
Correct |
65 ms |
5208 KB |
Output is correct |
79 |
Correct |
38 ms |
4736 KB |
Output is correct |
80 |
Correct |
46 ms |
4928 KB |
Output is correct |
81 |
Correct |
52 ms |
5024 KB |
Output is correct |
82 |
Correct |
47 ms |
5028 KB |
Output is correct |
83 |
Correct |
59 ms |
5024 KB |
Output is correct |
84 |
Correct |
52 ms |
4936 KB |
Output is correct |
85 |
Correct |
57 ms |
5076 KB |
Output is correct |
86 |
Correct |
62 ms |
5120 KB |
Output is correct |
87 |
Correct |
56 ms |
4992 KB |
Output is correct |
88 |
Correct |
60 ms |
5128 KB |
Output is correct |
89 |
Correct |
60 ms |
5076 KB |
Output is correct |
90 |
Correct |
63 ms |
5104 KB |
Output is correct |
91 |
Correct |
63 ms |
5136 KB |
Output is correct |
92 |
Correct |
66 ms |
5132 KB |
Output is correct |