#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, s;
cin >> n >> s;
vector<vector<char>> grid(n, vector<char>(n));
pii start, end;
for (int i{0}; i < n; ++i) {
for (int j{0}; j < n; ++j) {
cin >> grid[i][j];
if (grid[i][j] == 'M') start = {i,j};
else if (grid[i][j] == 'D') end = {i,j};
}
}
queue<pair<int,int>> q;
vector<vector<int>> pollu(n, vector<int>(n, INT_MAX));
for (int i{0}; i < n; ++i) {
for (int j{0}; j < n; ++j) {
if (grid[i][j] == 'H') {
pollu[i][j] = 0;
q.push({i, j});
}
}
}
// precompute distance to pollinate all regions
int dx[4] {-1, 1, 0, 0};
int dy[4] {0, 0, 1, -1};
while (q.size()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
for (int i{0}; i < 4; ++i) {
if (x + dx[i] < 0 || x + dx[i] >= n || y + dy[i] < 0 || y + dy[i] >= n || grid[x + dx[i]][y + dy[i]] == 'T') {
continue;
}
if (pollu[x][y] + 1 < pollu[x + dx[i]][y + dy[i]]) {
pollu[x + dx[i]][y + dy[i]] = pollu[x][y] + 1;
q.push({x + dx[i], y + dy[i]});
}
}
}
// for (int i{0}; i < n; ++i) {
// for (int j{0}; j < n; ++j) {
// if (pollu[i][j] == INT_MAX) {
// cout << '-' << ' ';
// continue;
// }
// cout << pollu[i][j] << ' ';
// }
// cout << '\n';
// }
// we can tell by construction that any adjacent cell will have either +1 or -1, we want to move on +1 route
// similarly we can tell that a movement of s cells is +[0,s]
// we want to find a maximum min over all segments of size s
// we can start by precomputing the distance to each cell from M
vector<vector<int>> dist(n, vector<int>(n, INT_MAX));
// implement binary search with bfs
// can't precompute distances, they are dependent on bee timings
int l{0}, r{n*n}, ans{-1};
while (l <= r) {
int mid{(l+r)/2};
q.push(start);
for (int i{0}; i < n; ++i) {
for (int j{0}; j < n; ++j) {
dist[i][j] = INT_MAX;
}
}
dist[start.first][start.second] = 0;
while (q.size()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
for (int i{0}; i < 4; ++i) {
if (x + dx[i] < 0 || x + dx[i] >= n || y + dy[i] < 0 || y + dy[i] >= n) continue;
if (grid[x + dx[i]][y + dy[i]] == 'T') continue;
if (pollu[x][y]-mid > (dist[x][y])/s && dist[x][y] + 1 < dist[x + dx[i]][y + dy[i]]) {
dist[x + dx[i]][y + dy[i]] = dist[x][y] + 1;
q.push({x + dx[i], y + dy[i]});
}
}
}
// cout << mid << '\n';
// for (int i{0}; i < n; ++i) {
// for (int j{0}; j < n; ++j) {
// if (dist[i][j] == INT_MAX) {
// cout << '-' << ' ';
// continue;
// }
// cout << dist[i][j] << ' ';
// }
// cout << '\n';
// }
if (dist[end.first][end.second] == INT_MAX) {
r = mid-1;
} else {
l = mid+1;
ans = mid;
}
}
cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
97 ms |
6056 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
240 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
5 ms |
1364 KB |
Output is correct |
34 |
Correct |
6 ms |
1324 KB |
Output is correct |
35 |
Correct |
23 ms |
1444 KB |
Output is correct |
36 |
Correct |
7 ms |
1648 KB |
Output is correct |
37 |
Correct |
7 ms |
1748 KB |
Output is correct |
38 |
Correct |
42 ms |
1764 KB |
Output is correct |
39 |
Correct |
9 ms |
2148 KB |
Output is correct |
40 |
Correct |
11 ms |
2132 KB |
Output is correct |
41 |
Correct |
44 ms |
2136 KB |
Output is correct |
42 |
Correct |
10 ms |
2588 KB |
Output is correct |
43 |
Correct |
15 ms |
2504 KB |
Output is correct |
44 |
Correct |
48 ms |
2480 KB |
Output is correct |
45 |
Correct |
12 ms |
3028 KB |
Output is correct |
46 |
Correct |
13 ms |
3028 KB |
Output is correct |
47 |
Correct |
61 ms |
3024 KB |
Output is correct |
48 |
Correct |
16 ms |
3560 KB |
Output is correct |
49 |
Correct |
17 ms |
3540 KB |
Output is correct |
50 |
Correct |
70 ms |
3548 KB |
Output is correct |
51 |
Correct |
18 ms |
4052 KB |
Output is correct |
52 |
Correct |
18 ms |
4100 KB |
Output is correct |
53 |
Correct |
85 ms |
4108 KB |
Output is correct |
54 |
Correct |
21 ms |
4700 KB |
Output is correct |
55 |
Correct |
23 ms |
4692 KB |
Output is correct |
56 |
Correct |
100 ms |
4744 KB |
Output is correct |
57 |
Correct |
23 ms |
5300 KB |
Output is correct |
58 |
Correct |
32 ms |
5304 KB |
Output is correct |
59 |
Correct |
112 ms |
5348 KB |
Output is correct |
60 |
Correct |
29 ms |
5936 KB |
Output is correct |
61 |
Correct |
28 ms |
5968 KB |
Output is correct |
62 |
Correct |
128 ms |
5972 KB |
Output is correct |
63 |
Correct |
105 ms |
5964 KB |
Output is correct |
64 |
Correct |
197 ms |
6036 KB |
Output is correct |
65 |
Correct |
157 ms |
6040 KB |
Output is correct |
66 |
Correct |
144 ms |
6036 KB |
Output is correct |
67 |
Correct |
119 ms |
6076 KB |
Output is correct |
68 |
Correct |
67 ms |
5964 KB |
Output is correct |
69 |
Correct |
51 ms |
5964 KB |
Output is correct |
70 |
Correct |
43 ms |
5956 KB |
Output is correct |
71 |
Correct |
60 ms |
5960 KB |
Output is correct |
72 |
Correct |
37 ms |
5960 KB |
Output is correct |
73 |
Correct |
36 ms |
5968 KB |
Output is correct |
74 |
Correct |
65 ms |
6072 KB |
Output is correct |
75 |
Correct |
82 ms |
5960 KB |
Output is correct |
76 |
Correct |
69 ms |
5976 KB |
Output is correct |
77 |
Correct |
71 ms |
5964 KB |
Output is correct |
78 |
Correct |
81 ms |
5964 KB |
Output is correct |
79 |
Correct |
54 ms |
6072 KB |
Output is correct |
80 |
Correct |
71 ms |
6068 KB |
Output is correct |
81 |
Correct |
85 ms |
6056 KB |
Output is correct |
82 |
Correct |
66 ms |
6076 KB |
Output is correct |
83 |
Correct |
85 ms |
6068 KB |
Output is correct |
84 |
Correct |
75 ms |
6036 KB |
Output is correct |
85 |
Correct |
95 ms |
6028 KB |
Output is correct |
86 |
Correct |
108 ms |
6060 KB |
Output is correct |
87 |
Correct |
84 ms |
5964 KB |
Output is correct |
88 |
Correct |
85 ms |
6060 KB |
Output is correct |
89 |
Correct |
83 ms |
6064 KB |
Output is correct |
90 |
Correct |
110 ms |
5972 KB |
Output is correct |
91 |
Correct |
97 ms |
6064 KB |
Output is correct |
92 |
Correct |
99 ms |
5968 KB |
Output is correct |