#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) {
if (bees[start.x][start.y] <= Time) return false;
queue < pair <int, int> > q;
q.push (start);
vector < vector <int> > dist (n + 1, vector <int> (n + 1, INT_MAX));
dist[start.x][start.y] = 0;
while (!q.empty ()) {
auto [X, Y] = q.front ();
q.pop ();
if (finish == make_pair (X, Y)) break;
for (int i = 0; i < 4; i++) {
int nwX = X + dx[i], nwY = Y + dy[i];
if (valid (nwX, nwY) and grid[nwX][nwY] != 'T') {
if ((dist[X][Y] + 1) / k < bees[nwX][nwY] - Time and dist[X][Y] + 1 < dist[nwX][nwY]) {
dist[nwX][nwY] = dist[X][Y] + 1;
q.push ({nwX, nwY});
}
}
}
}
return dist[finish.x][finish.y] != 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) and (grid[nwX][nwY] != 'T' and grid[nwX][nwY] != 'D') 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;
}
Compilation message
mecho.cpp: In function 'bool check(int)':
mecho.cpp:31:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
31 | auto [X, Y] = q.front ();
| ^
mecho.cpp: In function 'int main()':
mecho.cpp:72:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
72 | auto [X, Y] = q.front ();
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
79 ms |
6816 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is 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 |
Correct |
1 ms |
592 KB |
Output is correct |
13 |
Correct |
1 ms |
592 KB |
Output is correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
592 KB |
Output is correct |
20 |
Correct |
1 ms |
592 KB |
Output is correct |
21 |
Correct |
1 ms |
592 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
592 KB |
Output is correct |
24 |
Correct |
1 ms |
592 KB |
Output is correct |
25 |
Correct |
1 ms |
592 KB |
Output is correct |
26 |
Correct |
1 ms |
592 KB |
Output is correct |
27 |
Correct |
1 ms |
760 KB |
Output is correct |
28 |
Correct |
1 ms |
592 KB |
Output is correct |
29 |
Correct |
1 ms |
592 KB |
Output is correct |
30 |
Correct |
1 ms |
592 KB |
Output is correct |
31 |
Correct |
1 ms |
592 KB |
Output is correct |
32 |
Correct |
1 ms |
592 KB |
Output is correct |
33 |
Correct |
8 ms |
2412 KB |
Output is correct |
34 |
Correct |
7 ms |
2480 KB |
Output is correct |
35 |
Correct |
22 ms |
2688 KB |
Output is correct |
36 |
Correct |
9 ms |
2640 KB |
Output is correct |
37 |
Correct |
10 ms |
2672 KB |
Output is correct |
38 |
Correct |
26 ms |
2880 KB |
Output is correct |
39 |
Correct |
11 ms |
3264 KB |
Output is correct |
40 |
Correct |
10 ms |
3264 KB |
Output is correct |
41 |
Correct |
36 ms |
3072 KB |
Output is correct |
42 |
Correct |
15 ms |
3696 KB |
Output is correct |
43 |
Correct |
14 ms |
3460 KB |
Output is correct |
44 |
Correct |
48 ms |
3696 KB |
Output is correct |
45 |
Correct |
20 ms |
4348 KB |
Output is correct |
46 |
Correct |
17 ms |
4164 KB |
Output is correct |
47 |
Correct |
45 ms |
4132 KB |
Output is correct |
48 |
Correct |
19 ms |
4272 KB |
Output is correct |
49 |
Correct |
18 ms |
4292 KB |
Output is correct |
50 |
Correct |
63 ms |
4612 KB |
Output is correct |
51 |
Correct |
25 ms |
4752 KB |
Output is correct |
52 |
Correct |
21 ms |
5204 KB |
Output is correct |
53 |
Correct |
66 ms |
5160 KB |
Output is correct |
54 |
Correct |
29 ms |
5216 KB |
Output is correct |
55 |
Correct |
24 ms |
5460 KB |
Output is correct |
56 |
Correct |
77 ms |
5676 KB |
Output is correct |
57 |
Correct |
31 ms |
5712 KB |
Output is correct |
58 |
Correct |
30 ms |
5712 KB |
Output is correct |
59 |
Correct |
96 ms |
6160 KB |
Output is correct |
60 |
Correct |
32 ms |
6720 KB |
Output is correct |
61 |
Correct |
38 ms |
6720 KB |
Output is correct |
62 |
Correct |
103 ms |
6680 KB |
Output is correct |
63 |
Correct |
104 ms |
6692 KB |
Output is correct |
64 |
Correct |
170 ms |
6792 KB |
Output is correct |
65 |
Correct |
131 ms |
6176 KB |
Output is correct |
66 |
Correct |
119 ms |
6184 KB |
Output is correct |
67 |
Correct |
105 ms |
6824 KB |
Output is correct |
68 |
Correct |
52 ms |
6348 KB |
Output is correct |
69 |
Correct |
52 ms |
6724 KB |
Output is correct |
70 |
Correct |
48 ms |
6360 KB |
Output is correct |
71 |
Correct |
44 ms |
6836 KB |
Output is correct |
72 |
Correct |
38 ms |
6704 KB |
Output is correct |
73 |
Correct |
32 ms |
6556 KB |
Output is correct |
74 |
Correct |
56 ms |
6712 KB |
Output is correct |
75 |
Correct |
68 ms |
6648 KB |
Output is correct |
76 |
Correct |
62 ms |
6200 KB |
Output is correct |
77 |
Correct |
70 ms |
6104 KB |
Output is correct |
78 |
Correct |
73 ms |
6700 KB |
Output is correct |
79 |
Correct |
50 ms |
6776 KB |
Output is correct |
80 |
Correct |
58 ms |
6712 KB |
Output is correct |
81 |
Correct |
66 ms |
6716 KB |
Output is correct |
82 |
Correct |
59 ms |
7000 KB |
Output is correct |
83 |
Correct |
74 ms |
6852 KB |
Output is correct |
84 |
Correct |
72 ms |
6712 KB |
Output is correct |
85 |
Correct |
77 ms |
6184 KB |
Output is correct |
86 |
Correct |
88 ms |
6700 KB |
Output is correct |
87 |
Correct |
72 ms |
6852 KB |
Output is correct |
88 |
Correct |
85 ms |
6848 KB |
Output is correct |
89 |
Correct |
96 ms |
6652 KB |
Output is correct |
90 |
Correct |
90 ms |
6664 KB |
Output is correct |
91 |
Correct |
85 ms |
6084 KB |
Output is correct |
92 |
Correct |
84 ms |
6116 KB |
Output is correct |