#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 <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 ();
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] + 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] == '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;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
104 ms |
6208 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 |
Incorrect |
1 ms |
592 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
592 KB |
Output isn't 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 |
760 KB |
Output is correct |
22 |
Correct |
1 ms |
592 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 |
760 KB |
Output is correct |
27 |
Correct |
1 ms |
592 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 |
764 KB |
Output is correct |
31 |
Correct |
1 ms |
592 KB |
Output is correct |
32 |
Correct |
1 ms |
592 KB |
Output is correct |
33 |
Correct |
9 ms |
2412 KB |
Output is correct |
34 |
Correct |
9 ms |
2388 KB |
Output is correct |
35 |
Correct |
23 ms |
2340 KB |
Output is correct |
36 |
Correct |
13 ms |
2640 KB |
Output is correct |
37 |
Correct |
11 ms |
2640 KB |
Output is correct |
38 |
Correct |
30 ms |
2652 KB |
Output is correct |
39 |
Correct |
16 ms |
3072 KB |
Output is correct |
40 |
Correct |
15 ms |
3132 KB |
Output is correct |
41 |
Correct |
41 ms |
3076 KB |
Output is correct |
42 |
Correct |
20 ms |
3424 KB |
Output is correct |
43 |
Correct |
18 ms |
3440 KB |
Output is correct |
44 |
Correct |
49 ms |
3448 KB |
Output is correct |
45 |
Correct |
23 ms |
3848 KB |
Output is correct |
46 |
Correct |
23 ms |
3848 KB |
Output is correct |
47 |
Correct |
58 ms |
3860 KB |
Output is correct |
48 |
Correct |
27 ms |
4268 KB |
Output is correct |
49 |
Correct |
24 ms |
4268 KB |
Output is correct |
50 |
Correct |
72 ms |
4268 KB |
Output is correct |
51 |
Correct |
31 ms |
4800 KB |
Output is correct |
52 |
Correct |
31 ms |
4784 KB |
Output is correct |
53 |
Correct |
84 ms |
4764 KB |
Output is correct |
54 |
Correct |
42 ms |
5228 KB |
Output is correct |
55 |
Correct |
41 ms |
5208 KB |
Output is correct |
56 |
Correct |
105 ms |
5216 KB |
Output is correct |
57 |
Correct |
40 ms |
5708 KB |
Output is correct |
58 |
Correct |
49 ms |
5704 KB |
Output is correct |
59 |
Correct |
126 ms |
5716 KB |
Output is correct |
60 |
Correct |
48 ms |
6208 KB |
Output is correct |
61 |
Correct |
49 ms |
6196 KB |
Output is correct |
62 |
Correct |
136 ms |
6200 KB |
Output is correct |
63 |
Correct |
118 ms |
6216 KB |
Output is correct |
64 |
Correct |
183 ms |
6168 KB |
Output is correct |
65 |
Correct |
165 ms |
6192 KB |
Output is correct |
66 |
Correct |
143 ms |
6180 KB |
Output is correct |
67 |
Correct |
121 ms |
6192 KB |
Output is correct |
68 |
Correct |
74 ms |
6252 KB |
Output is correct |
69 |
Correct |
74 ms |
6200 KB |
Output is correct |
70 |
Correct |
66 ms |
6324 KB |
Output is correct |
71 |
Correct |
64 ms |
6216 KB |
Output is correct |
72 |
Incorrect |
53 ms |
6220 KB |
Output isn't correct |
73 |
Incorrect |
63 ms |
6264 KB |
Output isn't correct |
74 |
Correct |
71 ms |
6216 KB |
Output is correct |
75 |
Correct |
87 ms |
6220 KB |
Output is correct |
76 |
Correct |
87 ms |
6232 KB |
Output is correct |
77 |
Correct |
88 ms |
6256 KB |
Output is correct |
78 |
Correct |
98 ms |
6256 KB |
Output is correct |
79 |
Correct |
69 ms |
6240 KB |
Output is correct |
80 |
Correct |
86 ms |
6248 KB |
Output is correct |
81 |
Correct |
95 ms |
6228 KB |
Output is correct |
82 |
Correct |
120 ms |
6268 KB |
Output is correct |
83 |
Correct |
118 ms |
6244 KB |
Output is correct |
84 |
Correct |
91 ms |
6212 KB |
Output is correct |
85 |
Correct |
132 ms |
6208 KB |
Output is correct |
86 |
Correct |
99 ms |
6228 KB |
Output is correct |
87 |
Correct |
100 ms |
6232 KB |
Output is correct |
88 |
Correct |
100 ms |
6220 KB |
Output is correct |
89 |
Correct |
103 ms |
6140 KB |
Output is correct |
90 |
Correct |
114 ms |
6188 KB |
Output is correct |
91 |
Correct |
100 ms |
6156 KB |
Output is correct |
92 |
Correct |
113 ms |
6212 KB |
Output is correct |