// oh, these hills, they burn so bright / oh, these hills, they bring me life
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
#define all(x) begin(x), end(x)
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)(x.size())
#define inf 1000000010
#define linf 0x3f3f3f3f3f3f3f3f
#define mp make_pair
#define f first
#define s second
#define pi pair<int, int>
#ifdef LOCAL
#include "/mnt/c/yukon/pp.hpp"
#else
#define endl '\n'
#endif
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native")
struct Event {
pi loc;
int dist;
};
int grid[801][801], dist_bees[801][801];
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
int main() {
cin.tie(0)->sync_with_stdio(0);
int n;
cin >> n;
int S;
cin >> S;
vector<pi> hives;
pi start, end;
for (int i = 0; i < n; i++) {
string st;
cin >> st;
for (int j = 0; j < n; j++) {
if (st[j] == 'T')
grid[i][j] = -1;
else if (st[j] != 'D')
grid[i][j] = 0;
else
grid[i][j] = 1, end = {i, j};
if (st[j] == 'H') {
hives.emplace_back(i, j);
} else if (st[j] == 'M')
start = {i, j};
}
}
// let's get distance from bees first
queue<Event> bfs;
memset(dist_bees, 0x3f, sizeof dist_bees);
for (auto &[i, j] : hives) {
bfs.push({{i, j}, 0});
dist_bees[i][j] = 0;
}
while (!bfs.empty()) {
auto [i, j] = bfs.front().loc;
int dist = bfs.front().dist;
bfs.pop();
if (dist_bees[i][j] < dist)
continue;
for (int d = 0; d < 4; d++) {
int new_i = i + dx[d], new_j = j + dy[d];
if (new_i >= 0 && new_i < n && new_j >= 0 && new_j < n) {
if (grid[new_i][new_j] == 0 && dist_bees[new_i][new_j] > dist + 1) {
dist_bees[new_i][new_j] = dist + 1;
bfs.push({{new_i, new_j}, dist + 1});
}
}
}
}
// for (int i = 0; i < n; i++) {
// cout << dist_bees[i] << endl;
// }
// now mecho's turn
bfs.push({{start.f, start.s}, 0});
// just store distance w/o S factor
vector<vector<int>> mecho_dist(n, vector<int>(n, -inf));
mecho_dist[start.f][start.s] = dist_bees[start.f][start.s];
while (sz(bfs)) {
auto [i, j] = bfs.front().loc;
int dist = bfs.front().dist;
bfs.pop();
if (mecho_dist[i][j] > dist_bees[i][j] - (dist) / S)
continue;
int elapsed = (dist + 1) / S;
for (int d = 0; d < 4; d++) {
int ni = i + dx[d], nj = j + dy[d];
if (ni < 0 || ni >= n || nj < 0 || nj >= n)
continue;
// if (ni == 3 && nj == 3) {
// cout << dist + 1 << ' ' << elapsed << ' ' << dist_bees[ni][nj] << endl;
// }
int lead = min(mecho_dist[i][j], dist_bees[ni][nj] - elapsed);
if (grid[ni][nj] == 0 && mecho_dist[ni][nj] < lead && elapsed < dist_bees[ni][nj]) {
mecho_dist[ni][nj] = lead;
bfs.push({{ni, nj}, dist + 1});
}
}
}
// cout << "________" << endl;
// for (int i = 0; i < n; i++) {
// cout << mecho_dist[i] << endl;
// }
// cout << "________" << endl;
// for (int i = 0; i < n; i++) {
// cout << mecho_lead[i] << endl;
// }
int ans = -inf;
for (int d = 0; d < 4; d++) {
int ni = end.f + dx[d], nj = end.s + dy[d];
if (ni < 0 || ni >= n || nj < 0 || nj >= n)
continue;
if (grid[ni][nj] == -1 || mecho_dist[ni][nj] == inf)
continue;
ans = max(ans, mecho_dist[ni][nj] - 1);
// int elapsed = (mecho_dist[ni][nj] + S) / S;
// ans = max(ans, dist_bees[ni][nj] - elapsed);
}
if (ans < 0) {
cout << -1 << endl;
} else {
cout << ans << endl;
}
}
// don't get stuck on one approach
// question bounds
// flesh out your approach before implementing o.o
// math it out
// ok well X is always possible, how about X + 1 (etc.)
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
29 ms |
7980 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4700 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
4556 KB |
Output is correct |
14 |
Correct |
1 ms |
4700 KB |
Output is correct |
15 |
Correct |
1 ms |
4700 KB |
Output is correct |
16 |
Correct |
1 ms |
4700 KB |
Output is correct |
17 |
Correct |
1 ms |
4700 KB |
Output is correct |
18 |
Correct |
1 ms |
4700 KB |
Output is correct |
19 |
Correct |
1 ms |
4700 KB |
Output is correct |
20 |
Correct |
1 ms |
4696 KB |
Output is correct |
21 |
Correct |
1 ms |
4564 KB |
Output is correct |
22 |
Correct |
1 ms |
4728 KB |
Output is correct |
23 |
Correct |
1 ms |
4700 KB |
Output is correct |
24 |
Correct |
1 ms |
4700 KB |
Output is correct |
25 |
Correct |
1 ms |
4700 KB |
Output is correct |
26 |
Correct |
1 ms |
4700 KB |
Output is correct |
27 |
Correct |
1 ms |
4700 KB |
Output is correct |
28 |
Correct |
1 ms |
4700 KB |
Output is correct |
29 |
Correct |
1 ms |
4700 KB |
Output is correct |
30 |
Correct |
1 ms |
4700 KB |
Output is correct |
31 |
Correct |
1 ms |
4696 KB |
Output is correct |
32 |
Correct |
1 ms |
4700 KB |
Output is correct |
33 |
Correct |
3 ms |
5212 KB |
Output is correct |
34 |
Correct |
3 ms |
5076 KB |
Output is correct |
35 |
Correct |
105 ms |
5336 KB |
Output is correct |
36 |
Correct |
3 ms |
5212 KB |
Output is correct |
37 |
Correct |
4 ms |
5212 KB |
Output is correct |
38 |
Correct |
158 ms |
5712 KB |
Output is correct |
39 |
Correct |
4 ms |
5468 KB |
Output is correct |
40 |
Correct |
4 ms |
5468 KB |
Output is correct |
41 |
Correct |
224 ms |
5656 KB |
Output is correct |
42 |
Correct |
4 ms |
5724 KB |
Output is correct |
43 |
Correct |
5 ms |
5752 KB |
Output is correct |
44 |
Correct |
319 ms |
6228 KB |
Output is correct |
45 |
Correct |
7 ms |
5724 KB |
Output is correct |
46 |
Correct |
6 ms |
5724 KB |
Output is correct |
47 |
Correct |
405 ms |
6248 KB |
Output is correct |
48 |
Correct |
6 ms |
6236 KB |
Output is correct |
49 |
Correct |
7 ms |
6236 KB |
Output is correct |
50 |
Correct |
535 ms |
6768 KB |
Output is correct |
51 |
Correct |
7 ms |
6492 KB |
Output is correct |
52 |
Correct |
9 ms |
6492 KB |
Output is correct |
53 |
Correct |
709 ms |
7140 KB |
Output is correct |
54 |
Correct |
8 ms |
7004 KB |
Output is correct |
55 |
Correct |
10 ms |
7004 KB |
Output is correct |
56 |
Correct |
860 ms |
8024 KB |
Output is correct |
57 |
Correct |
11 ms |
7512 KB |
Output is correct |
58 |
Correct |
11 ms |
7396 KB |
Output is correct |
59 |
Execution timed out |
1054 ms |
8504 KB |
Time limit exceeded |
60 |
Correct |
10 ms |
8040 KB |
Output is correct |
61 |
Correct |
14 ms |
8028 KB |
Output is correct |
62 |
Execution timed out |
1093 ms |
8616 KB |
Time limit exceeded |
63 |
Correct |
27 ms |
8028 KB |
Output is correct |
64 |
Correct |
30 ms |
8028 KB |
Output is correct |
65 |
Correct |
30 ms |
8028 KB |
Output is correct |
66 |
Correct |
27 ms |
8028 KB |
Output is correct |
67 |
Correct |
23 ms |
7812 KB |
Output is correct |
68 |
Correct |
22 ms |
8028 KB |
Output is correct |
69 |
Correct |
26 ms |
8028 KB |
Output is correct |
70 |
Correct |
28 ms |
7856 KB |
Output is correct |
71 |
Correct |
20 ms |
8028 KB |
Output is correct |
72 |
Correct |
23 ms |
8028 KB |
Output is correct |
73 |
Correct |
18 ms |
8028 KB |
Output is correct |
74 |
Correct |
42 ms |
8080 KB |
Output is correct |
75 |
Correct |
18 ms |
8028 KB |
Output is correct |
76 |
Correct |
20 ms |
7908 KB |
Output is correct |
77 |
Correct |
20 ms |
8028 KB |
Output is correct |
78 |
Correct |
20 ms |
7928 KB |
Output is correct |
79 |
Correct |
220 ms |
8532 KB |
Output is correct |
80 |
Correct |
32 ms |
8028 KB |
Output is correct |
81 |
Correct |
20 ms |
8028 KB |
Output is correct |
82 |
Correct |
63 ms |
8140 KB |
Output is correct |
83 |
Correct |
22 ms |
7880 KB |
Output is correct |
84 |
Correct |
75 ms |
8028 KB |
Output is correct |
85 |
Correct |
103 ms |
8088 KB |
Output is correct |
86 |
Correct |
135 ms |
8124 KB |
Output is correct |
87 |
Correct |
35 ms |
8028 KB |
Output is correct |
88 |
Correct |
76 ms |
8172 KB |
Output is correct |
89 |
Correct |
258 ms |
8316 KB |
Output is correct |
90 |
Correct |
28 ms |
8024 KB |
Output is correct |
91 |
Correct |
39 ms |
8028 KB |
Output is correct |
92 |
Correct |
26 ms |
8028 KB |
Output is correct |