#include <bits/stdc++.h>
using namespace std;
#define all(V) V.begin(), V.end()
#define x first
#define y second
using LL = long long;
const int N = 808;
int dx[4] = {+1, -1, 0, 0};
int dy[4] = {0, 0, +1, -1};
char grid[N][N];
int n, k, bees[N][N];
pair <int, int> start, finish;
bool valid (int i, int j) {
return (i > 0 and i <= n and j > 0 and j <= n);
}
bool check (int Time) {
queue < pair <int, int> > q;
q.push (start);
vector < vector < pair <int, int> > > dist (n + 1, vector < pair <int, int> > (n + 1, {INT_MAX, 0}));
dist[start.x][start.y] = {Time, 0};
while (!q.empty ()) {
auto [X, Y] = q.front ();
q.pop ();
for (int i = 0; i < 4; i++) {
int nwX = X + dx[i], nwY = Y + dy[i];
if (valid (nwX, nwY) and (grid[nwX][nwY] == 'G' or grid[nwX][nwY] == 'D')) {
if (dist[X][Y].x + (dist[X][Y].y == k) < dist[nwX][nwY].x and dist[X][Y].x + (dist[X][Y].y == k) < bees[nwX][nwY]) {
if (dist[X][Y].y == k) dist[nwX][nwY] = {dist[X][Y].x + 1, 1};
else dist[nwX][nwY] = {dist[X][Y].x, dist[X][Y].y + 1};
q.push ({nwX, nwY});
}
}
}
}
return dist[finish.x][finish.y].x != INT_MAX;
}
int main() {
cin.tie (nullptr) -> ios_base :: sync_with_stdio (false);
cin >> n >> k;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> grid[i][j];
if (grid[i][j] == 'M') start = {i, j};
if (grid[i][j] == 'D') finish = {i, j};
}
}
queue < pair <int, int> > q;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (grid[i][j] == 'H') {
q.push ({i, j});
bees[i][j] = 0;
} else {
bees[i][j] = INT_MAX;
}
}
}
while (!q.empty ()) {
auto [X, Y] = q.front ();
q.pop ();
for (int i = 0; i < 4; i++) {
int nwX = X + dx[i], nwY = Y + dy[i];
if (!valid (nwX, nwY)) continue;
if (valid (nwX, nwY) and (grid[nwX][nwY] == 'G' or grid[nwX][nwY] == 'M') and bees[X][Y] + 1 < bees[nwX][nwY]) {
bees[nwX][nwY] = bees[X][Y] + 1;
q.push ({nwX, nwY});
}
}
}
int left = 0, right = n * n;
while (left < right) {
int mid = (left + right + 1) >> 1;
if (check (mid)) left = mid;
else right = mid - 1;
}
if (!check (left)) left--;
cout << left << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
5 |
Correct |
1 ms |
500 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
124 ms |
9376 KB |
Output is correct |
8 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Incorrect |
1 ms |
2808 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
592 KB |
Output isn't correct |
14 |
Correct |
2 ms |
2808 KB |
Output is correct |
15 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Incorrect |
1 ms |
592 KB |
Output isn't correct |
20 |
Correct |
1 ms |
592 KB |
Output is correct |
21 |
Incorrect |
1 ms |
592 KB |
Output isn't correct |
22 |
Correct |
1 ms |
592 KB |
Output is correct |
23 |
Incorrect |
1 ms |
592 KB |
Output isn't correct |
24 |
Correct |
1 ms |
592 KB |
Output is correct |
25 |
Incorrect |
1 ms |
2640 KB |
Output isn't correct |
26 |
Correct |
1 ms |
2808 KB |
Output is correct |
27 |
Incorrect |
1 ms |
2640 KB |
Output isn't correct |
28 |
Correct |
2 ms |
2640 KB |
Output is correct |
29 |
Incorrect |
1 ms |
2640 KB |
Output isn't correct |
30 |
Correct |
2 ms |
2640 KB |
Output is correct |
31 |
Incorrect |
1 ms |
2640 KB |
Output isn't correct |
32 |
Correct |
1 ms |
2640 KB |
Output is correct |
33 |
Incorrect |
13 ms |
4064 KB |
Output isn't correct |
34 |
Correct |
13 ms |
4052 KB |
Output is correct |
35 |
Correct |
26 ms |
4064 KB |
Output is correct |
36 |
Incorrect |
21 ms |
4440 KB |
Output isn't correct |
37 |
Correct |
18 ms |
4448 KB |
Output is correct |
38 |
Correct |
35 ms |
4440 KB |
Output is correct |
39 |
Incorrect |
24 ms |
4864 KB |
Output isn't correct |
40 |
Correct |
23 ms |
4864 KB |
Output is correct |
41 |
Correct |
46 ms |
4928 KB |
Output is correct |
42 |
Incorrect |
29 ms |
5400 KB |
Output isn't correct |
43 |
Correct |
29 ms |
5392 KB |
Output is correct |
44 |
Correct |
58 ms |
5376 KB |
Output is correct |
45 |
Incorrect |
34 ms |
5876 KB |
Output isn't correct |
46 |
Correct |
35 ms |
5872 KB |
Output is correct |
47 |
Correct |
69 ms |
5872 KB |
Output is correct |
48 |
Incorrect |
41 ms |
6416 KB |
Output isn't correct |
49 |
Correct |
40 ms |
6488 KB |
Output is correct |
50 |
Correct |
83 ms |
6444 KB |
Output is correct |
51 |
Incorrect |
49 ms |
7056 KB |
Output isn't correct |
52 |
Correct |
53 ms |
7084 KB |
Output is correct |
53 |
Correct |
99 ms |
6976 KB |
Output is correct |
54 |
Incorrect |
57 ms |
7620 KB |
Output isn't correct |
55 |
Correct |
57 ms |
7760 KB |
Output is correct |
56 |
Correct |
126 ms |
7648 KB |
Output is correct |
57 |
Incorrect |
66 ms |
8532 KB |
Output isn't correct |
58 |
Correct |
66 ms |
8520 KB |
Output is correct |
59 |
Correct |
133 ms |
8500 KB |
Output is correct |
60 |
Incorrect |
80 ms |
9440 KB |
Output isn't correct |
61 |
Correct |
91 ms |
9396 KB |
Output is correct |
62 |
Correct |
153 ms |
9344 KB |
Output is correct |
63 |
Correct |
154 ms |
9384 KB |
Output is correct |
64 |
Correct |
233 ms |
9384 KB |
Output is correct |
65 |
Correct |
191 ms |
9320 KB |
Output is correct |
66 |
Incorrect |
153 ms |
9392 KB |
Output isn't correct |
67 |
Correct |
148 ms |
9356 KB |
Output is correct |
68 |
Correct |
101 ms |
9392 KB |
Output is correct |
69 |
Correct |
96 ms |
9392 KB |
Output is correct |
70 |
Correct |
97 ms |
9452 KB |
Output is correct |
71 |
Correct |
99 ms |
9552 KB |
Output is correct |
72 |
Incorrect |
82 ms |
9424 KB |
Output isn't correct |
73 |
Incorrect |
83 ms |
9468 KB |
Output isn't correct |
74 |
Correct |
106 ms |
9360 KB |
Output is correct |
75 |
Correct |
127 ms |
9416 KB |
Output is correct |
76 |
Correct |
109 ms |
9456 KB |
Output is correct |
77 |
Correct |
110 ms |
9392 KB |
Output is correct |
78 |
Correct |
119 ms |
9496 KB |
Output is correct |
79 |
Correct |
101 ms |
9400 KB |
Output is correct |
80 |
Correct |
99 ms |
9472 KB |
Output is correct |
81 |
Correct |
106 ms |
9416 KB |
Output is correct |
82 |
Correct |
106 ms |
9376 KB |
Output is correct |
83 |
Correct |
140 ms |
9360 KB |
Output is correct |
84 |
Correct |
116 ms |
9448 KB |
Output is correct |
85 |
Correct |
118 ms |
9384 KB |
Output is correct |
86 |
Correct |
131 ms |
9420 KB |
Output is correct |
87 |
Correct |
118 ms |
9444 KB |
Output is correct |
88 |
Correct |
132 ms |
9400 KB |
Output is correct |
89 |
Correct |
141 ms |
9400 KB |
Output is correct |
90 |
Correct |
135 ms |
9628 KB |
Output is correct |
91 |
Correct |
126 ms |
9392 KB |
Output is correct |
92 |
Correct |
128 ms |
9388 KB |
Output is correct |