#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);
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); minHiveDist[dR][dC] = (int)1e9;
int low = 0, high = N * N;
while (low < high) {
int mid = (low + high + 1) / 2;
if (works(mid)) low = mid;
else high = mid - 1;
} cout << (works(low) ? low : low - 1) << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
4 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
5 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
6 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
7 |
Incorrect |
76 ms |
6248 KB |
Output isn't correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Incorrect |
1 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 |
596 KB |
Output isn't correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Incorrect |
0 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 |
0 ms |
468 KB |
Output isn't correct |
20 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
21 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
496 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 |
14 ms |
2260 KB |
Output isn't correct |
34 |
Incorrect |
19 ms |
2772 KB |
Output isn't correct |
35 |
Incorrect |
21 ms |
2840 KB |
Output isn't correct |
36 |
Incorrect |
19 ms |
2520 KB |
Output isn't correct |
37 |
Incorrect |
32 ms |
3088 KB |
Output isn't correct |
38 |
Incorrect |
25 ms |
3156 KB |
Output isn't correct |
39 |
Incorrect |
23 ms |
2820 KB |
Output isn't correct |
40 |
Incorrect |
35 ms |
3540 KB |
Output isn't correct |
41 |
Incorrect |
30 ms |
3440 KB |
Output isn't correct |
42 |
Incorrect |
28 ms |
3028 KB |
Output isn't correct |
43 |
Incorrect |
49 ms |
3860 KB |
Output isn't correct |
44 |
Incorrect |
44 ms |
3924 KB |
Output isn't correct |
45 |
Incorrect |
36 ms |
3404 KB |
Output isn't correct |
46 |
Incorrect |
50 ms |
4244 KB |
Output isn't correct |
47 |
Incorrect |
45 ms |
4180 KB |
Output isn't correct |
48 |
Incorrect |
42 ms |
3668 KB |
Output isn't correct |
49 |
Incorrect |
60 ms |
4628 KB |
Output isn't correct |
50 |
Incorrect |
55 ms |
4580 KB |
Output isn't correct |
51 |
Incorrect |
49 ms |
3948 KB |
Output isn't correct |
52 |
Incorrect |
71 ms |
4884 KB |
Output isn't correct |
53 |
Incorrect |
68 ms |
4948 KB |
Output isn't correct |
54 |
Incorrect |
58 ms |
4252 KB |
Output isn't correct |
55 |
Incorrect |
81 ms |
5328 KB |
Output isn't correct |
56 |
Incorrect |
79 ms |
5364 KB |
Output isn't correct |
57 |
Incorrect |
68 ms |
4428 KB |
Output isn't correct |
58 |
Incorrect |
114 ms |
5640 KB |
Output isn't correct |
59 |
Incorrect |
96 ms |
5728 KB |
Output isn't correct |
60 |
Incorrect |
80 ms |
4752 KB |
Output isn't correct |
61 |
Incorrect |
114 ms |
6052 KB |
Output isn't correct |
62 |
Incorrect |
103 ms |
6008 KB |
Output isn't correct |
63 |
Incorrect |
89 ms |
5972 KB |
Output isn't correct |
64 |
Incorrect |
191 ms |
6084 KB |
Output isn't correct |
65 |
Incorrect |
161 ms |
6088 KB |
Output isn't correct |
66 |
Incorrect |
130 ms |
6084 KB |
Output isn't correct |
67 |
Correct |
99 ms |
6084 KB |
Output is correct |
68 |
Incorrect |
47 ms |
6084 KB |
Output isn't correct |
69 |
Incorrect |
49 ms |
6092 KB |
Output isn't correct |
70 |
Incorrect |
37 ms |
6060 KB |
Output isn't correct |
71 |
Incorrect |
34 ms |
6100 KB |
Output isn't correct |
72 |
Incorrect |
62 ms |
6136 KB |
Output isn't correct |
73 |
Incorrect |
36 ms |
6412 KB |
Output isn't correct |
74 |
Incorrect |
93 ms |
6548 KB |
Output isn't correct |
75 |
Incorrect |
61 ms |
6484 KB |
Output isn't correct |
76 |
Incorrect |
64 ms |
6596 KB |
Output isn't correct |
77 |
Incorrect |
66 ms |
6516 KB |
Output isn't correct |
78 |
Correct |
72 ms |
5136 KB |
Output is correct |
79 |
Incorrect |
94 ms |
6240 KB |
Output isn't correct |
80 |
Incorrect |
64 ms |
5120 KB |
Output isn't correct |
81 |
Incorrect |
60 ms |
4980 KB |
Output isn't correct |
82 |
Incorrect |
76 ms |
5168 KB |
Output isn't correct |
83 |
Incorrect |
66 ms |
5012 KB |
Output isn't correct |
84 |
Incorrect |
93 ms |
6404 KB |
Output isn't correct |
85 |
Incorrect |
63 ms |
5132 KB |
Output isn't correct |
86 |
Incorrect |
63 ms |
5108 KB |
Output isn't correct |
87 |
Incorrect |
67 ms |
5224 KB |
Output isn't correct |
88 |
Incorrect |
65 ms |
5156 KB |
Output isn't correct |
89 |
Incorrect |
96 ms |
6220 KB |
Output isn't correct |
90 |
Incorrect |
72 ms |
5836 KB |
Output isn't correct |
91 |
Incorrect |
77 ms |
6200 KB |
Output isn't correct |
92 |
Incorrect |
67 ms |
5304 KB |
Output isn't correct |