#include <iostream>
#include <array>
#include <string>
#include <queue>
const int MAX_N = 800 + 1;
const int INF = 1e9;
std::array<std::string, MAX_N> grid;
std::array<std::array<int, MAX_N>, MAX_N> minuteToBeHive, level;
void bfs(int n) {
std::queue<std::pair<int, int>> q;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
minuteToBeHive[i][j] = INF;
if (grid[i][j] == 'H') {
q.push({i, j});
minuteToBeHive[i][j] = 0;
}
}
}
while (!q.empty()) {
auto v = q.front();
q.pop();
if (v.first > 0) {
if (grid[v.first - 1][v.second] == 'G' || grid[v.first - 1][v.second] == 'M') {
if (minuteToBeHive[v.first - 1][v.second] == INF)
q.push({v.first - 1, v.second});
minuteToBeHive[v.first - 1][v.second] = std::min(minuteToBeHive[v.first - 1][v.second], minuteToBeHive[v.first][v.second] + 1);
}
}
if (v.first < n - 1) {
if (grid[v.first + 1][v.second] == 'G' || grid[v.first + 1][v.second] == 'M') {
if (minuteToBeHive[v.first + 1][v.second] == INF)
q.push({v.first + 1, v.second});
minuteToBeHive[v.first + 1][v.second] = std::min(minuteToBeHive[v.first + 1][v.second], minuteToBeHive[v.first][v.second] + 1);
}
}
if (v.second > 0) {
if (grid[v.first][v.second - 1] == 'G' || grid[v.first][v.second - 1] == 'M') {
if (minuteToBeHive[v.first][v.second - 1] == INF)
q.push({v.first, v.second - 1});
minuteToBeHive[v.first][v.second - 1] = std::min(minuteToBeHive[v.first][v.second - 1], minuteToBeHive[v.first][v.second] + 1);
}
}
if (v.second < n - 1) {
if (grid[v.first][v.second + 1] == 'G' || grid[v.first][v.second + 1] == 'M') {
if (minuteToBeHive[v.first][v.second + 1] == INF)
q.push({v.first, v.second + 1});
minuteToBeHive[v.first][v.second + 1] = std::min(minuteToBeHive[v.first][v.second + 1], minuteToBeHive[v.first][v.second] + 1);
}
}
}
}
bool bfs2(int n, int s, int startMinute) {
std::pair<int, int> start, end;
std::queue<std::pair<int, int>> q;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
level[i][j] = INF;
if (grid[i][j] == 'M')
start = {i, j};
if (grid[i][j] == 'D')
end = {i, j};
}
}
q.push(start);
level[start.first][start.second] = 0;
if (startMinute >= minuteToBeHive[start.first][start.second])
return false;
while (!q.empty()) {
auto v = q.front();
q.pop();
if (v.first > 0) {
if ((grid[v.first - 1][v.second] == 'G' || grid[v.first - 1][v.second] == 'D' || grid[v.first - 1][v.second] == 'M') && (startMinute + (level[v.first][v.second] + 1) / s < minuteToBeHive[v.first - 1][v.second])) {
if (level[v.first - 1][v.second] == INF)
q.push({v.first - 1, v.second});
level[v.first - 1][v.second] = std::min(level[v.first - 1][v.second], level[v.first][v.second] + 1);
}
}
if (v.first < n - 1) {
if ((grid[v.first + 1][v.second] == 'G' || grid[v.first + 1][v.second] == 'D' || grid[v.first + 1][v.second] == 'M') && (startMinute + (level[v.first][v.second] + 1) / s < minuteToBeHive[v.first + 1][v.second])) {
if (level[v.first + 1][v.second] == INF)
q.push({v.first + 1, v.second});
level[v.first + 1][v.second] = std::min(level[v.first + 1][v.second], level[v.first][v.second] + 1);
}
}
if (v.second > 0) {
if ((grid[v.first][v.second - 1] == 'G' || grid[v.first][v.second - 1] == 'D' || grid[v.first][v.second - 1] == 'M') && (startMinute + (level[v.first][v.second] + 1) / s < minuteToBeHive[v.first][v.second - 1])) {
if (level[v.first][v.second - 1] == INF)
q.push({v.first, v.second - 1});
level[v.first][v.second - 1] = std::min(level[v.first][v.second - 1], level[v.first][v.second] + 1);
}
}
if (v.second < n - 1) {
if ((grid[v.first][v.second + 1] == 'G' || grid[v.first][v.second + 1] == 'D' || grid[v.first][v.second + 1] == 'M') && (startMinute + (level[v.first][v.second] + 1) / s < minuteToBeHive[v.first][v.second + 1])) {
if (level[v.first][v.second + 1] == INF)
q.push({v.first, v.second + 1});
level[v.first][v.second + 1] = std::min(level[v.first][v.second + 1], level[v.first][v.second] + 1);
}
}
}
return level[end.first][end.second] != INF;
}
int solve(int n, int s) {
bfs(n);
int l = 0, r = n * n;
while (r - l > 1) {
int mid = (r + l) / 2;
if (bfs2(n, s, mid)) {
l = mid;
} else {
r = mid;
}
}
if (bfs2(n, s, l))
return l;
return -1;
}
int main() {
int n, s; std::cin >> n >> s;
for (int i = 0; i < n; ++i) {
std::cin >> grid[i];
}
std::cout << solve(n, s) << std::endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2520 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2516 KB |
Output is correct |
7 |
Correct |
73 ms |
6392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
4700 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2516 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
0 ms |
2652 KB |
Output is correct |
25 |
Correct |
1 ms |
4700 KB |
Output is correct |
26 |
Correct |
1 ms |
4700 KB |
Output is correct |
27 |
Correct |
1 ms |
4700 KB |
Output is correct |
28 |
Correct |
1 ms |
4700 KB |
Output is correct |
29 |
Correct |
1 ms |
4700 KB |
Output is correct |
30 |
Correct |
1 ms |
4700 KB |
Output is correct |
31 |
Correct |
1 ms |
4700 KB |
Output is correct |
32 |
Correct |
1 ms |
4700 KB |
Output is correct |
33 |
Correct |
7 ms |
4956 KB |
Output is correct |
34 |
Correct |
5 ms |
4956 KB |
Output is correct |
35 |
Correct |
17 ms |
4956 KB |
Output is correct |
36 |
Correct |
7 ms |
4952 KB |
Output is correct |
37 |
Correct |
11 ms |
4976 KB |
Output is correct |
38 |
Correct |
21 ms |
4956 KB |
Output is correct |
39 |
Correct |
8 ms |
4956 KB |
Output is correct |
40 |
Correct |
9 ms |
4956 KB |
Output is correct |
41 |
Correct |
28 ms |
4956 KB |
Output is correct |
42 |
Correct |
11 ms |
5208 KB |
Output is correct |
43 |
Correct |
10 ms |
5212 KB |
Output is correct |
44 |
Correct |
35 ms |
5212 KB |
Output is correct |
45 |
Correct |
14 ms |
5468 KB |
Output is correct |
46 |
Correct |
13 ms |
5248 KB |
Output is correct |
47 |
Correct |
40 ms |
5480 KB |
Output is correct |
48 |
Correct |
15 ms |
5468 KB |
Output is correct |
49 |
Correct |
15 ms |
5656 KB |
Output is correct |
50 |
Correct |
51 ms |
5464 KB |
Output is correct |
51 |
Correct |
17 ms |
5724 KB |
Output is correct |
52 |
Correct |
17 ms |
5724 KB |
Output is correct |
53 |
Correct |
63 ms |
5940 KB |
Output is correct |
54 |
Correct |
20 ms |
5980 KB |
Output is correct |
55 |
Correct |
20 ms |
6180 KB |
Output is correct |
56 |
Correct |
69 ms |
5972 KB |
Output is correct |
57 |
Correct |
23 ms |
6232 KB |
Output is correct |
58 |
Correct |
22 ms |
6236 KB |
Output is correct |
59 |
Correct |
78 ms |
6236 KB |
Output is correct |
60 |
Correct |
30 ms |
6492 KB |
Output is correct |
61 |
Correct |
33 ms |
6492 KB |
Output is correct |
62 |
Correct |
97 ms |
6488 KB |
Output is correct |
63 |
Correct |
80 ms |
6488 KB |
Output is correct |
64 |
Correct |
137 ms |
6480 KB |
Output is correct |
65 |
Correct |
109 ms |
6668 KB |
Output is correct |
66 |
Correct |
92 ms |
6492 KB |
Output is correct |
67 |
Correct |
88 ms |
6492 KB |
Output is correct |
68 |
Correct |
44 ms |
6484 KB |
Output is correct |
69 |
Correct |
43 ms |
6472 KB |
Output is correct |
70 |
Correct |
42 ms |
6688 KB |
Output is correct |
71 |
Correct |
42 ms |
6484 KB |
Output is correct |
72 |
Correct |
31 ms |
6480 KB |
Output is correct |
73 |
Correct |
36 ms |
6740 KB |
Output is correct |
74 |
Correct |
48 ms |
6732 KB |
Output is correct |
75 |
Correct |
57 ms |
6748 KB |
Output is correct |
76 |
Correct |
55 ms |
6740 KB |
Output is correct |
77 |
Correct |
58 ms |
6920 KB |
Output is correct |
78 |
Correct |
64 ms |
6748 KB |
Output is correct |
79 |
Correct |
44 ms |
6748 KB |
Output is correct |
80 |
Correct |
51 ms |
6604 KB |
Output is correct |
81 |
Correct |
57 ms |
6852 KB |
Output is correct |
82 |
Correct |
56 ms |
6736 KB |
Output is correct |
83 |
Correct |
64 ms |
6860 KB |
Output is correct |
84 |
Correct |
61 ms |
6484 KB |
Output is correct |
85 |
Correct |
63 ms |
6856 KB |
Output is correct |
86 |
Correct |
66 ms |
6712 KB |
Output is correct |
87 |
Correct |
61 ms |
6748 KB |
Output is correct |
88 |
Correct |
66 ms |
6832 KB |
Output is correct |
89 |
Correct |
71 ms |
6748 KB |
Output is correct |
90 |
Correct |
73 ms |
6736 KB |
Output is correct |
91 |
Correct |
66 ms |
6736 KB |
Output is correct |
92 |
Correct |
66 ms |
6748 KB |
Output is correct |