#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<typename K> using hset = gp_hash_table<K, null_type>;
template<typename K, typename V> using hmap = gp_hash_table<K, V>;
using namespace std;
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define smax(x, y) (x = max(x, y))
#define smin(x, y) (x = min(x, y))
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i,0,a)
#define ROF(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i, a) ROF(i,0,a)
using ll = long long;
using ld = long double;
template<typename T>
using vv = vector<vector<T>>;
using vi = vector<int>;
using ii = array<int, 2>;
using vii = vector<ii>;
using vvi = vv<int>;
using vll = vector<ll>;
using l2 = array<ll, 2>;
using vl2 = vector<l2>;
using vvll = vv<ll>;
template<typename T>
using minq = priority_queue<T, vector<T>, greater<T>>;
template<typename T>
using maxq = priority_queue<T>;
const ll M = 1000000007;
const ll infll = M * M;
template<typename IN>
IN discrete_binary_search(function<bool(IN)> predicate, IN low = 0, IN high = numeric_limits<IN>::max()) {
while (low < high) {
IN middle = low + (high - low) / 2; // todo std::midpoint in cpp 2020
if (predicate(middle))
high = middle;
else low = middle + 1;
}
return low;
}
vii dirs{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
vii adjc(int x, int y) {
vii res;
for (auto [dx, dy] : dirs)
res.pb({x + dx, y + dy});
return res;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, s;
cin >> n >> s;
vv<char> g(n, vector<char>(n));
int mr, mc, dr, dc;
vii hives;
F0R(i, n) {
F0R(j, n) {
char x;
cin >> x;
g[i][j] = x;
if (x == 'M') {
mr = i;
mc = j;
} else if (x == 'H') {
hives.pb({i, j});
} else if (x == 'D') {
dr = i;
dc = j;
}
}
}
vvi beetime(n, vi(n, M));
queue<ii> bees;
for (auto [r, c] : hives) {
bees.push({r, c});
beetime[r][c] = 0;
}
F0R(t, n * n) {
queue<ii> nextbees;
while (!bees.empty()) {
assert(bees.size() < n * n);
auto [r, c] = bees.front(); bees.pop();
for (auto [nr, nc] : adjc(r, c)) {
if (0 <= nr && nr < n && 0 <= nc && nc < n && beetime[nr][nc] == M && (g[nr][nc] == 'G' || g[nr][nc] == 'M')) {
nextbees.push({nr, nc});
beetime[nr][nc] = t + 1;
}
}
}
swap(bees, nextbees);
}
cout << discrete_binary_search<int>([&](int startt) -> bool {
vvi seen(n, vi(n));
queue<ii> bear;
bear.push({mr, mc});
seen[mr][mc] = 1;
FOR(t, startt, n * n) {
F0R(_, s) {
queue<ii> nextbear;
while (!bear.empty()) {
auto [r, c] = bear.front(); bear.pop();
if (r == dr && c == dc) return false;
if (beetime[r][c] == t) continue;
for (auto [nr, nc] : adjc(r, c)) {
if (0 <= nr && nr < n && 0 <= nc && nc < n && (g[nr][nc] == 'G' || g[nr][nc] == 'D') && seen[nr][nc] == 0 && beetime[nr][nc] > t) {
nextbear.push({nr, nc});
seen[nr][nc] = 1;
}
}
}
swap(bear, nextbear);
if (bear.empty()) return true;
}
}
return true;
}, 0, n * n) - 1 << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
397 ms |
6096 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
2 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
17 ms |
1468 KB |
Output is correct |
34 |
Correct |
19 ms |
1424 KB |
Output is correct |
35 |
Correct |
105 ms |
1428 KB |
Output is correct |
36 |
Correct |
36 ms |
1744 KB |
Output is correct |
37 |
Correct |
25 ms |
1740 KB |
Output is correct |
38 |
Correct |
123 ms |
1628 KB |
Output is correct |
39 |
Correct |
31 ms |
2120 KB |
Output is correct |
40 |
Correct |
29 ms |
2116 KB |
Output is correct |
41 |
Correct |
156 ms |
2132 KB |
Output is correct |
42 |
Correct |
36 ms |
2608 KB |
Output is correct |
43 |
Correct |
36 ms |
2552 KB |
Output is correct |
44 |
Correct |
182 ms |
2552 KB |
Output is correct |
45 |
Correct |
45 ms |
3024 KB |
Output is correct |
46 |
Correct |
52 ms |
2996 KB |
Output is correct |
47 |
Correct |
251 ms |
2996 KB |
Output is correct |
48 |
Correct |
48 ms |
3524 KB |
Output is correct |
49 |
Correct |
51 ms |
3604 KB |
Output is correct |
50 |
Correct |
343 ms |
3536 KB |
Output is correct |
51 |
Correct |
57 ms |
4084 KB |
Output is correct |
52 |
Correct |
61 ms |
4124 KB |
Output is correct |
53 |
Correct |
346 ms |
4196 KB |
Output is correct |
54 |
Correct |
71 ms |
4752 KB |
Output is correct |
55 |
Correct |
70 ms |
4728 KB |
Output is correct |
56 |
Correct |
404 ms |
4728 KB |
Output is correct |
57 |
Correct |
90 ms |
5376 KB |
Output is correct |
58 |
Correct |
89 ms |
5436 KB |
Output is correct |
59 |
Correct |
501 ms |
5388 KB |
Output is correct |
60 |
Correct |
95 ms |
6104 KB |
Output is correct |
61 |
Correct |
94 ms |
6076 KB |
Output is correct |
62 |
Correct |
559 ms |
6056 KB |
Output is correct |
63 |
Correct |
369 ms |
6060 KB |
Output is correct |
64 |
Correct |
527 ms |
6068 KB |
Output is correct |
65 |
Correct |
509 ms |
6052 KB |
Output is correct |
66 |
Correct |
421 ms |
6064 KB |
Output is correct |
67 |
Correct |
381 ms |
6052 KB |
Output is correct |
68 |
Correct |
172 ms |
6132 KB |
Output is correct |
69 |
Correct |
161 ms |
6096 KB |
Output is correct |
70 |
Correct |
141 ms |
6076 KB |
Output is correct |
71 |
Correct |
136 ms |
6076 KB |
Output is correct |
72 |
Correct |
113 ms |
6168 KB |
Output is correct |
73 |
Correct |
158 ms |
6188 KB |
Output is correct |
74 |
Correct |
244 ms |
6096 KB |
Output is correct |
75 |
Correct |
299 ms |
6076 KB |
Output is correct |
76 |
Correct |
319 ms |
6092 KB |
Output is correct |
77 |
Correct |
279 ms |
6100 KB |
Output is correct |
78 |
Correct |
366 ms |
6064 KB |
Output is correct |
79 |
Correct |
206 ms |
6104 KB |
Output is correct |
80 |
Correct |
322 ms |
6216 KB |
Output is correct |
81 |
Correct |
330 ms |
6076 KB |
Output is correct |
82 |
Correct |
285 ms |
6096 KB |
Output is correct |
83 |
Correct |
393 ms |
6112 KB |
Output is correct |
84 |
Correct |
303 ms |
6084 KB |
Output is correct |
85 |
Correct |
301 ms |
6116 KB |
Output is correct |
86 |
Correct |
310 ms |
6172 KB |
Output is correct |
87 |
Correct |
318 ms |
6076 KB |
Output is correct |
88 |
Correct |
398 ms |
6200 KB |
Output is correct |
89 |
Correct |
344 ms |
6084 KB |
Output is correct |
90 |
Correct |
390 ms |
6128 KB |
Output is correct |
91 |
Correct |
374 ms |
6096 KB |
Output is correct |
92 |
Correct |
375 ms |
6092 KB |
Output is correct |