#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int MAX_N = 800 + 5, INF = 1e9, MAX_T = MAX_N * MAX_N;
int n, s;
char arr[MAX_N][MAX_N];
bool bee_seen[MAX_N][MAX_N];
int bee_dist[MAX_N][MAX_N]; // INF if unreachable
queue<pii> bee_order;
void bee_bfs() {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++)
if (arr[i][j] == 'H') {
bee_seen[i][j] = true;
bee_order.push({i, j});
} else
bee_dist[i][j] = INF;
}
while (bee_order.size()) {
int i = bee_order.front().first, j = bee_order.front().second;
bee_order.pop();
vector<pii> delta = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
for (pii in : delta) {
int new_i = i + in.first, new_j = j + in.second;
if (new_i < 1 || new_i > n || new_j < 1 || new_j > n) continue;
if (arr[new_i][new_j] == 'T' || arr[new_i][new_j] == 'D') continue;
if (bee_seen[new_i][new_j]) continue;
bee_seen[new_i][new_j] = true;
bee_dist[new_i][new_j] = bee_dist[i][j] + 1;
bee_order.push({new_i, new_j});
}
}
}
bool check_seen[MAX_N][MAX_N];
int check_dist[MAX_N][MAX_N]; // INF if unreachable
queue<pii> check_order;
bool check(int t) {
pii finish = {0, 0};
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (arr[i][j] == 'M') {
if (bee_dist[i][j] < t) return false;
check_seen[i][j] = true;
check_dist[i][j] = 0;
check_order.push({i, j});
} else {
check_seen[i][j] = false;
check_dist[i][j] = INF;
}
if (arr[i][j] == 'D')
finish = {i, j};
}
}
while (check_order.size()) {
int i = check_order.front().first, j = check_order.front().second;
check_order.pop();
// cout << t << ": " << i << " " << j << endl;
vector<pii> delta = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
for (pii in : delta) {
int new_i = i + in.first, new_j = j + in.second;
if (new_i < 1 || new_i > n || new_j < 1 || new_j > n) continue;
if (arr[new_i][new_j] == 'T') continue;
if (check_seen[new_i][new_j]) continue;
int new_dist = check_dist[i][j] + 1;
if (bee_dist[new_i][new_j] < t + (new_dist / s)) continue;
check_dist[new_i][new_j] = new_dist;
check_seen[new_i][new_j] = true;
check_order.push({new_i, new_j});
}
}
return check_seen[finish.first][finish.second];
}
int bin_search() {
int lo = 1, hi = MAX_T;
while (lo != hi) {
int mid = ceil((lo + hi) / (double) 2);
if (check(mid)) lo = mid;
else hi = mid - 1;
}
if (check(lo)) return lo;
else return -1;
}
int main() {
// freopen("mecho.in", "r", stdin);
cin >> n >> s;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
cin >> arr[i][j];
bee_bfs();
int ans = bin_search();
if (ans != -1) ans--;
cout << ans << '\n';
// for (int i = 1; i <= n; i++) {
// for (int j = 1; j <= n; j++)
// cout << bee_dist[i][j] << " ";
// cout << endl;
// }
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
149 ms |
7320 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
2 ms |
4700 KB |
Output is correct |
12 |
Correct |
2 ms |
6748 KB |
Output is correct |
13 |
Correct |
1 ms |
4700 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
2 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
2 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4700 KB |
Output is correct |
22 |
Correct |
1 ms |
4700 KB |
Output is correct |
23 |
Correct |
2 ms |
4700 KB |
Output is correct |
24 |
Correct |
1 ms |
4696 KB |
Output is correct |
25 |
Correct |
2 ms |
6748 KB |
Output is correct |
26 |
Correct |
2 ms |
6748 KB |
Output is correct |
27 |
Correct |
2 ms |
6748 KB |
Output is correct |
28 |
Correct |
2 ms |
6748 KB |
Output is correct |
29 |
Correct |
2 ms |
6748 KB |
Output is correct |
30 |
Correct |
3 ms |
6748 KB |
Output is correct |
31 |
Correct |
2 ms |
7000 KB |
Output is correct |
32 |
Correct |
3 ms |
6748 KB |
Output is correct |
33 |
Correct |
11 ms |
7000 KB |
Output is correct |
34 |
Correct |
15 ms |
7004 KB |
Output is correct |
35 |
Correct |
38 ms |
7040 KB |
Output is correct |
36 |
Correct |
13 ms |
7004 KB |
Output is correct |
37 |
Correct |
17 ms |
7004 KB |
Output is correct |
38 |
Correct |
49 ms |
7080 KB |
Output is correct |
39 |
Correct |
16 ms |
7004 KB |
Output is correct |
40 |
Correct |
21 ms |
6960 KB |
Output is correct |
41 |
Correct |
63 ms |
7120 KB |
Output is correct |
42 |
Correct |
19 ms |
7004 KB |
Output is correct |
43 |
Correct |
24 ms |
7316 KB |
Output is correct |
44 |
Correct |
82 ms |
7160 KB |
Output is correct |
45 |
Correct |
24 ms |
7048 KB |
Output is correct |
46 |
Correct |
29 ms |
7004 KB |
Output is correct |
47 |
Correct |
91 ms |
7004 KB |
Output is correct |
48 |
Correct |
27 ms |
7020 KB |
Output is correct |
49 |
Correct |
34 ms |
7236 KB |
Output is correct |
50 |
Correct |
120 ms |
6996 KB |
Output is correct |
51 |
Correct |
31 ms |
7256 KB |
Output is correct |
52 |
Correct |
41 ms |
7512 KB |
Output is correct |
53 |
Correct |
163 ms |
7276 KB |
Output is correct |
54 |
Correct |
41 ms |
7312 KB |
Output is correct |
55 |
Correct |
47 ms |
7152 KB |
Output is correct |
56 |
Correct |
194 ms |
7140 KB |
Output is correct |
57 |
Correct |
46 ms |
7188 KB |
Output is correct |
58 |
Correct |
53 ms |
7572 KB |
Output is correct |
59 |
Correct |
204 ms |
7196 KB |
Output is correct |
60 |
Correct |
53 ms |
7260 KB |
Output is correct |
61 |
Correct |
59 ms |
7392 KB |
Output is correct |
62 |
Correct |
225 ms |
7396 KB |
Output is correct |
63 |
Correct |
179 ms |
7400 KB |
Output is correct |
64 |
Correct |
300 ms |
7392 KB |
Output is correct |
65 |
Correct |
259 ms |
7400 KB |
Output is correct |
66 |
Correct |
203 ms |
7392 KB |
Output is correct |
67 |
Correct |
186 ms |
7252 KB |
Output is correct |
68 |
Correct |
96 ms |
7420 KB |
Output is correct |
69 |
Correct |
88 ms |
7420 KB |
Output is correct |
70 |
Correct |
87 ms |
7424 KB |
Output is correct |
71 |
Correct |
80 ms |
7316 KB |
Output is correct |
72 |
Correct |
66 ms |
7200 KB |
Output is correct |
73 |
Correct |
75 ms |
7672 KB |
Output is correct |
74 |
Correct |
111 ms |
7620 KB |
Output is correct |
75 |
Correct |
141 ms |
7440 KB |
Output is correct |
76 |
Correct |
119 ms |
7496 KB |
Output is correct |
77 |
Correct |
120 ms |
7768 KB |
Output is correct |
78 |
Correct |
153 ms |
7652 KB |
Output is correct |
79 |
Correct |
107 ms |
7508 KB |
Output is correct |
80 |
Correct |
129 ms |
7652 KB |
Output is correct |
81 |
Correct |
129 ms |
7644 KB |
Output is correct |
82 |
Correct |
140 ms |
7648 KB |
Output is correct |
83 |
Correct |
144 ms |
7404 KB |
Output is correct |
84 |
Correct |
157 ms |
7680 KB |
Output is correct |
85 |
Correct |
137 ms |
7504 KB |
Output is correct |
86 |
Correct |
155 ms |
7504 KB |
Output is correct |
87 |
Correct |
161 ms |
7612 KB |
Output is correct |
88 |
Correct |
161 ms |
7552 KB |
Output is correct |
89 |
Correct |
167 ms |
7376 KB |
Output is correct |
90 |
Correct |
169 ms |
7504 KB |
Output is correct |
91 |
Correct |
166 ms |
7556 KB |
Output is correct |
92 |
Correct |
155 ms |
7368 KB |
Output is correct |