#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define F0R1(i, a) for (int i = 1; i <= (a); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)
#define F0Rd1(i, a) for (int i = a; i > 0; i--)
#define SORT(vec) sort(vec.begin(), vec.end())
#define S0RT(arr, n) sort(arr, arr + n)
#define pA first
#define pB second
#define MOD 1000000007
#define nl "\n"
#define pb push_back
int n, steps, rootX, rootY, homeX, homeY;
vector<pii> adj[800][800];
bool vis[800][800];
bool visBee[800][800];
bool blocked[800][800];
vector<pii> hives;
queue<pii> beeQueue;
int initialDist = 0;
void moveBees() {
int sz = beeQueue.size();
FOR(i, 0, sz) {
pii node = beeQueue.front();
beeQueue.pop();
for (pii neighbor : adj[node.pA][node.pB]) {
if (neighbor.pA == homeX && neighbor.pB == homeY) {
continue;
}
if (!visBee[neighbor.pA][neighbor.pB]) {
visBee[neighbor.pA][neighbor.pB] = true;
beeQueue.push(neighbor);
}
}
}
}
bool bfs(int eatHoney) {
FOR(i, 0, n) {
FOR(j, 0, n) {
vis[i][j] = false;
visBee[i][j] = false;
}
}
queue<pii> bfsQueue;
beeQueue = queue<pii>();
vis[rootX][rootY] = true;
bfsQueue.push(pii(rootX, rootY));
for (pii i : hives) {
visBee[i.pA][i.pB] = true;
beeQueue.push(i);
}
FOR(i, 0, eatHoney) { moveBees(); }
// somehow avoid having to replace the queue with every step
while (!bfsQueue.empty()) {
FOR(i, 0, steps) {
int sz = bfsQueue.size();
FOR(j, 0, sz) {
pii node = bfsQueue.front();
bfsQueue.pop();
if (visBee[node.pA][node.pB]) {
continue;
}
for (pii neighbor : adj[node.pA][node.pB]) {
if (!vis[neighbor.pA][neighbor.pB] && !visBee[neighbor.pA][neighbor.pB]) {
vis[neighbor.pA][neighbor.pB] = true;
if (neighbor.pA == homeX && neighbor.pB == homeY) {
return true;
}
bfsQueue.push(neighbor);
}
}
}
}
moveBees();
}
return false;
}
void gridAdj() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (!blocked[i][j]) {
if (i > 0 && !blocked[i - 1][j]) {
adj[i][j].pb(pii(i - 1, j));
}
if (i < n - 1 && !blocked[i + 1][j]) {
adj[i][j].pb(pii(i + 1, j));
}
if (j > 0 && !blocked[i][j - 1]) {
adj[i][j].pb(pii(i, j - 1));
}
if (j < n - 1 && !blocked[i][j + 1]) {
adj[i][j].pb(pii(i, j + 1));
}
}
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> steps;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
FOR(j, 0, n) {
if (s[j] == 'T') {
blocked[i][j] = true;
} else if (s[j] == 'M') {
rootX = i;
rootY = j;
} else if (s[j] == 'D') {
homeX = i;
homeY = j;
} else if (s[j] == 'H') {
hives.pb(pii(i, j));
}
}
}
gridAdj();
int l = 0;
int r = 1e5;
int ans = l - 1;
while (l <= r) {
ll mid = (r + l) / 2;
if (bfs(mid)) {
ans = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
cout << ans << nl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15308 KB |
Output is correct |
2 |
Correct |
8 ms |
15308 KB |
Output is correct |
3 |
Correct |
8 ms |
15360 KB |
Output is correct |
4 |
Correct |
8 ms |
15280 KB |
Output is correct |
5 |
Correct |
8 ms |
15384 KB |
Output is correct |
6 |
Correct |
8 ms |
15308 KB |
Output is correct |
7 |
Execution timed out |
1095 ms |
47328 KB |
Time limit exceeded |
8 |
Correct |
8 ms |
15308 KB |
Output is correct |
9 |
Correct |
8 ms |
15392 KB |
Output is correct |
10 |
Correct |
8 ms |
15308 KB |
Output is correct |
11 |
Correct |
8 ms |
15308 KB |
Output is correct |
12 |
Correct |
9 ms |
15560 KB |
Output is correct |
13 |
Correct |
9 ms |
15564 KB |
Output is correct |
14 |
Correct |
9 ms |
15564 KB |
Output is correct |
15 |
Correct |
8 ms |
15308 KB |
Output is correct |
16 |
Correct |
8 ms |
15308 KB |
Output is correct |
17 |
Correct |
8 ms |
15308 KB |
Output is correct |
18 |
Correct |
8 ms |
15308 KB |
Output is correct |
19 |
Correct |
8 ms |
15436 KB |
Output is correct |
20 |
Correct |
9 ms |
15436 KB |
Output is correct |
21 |
Correct |
9 ms |
15448 KB |
Output is correct |
22 |
Correct |
8 ms |
15436 KB |
Output is correct |
23 |
Correct |
9 ms |
15436 KB |
Output is correct |
24 |
Correct |
9 ms |
15436 KB |
Output is correct |
25 |
Correct |
8 ms |
15564 KB |
Output is correct |
26 |
Correct |
10 ms |
15436 KB |
Output is correct |
27 |
Correct |
9 ms |
15564 KB |
Output is correct |
28 |
Correct |
9 ms |
15564 KB |
Output is correct |
29 |
Correct |
9 ms |
15564 KB |
Output is correct |
30 |
Correct |
9 ms |
15564 KB |
Output is correct |
31 |
Correct |
9 ms |
15564 KB |
Output is correct |
32 |
Correct |
9 ms |
15564 KB |
Output is correct |
33 |
Correct |
31 ms |
17996 KB |
Output is correct |
34 |
Correct |
32 ms |
17996 KB |
Output is correct |
35 |
Correct |
83 ms |
19980 KB |
Output is correct |
36 |
Correct |
40 ms |
18792 KB |
Output is correct |
37 |
Correct |
38 ms |
18764 KB |
Output is correct |
38 |
Correct |
161 ms |
21264 KB |
Output is correct |
39 |
Incorrect |
47 ms |
19592 KB |
Output isn't correct |
40 |
Correct |
46 ms |
19584 KB |
Output is correct |
41 |
Correct |
213 ms |
22704 KB |
Output is correct |
42 |
Incorrect |
49 ms |
20452 KB |
Output isn't correct |
43 |
Correct |
58 ms |
20428 KB |
Output is correct |
44 |
Correct |
273 ms |
24312 KB |
Output is correct |
45 |
Incorrect |
52 ms |
21404 KB |
Output isn't correct |
46 |
Correct |
67 ms |
21364 KB |
Output is correct |
47 |
Correct |
344 ms |
25980 KB |
Output is correct |
48 |
Incorrect |
56 ms |
22340 KB |
Output isn't correct |
49 |
Correct |
80 ms |
22416 KB |
Output is correct |
50 |
Correct |
412 ms |
27952 KB |
Output is correct |
51 |
Incorrect |
64 ms |
23536 KB |
Output isn't correct |
52 |
Incorrect |
89 ms |
23496 KB |
Output isn't correct |
53 |
Correct |
499 ms |
30060 KB |
Output is correct |
54 |
Incorrect |
64 ms |
24696 KB |
Output isn't correct |
55 |
Incorrect |
90 ms |
24644 KB |
Output isn't correct |
56 |
Correct |
582 ms |
32196 KB |
Output is correct |
57 |
Incorrect |
66 ms |
25968 KB |
Output isn't correct |
58 |
Incorrect |
95 ms |
25932 KB |
Output isn't correct |
59 |
Correct |
684 ms |
34608 KB |
Output is correct |
60 |
Incorrect |
72 ms |
27228 KB |
Output isn't correct |
61 |
Incorrect |
96 ms |
27200 KB |
Output isn't correct |
62 |
Correct |
825 ms |
37248 KB |
Output is correct |
63 |
Correct |
441 ms |
30660 KB |
Output is correct |
64 |
Correct |
440 ms |
30788 KB |
Output is correct |
65 |
Correct |
456 ms |
30660 KB |
Output is correct |
66 |
Correct |
494 ms |
30876 KB |
Output is correct |
67 |
Correct |
462 ms |
30828 KB |
Output is correct |
68 |
Correct |
583 ms |
30804 KB |
Output is correct |
69 |
Correct |
484 ms |
30800 KB |
Output is correct |
70 |
Correct |
540 ms |
30916 KB |
Output is correct |
71 |
Correct |
554 ms |
30732 KB |
Output is correct |
72 |
Correct |
614 ms |
30792 KB |
Output is correct |
73 |
Execution timed out |
1091 ms |
47288 KB |
Time limit exceeded |
74 |
Execution timed out |
1084 ms |
47256 KB |
Time limit exceeded |
75 |
Execution timed out |
1091 ms |
47468 KB |
Time limit exceeded |
76 |
Execution timed out |
1096 ms |
47228 KB |
Time limit exceeded |
77 |
Execution timed out |
1094 ms |
47172 KB |
Time limit exceeded |
78 |
Execution timed out |
1053 ms |
47352 KB |
Time limit exceeded |
79 |
Execution timed out |
1066 ms |
47268 KB |
Time limit exceeded |
80 |
Execution timed out |
1048 ms |
47344 KB |
Time limit exceeded |
81 |
Execution timed out |
1067 ms |
47272 KB |
Time limit exceeded |
82 |
Execution timed out |
1090 ms |
47180 KB |
Time limit exceeded |
83 |
Execution timed out |
1097 ms |
47084 KB |
Time limit exceeded |
84 |
Execution timed out |
1090 ms |
47064 KB |
Time limit exceeded |
85 |
Execution timed out |
1089 ms |
47044 KB |
Time limit exceeded |
86 |
Execution timed out |
1093 ms |
47260 KB |
Time limit exceeded |
87 |
Execution timed out |
1070 ms |
47084 KB |
Time limit exceeded |
88 |
Execution timed out |
1093 ms |
47096 KB |
Time limit exceeded |
89 |
Execution timed out |
1049 ms |
47300 KB |
Time limit exceeded |
90 |
Execution timed out |
1063 ms |
47052 KB |
Time limit exceeded |
91 |
Execution timed out |
1058 ms |
47172 KB |
Time limit exceeded |
92 |
Execution timed out |
1081 ms |
47128 KB |
Time limit exceeded |