#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;
}
}
}
cout << discrete_binary_search<int>([&](int t) -> bool {
queue<ii> bees, bear;
vvi seenbees(n, vi(n)), seenbear(n, vi(n));
for (auto [r, c] : hives) {
bees.push({r, c});
seenbees[r][c] = 1;
}
bear.push({mr, mc});
seenbear[mr][mc] = 1;
auto upd_bees = [&]() -> void {
queue<ii> nextbees;
while (!bees.empty()) {
cout << bees.size() + nextbees.size() << '\n';
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 && seenbees[nr][nc] == 0 && (g[nr][nc] == 'G' || g[nr][nc] == 'M')) {
nextbees.push({nr, nc});
seenbees[nr][nc] = 1;
}
}
}
swap(bees, nextbees);
// bees = nextbees;
};
F0R(_, t) {
upd_bees();
if (bees.empty()) break;
}
while (!bees.empty()) {
F0R(_, s) {
queue<ii> nextbear;
while (!bear.empty()) {
assert(bear.size() < n * n);
auto [r, c] = bear.front(); bear.pop();
if (seenbees[r][c] == 1) continue;
if (r == dr && c == dc) return false;
for (auto [nr, nc] : adjc(r, c)) {
if (0 <= nr && nr < n && 0 <= nc && nc < n && seenbear[nr][nc] == 0 && (g[nr][nc] == 'G' || g[nr][nc] == 'D') && seenbees[nr][nc] == 0) {
nextbear.push({nr, nc});
seenbear[nr][nc] = 1;
}
}
}
swap(bear, nextbear);
if (bear.empty()) return true;
// bear = nextbear;
}
upd_bees();
}
return true;
}, n*n / 7, n * n / 7) - 1 << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
5 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
6 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
7 |
Incorrect |
13 ms |
1576 KB |
Output isn't correct |
8 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
9 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
10 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
11 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
12 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
14 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
15 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
16 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
17 |
Incorrect |
0 ms |
316 KB |
Output isn't correct |
18 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
19 |
Incorrect |
0 ms |
312 KB |
Output isn't correct |
20 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
21 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
308 KB |
Output isn't correct |
24 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
25 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
27 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
29 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
30 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
31 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
32 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
33 |
Incorrect |
3 ms |
460 KB |
Output isn't correct |
34 |
Incorrect |
2 ms |
452 KB |
Output isn't correct |
35 |
Incorrect |
3 ms |
460 KB |
Output isn't correct |
36 |
Incorrect |
3 ms |
588 KB |
Output isn't correct |
37 |
Incorrect |
4 ms |
584 KB |
Output isn't correct |
38 |
Incorrect |
3 ms |
588 KB |
Output isn't correct |
39 |
Incorrect |
3 ms |
716 KB |
Output isn't correct |
40 |
Incorrect |
3 ms |
716 KB |
Output isn't correct |
41 |
Incorrect |
4 ms |
716 KB |
Output isn't correct |
42 |
Incorrect |
4 ms |
716 KB |
Output isn't correct |
43 |
Incorrect |
5 ms |
716 KB |
Output isn't correct |
44 |
Incorrect |
4 ms |
716 KB |
Output isn't correct |
45 |
Incorrect |
5 ms |
844 KB |
Output isn't correct |
46 |
Incorrect |
7 ms |
844 KB |
Output isn't correct |
47 |
Incorrect |
8 ms |
844 KB |
Output isn't correct |
48 |
Incorrect |
6 ms |
1020 KB |
Output isn't correct |
49 |
Incorrect |
6 ms |
964 KB |
Output isn't correct |
50 |
Incorrect |
7 ms |
972 KB |
Output isn't correct |
51 |
Incorrect |
8 ms |
1100 KB |
Output isn't correct |
52 |
Incorrect |
7 ms |
1100 KB |
Output isn't correct |
53 |
Incorrect |
8 ms |
1156 KB |
Output isn't correct |
54 |
Incorrect |
8 ms |
1228 KB |
Output isn't correct |
55 |
Incorrect |
9 ms |
1228 KB |
Output isn't correct |
56 |
Incorrect |
9 ms |
1228 KB |
Output isn't correct |
57 |
Incorrect |
9 ms |
1356 KB |
Output isn't correct |
58 |
Incorrect |
10 ms |
1348 KB |
Output isn't correct |
59 |
Incorrect |
9 ms |
1344 KB |
Output isn't correct |
60 |
Incorrect |
11 ms |
1568 KB |
Output isn't correct |
61 |
Incorrect |
11 ms |
1564 KB |
Output isn't correct |
62 |
Incorrect |
11 ms |
1448 KB |
Output isn't correct |
63 |
Incorrect |
12 ms |
1584 KB |
Output isn't correct |
64 |
Incorrect |
11 ms |
1576 KB |
Output isn't correct |
65 |
Incorrect |
13 ms |
1484 KB |
Output isn't correct |
66 |
Incorrect |
12 ms |
1484 KB |
Output isn't correct |
67 |
Incorrect |
11 ms |
1484 KB |
Output isn't correct |
68 |
Incorrect |
11 ms |
1484 KB |
Output isn't correct |
69 |
Incorrect |
11 ms |
1484 KB |
Output isn't correct |
70 |
Incorrect |
10 ms |
1472 KB |
Output isn't correct |
71 |
Incorrect |
10 ms |
1580 KB |
Output isn't correct |
72 |
Incorrect |
13 ms |
1580 KB |
Output isn't correct |
73 |
Incorrect |
11 ms |
1612 KB |
Output isn't correct |
74 |
Incorrect |
11 ms |
1604 KB |
Output isn't correct |
75 |
Incorrect |
12 ms |
1604 KB |
Output isn't correct |
76 |
Incorrect |
12 ms |
1604 KB |
Output isn't correct |
77 |
Incorrect |
12 ms |
1608 KB |
Output isn't correct |
78 |
Incorrect |
11 ms |
1608 KB |
Output isn't correct |
79 |
Incorrect |
10 ms |
1484 KB |
Output isn't correct |
80 |
Incorrect |
11 ms |
1532 KB |
Output isn't correct |
81 |
Incorrect |
11 ms |
1604 KB |
Output isn't correct |
82 |
Incorrect |
13 ms |
1616 KB |
Output isn't correct |
83 |
Incorrect |
11 ms |
1580 KB |
Output isn't correct |
84 |
Incorrect |
11 ms |
1588 KB |
Output isn't correct |
85 |
Incorrect |
10 ms |
1484 KB |
Output isn't correct |
86 |
Incorrect |
11 ms |
1592 KB |
Output isn't correct |
87 |
Incorrect |
10 ms |
1484 KB |
Output isn't correct |
88 |
Incorrect |
12 ms |
1484 KB |
Output isn't correct |
89 |
Incorrect |
10 ms |
1568 KB |
Output isn't correct |
90 |
Incorrect |
12 ms |
1472 KB |
Output isn't correct |
91 |
Incorrect |
10 ms |
1588 KB |
Output isn't correct |
92 |
Incorrect |
12 ms |
1484 KB |
Output isn't correct |