#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; }
void bfs(vector<pii> initialCoords, int minDist[801][801]) {
queue<pair<pii, int>> q;
vector<vector<bool>> visited(801, vector<bool>(801));
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] != 'T') {
visited[r][c] = true;
q.push({ {r, c}, dist + 1 });
}
}
}
}
vector<vector<bool>> visited;
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] != 'T') {
if (dist / S + 1 + 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 = 2 * N;
while (low < high) {
int mid = (low + high + 1) / 2;
if (works(mid)) low = mid;
else high = mid - 1;
} cout << low << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
464 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
456 KB |
Output is correct |
7 |
Correct |
63 ms |
5752 KB |
Output is correct |
8 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
480 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
14 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
15 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
16 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
17 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
18 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
19 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
20 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
21 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
24 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
25 |
Incorrect |
2 ms |
712 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
840 KB |
Output isn't correct |
27 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
852 KB |
Output isn't correct |
29 |
Incorrect |
1 ms |
844 KB |
Output isn't correct |
30 |
Incorrect |
1 ms |
840 KB |
Output isn't correct |
31 |
Incorrect |
1 ms |
852 KB |
Output isn't correct |
32 |
Incorrect |
1 ms |
968 KB |
Output isn't correct |
33 |
Incorrect |
10 ms |
2388 KB |
Output isn't correct |
34 |
Incorrect |
14 ms |
3028 KB |
Output isn't correct |
35 |
Correct |
18 ms |
3048 KB |
Output is correct |
36 |
Incorrect |
14 ms |
2820 KB |
Output isn't correct |
37 |
Incorrect |
23 ms |
3412 KB |
Output isn't correct |
38 |
Correct |
30 ms |
3408 KB |
Output is correct |
39 |
Incorrect |
17 ms |
3128 KB |
Output isn't correct |
40 |
Incorrect |
28 ms |
3808 KB |
Output isn't correct |
41 |
Correct |
29 ms |
3920 KB |
Output is correct |
42 |
Incorrect |
20 ms |
3452 KB |
Output isn't correct |
43 |
Incorrect |
28 ms |
4180 KB |
Output isn't correct |
44 |
Correct |
37 ms |
4220 KB |
Output is correct |
45 |
Incorrect |
26 ms |
3660 KB |
Output isn't correct |
46 |
Incorrect |
36 ms |
4564 KB |
Output isn't correct |
47 |
Correct |
52 ms |
4636 KB |
Output is correct |
48 |
Incorrect |
31 ms |
4052 KB |
Output isn't correct |
49 |
Incorrect |
44 ms |
4928 KB |
Output isn't correct |
50 |
Correct |
50 ms |
5032 KB |
Output is correct |
51 |
Incorrect |
35 ms |
4392 KB |
Output isn't correct |
52 |
Incorrect |
49 ms |
5332 KB |
Output isn't correct |
53 |
Correct |
66 ms |
5448 KB |
Output is correct |
54 |
Incorrect |
41 ms |
4684 KB |
Output isn't correct |
55 |
Incorrect |
63 ms |
5764 KB |
Output isn't correct |
56 |
Correct |
75 ms |
5844 KB |
Output is correct |
57 |
Incorrect |
47 ms |
5108 KB |
Output isn't correct |
58 |
Incorrect |
66 ms |
6272 KB |
Output isn't correct |
59 |
Correct |
88 ms |
6292 KB |
Output is correct |
60 |
Incorrect |
54 ms |
5460 KB |
Output isn't correct |
61 |
Incorrect |
76 ms |
6612 KB |
Output isn't correct |
62 |
Correct |
107 ms |
6720 KB |
Output is correct |
63 |
Correct |
87 ms |
6732 KB |
Output is correct |
64 |
Correct |
128 ms |
6720 KB |
Output is correct |
65 |
Correct |
121 ms |
6724 KB |
Output is correct |
66 |
Incorrect |
95 ms |
6612 KB |
Output isn't correct |
67 |
Incorrect |
90 ms |
6712 KB |
Output isn't correct |
68 |
Correct |
42 ms |
6484 KB |
Output is correct |
69 |
Correct |
31 ms |
6732 KB |
Output is correct |
70 |
Correct |
29 ms |
6764 KB |
Output is correct |
71 |
Correct |
29 ms |
6680 KB |
Output is correct |
72 |
Incorrect |
25 ms |
4640 KB |
Output isn't correct |
73 |
Incorrect |
32 ms |
7052 KB |
Output isn't correct |
74 |
Correct |
53 ms |
7096 KB |
Output is correct |
75 |
Correct |
51 ms |
7068 KB |
Output is correct |
76 |
Correct |
59 ms |
7088 KB |
Output is correct |
77 |
Correct |
51 ms |
7228 KB |
Output is correct |
78 |
Incorrect |
68 ms |
5756 KB |
Output isn't correct |
79 |
Correct |
47 ms |
5632 KB |
Output is correct |
80 |
Correct |
51 ms |
5600 KB |
Output is correct |
81 |
Correct |
56 ms |
5716 KB |
Output is correct |
82 |
Correct |
50 ms |
5576 KB |
Output is correct |
83 |
Correct |
68 ms |
5752 KB |
Output is correct |
84 |
Correct |
58 ms |
5732 KB |
Output is correct |
85 |
Correct |
58 ms |
5676 KB |
Output is correct |
86 |
Correct |
61 ms |
5760 KB |
Output is correct |
87 |
Correct |
55 ms |
5680 KB |
Output is correct |
88 |
Correct |
60 ms |
5852 KB |
Output is correct |
89 |
Correct |
66 ms |
5828 KB |
Output is correct |
90 |
Correct |
67 ms |
5828 KB |
Output is correct |
91 |
Correct |
66 ms |
5716 KB |
Output is correct |
92 |
Correct |
69 ms |
5808 KB |
Output is correct |