#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define f1 first
#define s2 second
using ii = pair<int, int>;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
const int MAX = 869;
const int inf = 1e9 + 69;
int N, S;
char s[MAX][MAX];
int dist[MAX][MAX];
inline bool valid(int r, int c)
{
return 0 <= r && r <= N && 0 <= c && c < N;
}
int main()
{
ios :: sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N >> S;
for (int i = 0; i < N; ++i)
cin >> s[i];
ii st, fn;
vector<ii> bee;
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < N; ++j)
{
if (s[i][j] == 'M')
st = {i, j};
else if (s[i][j] == 'H')
bee.pb({i, j});
else if (s[i][j] == 'D')
fn = {i, j};
}
}
const auto check = [&](int val) -> bool
{
queue<ii> q, qb; //bear, bee
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j)
dist[i][j] = -inf;
const auto bearMove = [&]() -> bool
{
while (!q.empty() && dist[q.front().f1][q.front().s2] < 0)
q.pop();
if (q.empty())
return false;
bool moved = false;
int cur = dist[q.front().f1][q.front().s2];
while (!q.empty())
{
auto [r, c] = q.front();
if (dist[r][c] >= cur + S)
break;
q.pop();
if (dist[r][c] < 0)
continue;
for (int i = 0; i < 4; ++i)
{
int nr = r + dx[i], nc = c + dy[i];
if (valid(nr, nc) && s[nr][nc] != 'T' && (dist[nr][nc] == -inf))
moved = true, dist[nr][nc] = dist[r][c] + 1, q.push({nr, nc});
}
}
return moved;
};
const auto beeSwarm = [&]() -> void
{
if (qb.empty())
return;
int cur = dist[qb.front().f1][qb.front().s2];
while (!qb.empty())
{
auto [r, c] = qb.front();
if (dist[r][c] != cur)
break;
qb.pop();
for (int i = 0; i < 4; ++i)
{
int nr = r + dx[i], nc = c + dy[i];
if (valid(nr, nc) && s[nr][nc] != 'T' && s[nr][nc] != 'D' && (dist[nr][nc] == -inf || dist[nr][nc] >= 0))
dist[nr][nc] = dist[r][c] - 1, qb.push({nr, nc});
}
}
};
dist[st.f1][st.s2] = 0;
q.push(st);
for (auto [r, c] : bee)
dist[r][c] = -1, qb.push({r, c});
for (int rep = 0; rep < val; ++rep)
beeSwarm();
while (dist[fn.f1][fn.s2] == -inf)
{
if (!bearMove())
return false;
beeSwarm();
// for (int i = 0; i < N; ++i)
// for (int j = 0; j < N; ++j)
// cerr << setw(2) << max(-9, dist[i][j]) << " \n"[j == N-1];
}
return true;
};
int l = -1, r = N*N;
while (l < r)
{
int mid = (l + r + 1) >> 1;
if (check(mid)) l = mid;
else r = mid - 1;
}
cout << l << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
289 ms |
4472 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
324 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
456 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
324 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
328 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
504 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
456 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
452 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
588 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
24 ms |
1936 KB |
Output is correct |
34 |
Correct |
22 ms |
1876 KB |
Output is correct |
35 |
Correct |
35 ms |
1876 KB |
Output is correct |
36 |
Correct |
36 ms |
2188 KB |
Output is correct |
37 |
Correct |
31 ms |
2132 KB |
Output is correct |
38 |
Correct |
45 ms |
2184 KB |
Output is correct |
39 |
Correct |
38 ms |
2440 KB |
Output is correct |
40 |
Correct |
38 ms |
2428 KB |
Output is correct |
41 |
Correct |
58 ms |
2388 KB |
Output is correct |
42 |
Correct |
50 ms |
2644 KB |
Output is correct |
43 |
Correct |
47 ms |
2712 KB |
Output is correct |
44 |
Correct |
79 ms |
2644 KB |
Output is correct |
45 |
Correct |
66 ms |
2956 KB |
Output is correct |
46 |
Correct |
58 ms |
2896 KB |
Output is correct |
47 |
Correct |
85 ms |
2956 KB |
Output is correct |
48 |
Correct |
72 ms |
3228 KB |
Output is correct |
49 |
Correct |
68 ms |
3228 KB |
Output is correct |
50 |
Correct |
107 ms |
3240 KB |
Output is correct |
51 |
Correct |
89 ms |
3496 KB |
Output is correct |
52 |
Correct |
81 ms |
3412 KB |
Output is correct |
53 |
Correct |
127 ms |
3508 KB |
Output is correct |
54 |
Correct |
107 ms |
3768 KB |
Output is correct |
55 |
Correct |
106 ms |
3792 KB |
Output is correct |
56 |
Correct |
149 ms |
3788 KB |
Output is correct |
57 |
Correct |
116 ms |
4060 KB |
Output is correct |
58 |
Correct |
108 ms |
4052 KB |
Output is correct |
59 |
Correct |
175 ms |
4188 KB |
Output is correct |
60 |
Correct |
142 ms |
4352 KB |
Output is correct |
61 |
Correct |
121 ms |
4308 KB |
Output is correct |
62 |
Correct |
194 ms |
4324 KB |
Output is correct |
63 |
Correct |
264 ms |
4480 KB |
Output is correct |
64 |
Correct |
282 ms |
4360 KB |
Output is correct |
65 |
Correct |
262 ms |
4428 KB |
Output is correct |
66 |
Correct |
278 ms |
4356 KB |
Output is correct |
67 |
Correct |
273 ms |
4308 KB |
Output is correct |
68 |
Correct |
294 ms |
4416 KB |
Output is correct |
69 |
Correct |
254 ms |
4272 KB |
Output is correct |
70 |
Correct |
260 ms |
4396 KB |
Output is correct |
71 |
Correct |
311 ms |
4384 KB |
Output is correct |
72 |
Correct |
321 ms |
4404 KB |
Output is correct |
73 |
Correct |
240 ms |
4776 KB |
Output is correct |
74 |
Correct |
284 ms |
4652 KB |
Output is correct |
75 |
Correct |
325 ms |
4684 KB |
Output is correct |
76 |
Correct |
295 ms |
4684 KB |
Output is correct |
77 |
Correct |
295 ms |
4568 KB |
Output is correct |
78 |
Correct |
332 ms |
4812 KB |
Output is correct |
79 |
Correct |
320 ms |
4568 KB |
Output is correct |
80 |
Correct |
316 ms |
4748 KB |
Output is correct |
81 |
Correct |
305 ms |
4564 KB |
Output is correct |
82 |
Correct |
297 ms |
4568 KB |
Output is correct |
83 |
Correct |
310 ms |
4684 KB |
Output is correct |
84 |
Correct |
304 ms |
4580 KB |
Output is correct |
85 |
Correct |
329 ms |
4576 KB |
Output is correct |
86 |
Correct |
297 ms |
4580 KB |
Output is correct |
87 |
Correct |
302 ms |
4564 KB |
Output is correct |
88 |
Correct |
299 ms |
4520 KB |
Output is correct |
89 |
Correct |
295 ms |
4520 KB |
Output is correct |
90 |
Correct |
319 ms |
4520 KB |
Output is correct |
91 |
Correct |
312 ms |
4528 KB |
Output is correct |
92 |
Correct |
310 ms |
4428 KB |
Output is correct |