#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 + (level[start.first][start.second] + 1) / s > 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;
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 |
Incorrect |
0 ms |
2392 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
5 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
6 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
7 |
Incorrect |
68 ms |
6236 KB |
Output isn't correct |
8 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
9 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
11 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
12 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
14 |
Correct |
1 ms |
4700 KB |
Output is correct |
15 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
16 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
17 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
18 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
19 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
20 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
21 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
24 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
25 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
27 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
4696 KB |
Output isn't correct |
29 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
30 |
Incorrect |
1 ms |
4696 KB |
Output isn't correct |
31 |
Incorrect |
1 ms |
4696 KB |
Output isn't correct |
32 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
33 |
Incorrect |
10 ms |
4912 KB |
Output isn't correct |
34 |
Incorrect |
11 ms |
4700 KB |
Output isn't correct |
35 |
Incorrect |
15 ms |
4700 KB |
Output isn't correct |
36 |
Incorrect |
13 ms |
4700 KB |
Output isn't correct |
37 |
Incorrect |
14 ms |
4936 KB |
Output isn't correct |
38 |
Incorrect |
21 ms |
4940 KB |
Output isn't correct |
39 |
Incorrect |
15 ms |
4956 KB |
Output isn't correct |
40 |
Incorrect |
18 ms |
4960 KB |
Output isn't correct |
41 |
Incorrect |
31 ms |
4912 KB |
Output isn't correct |
42 |
Incorrect |
20 ms |
5212 KB |
Output isn't correct |
43 |
Incorrect |
26 ms |
5044 KB |
Output isn't correct |
44 |
Incorrect |
33 ms |
5212 KB |
Output isn't correct |
45 |
Incorrect |
24 ms |
5168 KB |
Output isn't correct |
46 |
Incorrect |
28 ms |
5208 KB |
Output isn't correct |
47 |
Incorrect |
39 ms |
5212 KB |
Output isn't correct |
48 |
Incorrect |
29 ms |
5208 KB |
Output isn't correct |
49 |
Incorrect |
35 ms |
5432 KB |
Output isn't correct |
50 |
Incorrect |
46 ms |
5212 KB |
Output isn't correct |
51 |
Incorrect |
33 ms |
5464 KB |
Output isn't correct |
52 |
Incorrect |
39 ms |
5468 KB |
Output isn't correct |
53 |
Incorrect |
54 ms |
5468 KB |
Output isn't correct |
54 |
Incorrect |
39 ms |
5724 KB |
Output isn't correct |
55 |
Incorrect |
45 ms |
5688 KB |
Output isn't correct |
56 |
Incorrect |
62 ms |
5720 KB |
Output isn't correct |
57 |
Incorrect |
46 ms |
5980 KB |
Output isn't correct |
58 |
Incorrect |
52 ms |
5868 KB |
Output isn't correct |
59 |
Incorrect |
72 ms |
5980 KB |
Output isn't correct |
60 |
Incorrect |
50 ms |
6232 KB |
Output isn't correct |
61 |
Incorrect |
58 ms |
6236 KB |
Output isn't correct |
62 |
Incorrect |
92 ms |
6236 KB |
Output isn't correct |
63 |
Incorrect |
78 ms |
6228 KB |
Output isn't correct |
64 |
Incorrect |
129 ms |
6072 KB |
Output isn't correct |
65 |
Incorrect |
108 ms |
6236 KB |
Output isn't correct |
66 |
Incorrect |
84 ms |
6236 KB |
Output isn't correct |
67 |
Correct |
82 ms |
6232 KB |
Output is correct |
68 |
Incorrect |
41 ms |
6224 KB |
Output isn't correct |
69 |
Incorrect |
36 ms |
6236 KB |
Output isn't correct |
70 |
Incorrect |
35 ms |
6236 KB |
Output isn't correct |
71 |
Incorrect |
33 ms |
6228 KB |
Output isn't correct |
72 |
Incorrect |
27 ms |
6228 KB |
Output isn't correct |
73 |
Incorrect |
37 ms |
6492 KB |
Output isn't correct |
74 |
Incorrect |
53 ms |
6488 KB |
Output isn't correct |
75 |
Incorrect |
60 ms |
6508 KB |
Output isn't correct |
76 |
Incorrect |
49 ms |
6492 KB |
Output isn't correct |
77 |
Incorrect |
47 ms |
6488 KB |
Output isn't correct |
78 |
Correct |
58 ms |
6492 KB |
Output is correct |
79 |
Incorrect |
45 ms |
6488 KB |
Output isn't correct |
80 |
Incorrect |
45 ms |
6236 KB |
Output isn't correct |
81 |
Incorrect |
56 ms |
6492 KB |
Output isn't correct |
82 |
Incorrect |
50 ms |
6488 KB |
Output isn't correct |
83 |
Incorrect |
63 ms |
6448 KB |
Output isn't correct |
84 |
Incorrect |
57 ms |
6232 KB |
Output isn't correct |
85 |
Incorrect |
59 ms |
6236 KB |
Output isn't correct |
86 |
Incorrect |
61 ms |
6236 KB |
Output isn't correct |
87 |
Incorrect |
59 ms |
6232 KB |
Output isn't correct |
88 |
Incorrect |
64 ms |
6232 KB |
Output isn't correct |
89 |
Incorrect |
64 ms |
6236 KB |
Output isn't correct |
90 |
Incorrect |
69 ms |
6232 KB |
Output isn't correct |
91 |
Incorrect |
70 ms |
6232 KB |
Output isn't correct |
92 |
Incorrect |
60 ms |
6232 KB |
Output isn't correct |