#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];
int dist[800][800];
bool visBee[800][800];
int distBee[800][800];
bool blocked[800][800];
bool blocked1[800][800];
vector<pii> hives;
void bfsBees() {
queue<pii> bfsQueue;
for (pii root : hives) {
visBee[root.pA][root.pB] = true;
bfsQueue.push(root);
distBee[root.pA][root.pB] = 0;
}
while (!bfsQueue.empty()) {
pii node = bfsQueue.front();
bfsQueue.pop();
for (pii neighbor : adj[node.pA][node.pB]) {
if (!visBee[neighbor.pA][neighbor.pB] && !(neighbor.pA == homeX && neighbor.pB == homeY)) {
visBee[neighbor.pA][neighbor.pB] = true;
bfsQueue.push(neighbor);
distBee[neighbor.pA][neighbor.pB] = distBee[node.pA][node.pB] + 1;
}
}
}
}
bool bfs(int eatHoney) {
FOR(i, 0, n) {
FOR(j, 0, n) {
vis[i][j] = false;
}
}
queue<pii> bfsQueue;
vis[rootX][rootY] = true;
bfsQueue.push(pii(rootX, rootY));
dist[rootX][rootY] = 0;
if (distBee[rootX][rootY] <= eatHoney) {
bfsQueue.pop();
}
while (!bfsQueue.empty()) {
pii node = bfsQueue.front();
bfsQueue.pop();
for (pii neighbor : adj[node.pA][node.pB]) {
int dist1 = dist[node.pA][node.pB] + 1;
if (!vis[neighbor.pA][neighbor.pB] && dist1/steps < distBee[neighbor.pA][neighbor.pB] - eatHoney) {
vis[neighbor.pA][neighbor.pB] = true;
if (neighbor.pA == homeX && neighbor.pB == homeY) {
return true;
}
bfsQueue.push(neighbor);
dist[neighbor.pA][neighbor.pB] = dist1;
}
}
}
for (pii i : adj[homeX][homeY]) {
if (vis[i.pA][i.pB] && dist[i.pA][i.pB]/steps < distBee[i.pA][i.pB] - eatHoney) {
return true;
}
}
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() {
// freopen("!.in", "r", stdin);
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));
}
}
}
FOR(i, 0, n) {
FOR(j, 0, n) {
vis[i][j] = false;
visBee[i][j] = false;
dist[i][j] = 1e9;
distBee[i][j] = 1e9;
blocked1[i][j] = false;
}
}
gridAdj();
bfsBees();
int l = 0;
int r = 1e6;
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 |
7 ms |
15308 KB |
Output is correct |
2 |
Correct |
10 ms |
15308 KB |
Output is correct |
3 |
Correct |
9 ms |
15308 KB |
Output is correct |
4 |
Correct |
7 ms |
15308 KB |
Output is correct |
5 |
Correct |
8 ms |
15348 KB |
Output is correct |
6 |
Correct |
10 ms |
15348 KB |
Output is correct |
7 |
Correct |
415 ms |
53100 KB |
Output is correct |
8 |
Correct |
8 ms |
15436 KB |
Output is correct |
9 |
Correct |
8 ms |
15436 KB |
Output is correct |
10 |
Correct |
7 ms |
15436 KB |
Output is correct |
11 |
Correct |
8 ms |
15436 KB |
Output is correct |
12 |
Correct |
8 ms |
15992 KB |
Output is correct |
13 |
Correct |
10 ms |
15856 KB |
Output is correct |
14 |
Correct |
9 ms |
15980 KB |
Output is correct |
15 |
Correct |
7 ms |
15476 KB |
Output is correct |
16 |
Correct |
8 ms |
15436 KB |
Output is correct |
17 |
Correct |
8 ms |
15512 KB |
Output is correct |
18 |
Correct |
8 ms |
15564 KB |
Output is correct |
19 |
Correct |
8 ms |
15584 KB |
Output is correct |
20 |
Correct |
8 ms |
15604 KB |
Output is correct |
21 |
Correct |
7 ms |
15692 KB |
Output is correct |
22 |
Correct |
8 ms |
15692 KB |
Output is correct |
23 |
Correct |
8 ms |
15772 KB |
Output is correct |
24 |
Correct |
9 ms |
15888 KB |
Output is correct |
25 |
Correct |
9 ms |
15948 KB |
Output is correct |
26 |
Correct |
10 ms |
15948 KB |
Output is correct |
27 |
Correct |
8 ms |
15948 KB |
Output is correct |
28 |
Correct |
11 ms |
16108 KB |
Output is correct |
29 |
Correct |
9 ms |
15988 KB |
Output is correct |
30 |
Correct |
11 ms |
16000 KB |
Output is correct |
31 |
Correct |
9 ms |
16076 KB |
Output is correct |
32 |
Correct |
9 ms |
16076 KB |
Output is correct |
33 |
Correct |
15 ms |
20576 KB |
Output is correct |
34 |
Correct |
17 ms |
20688 KB |
Output is correct |
35 |
Correct |
67 ms |
22508 KB |
Output is correct |
36 |
Correct |
19 ms |
21708 KB |
Output is correct |
37 |
Correct |
21 ms |
21620 KB |
Output is correct |
38 |
Correct |
109 ms |
24212 KB |
Output is correct |
39 |
Correct |
22 ms |
22788 KB |
Output is correct |
40 |
Correct |
23 ms |
22888 KB |
Output is correct |
41 |
Correct |
128 ms |
26012 KB |
Output is correct |
42 |
Correct |
24 ms |
24064 KB |
Output is correct |
43 |
Correct |
26 ms |
24012 KB |
Output is correct |
44 |
Correct |
182 ms |
27900 KB |
Output is correct |
45 |
Correct |
28 ms |
25420 KB |
Output is correct |
46 |
Correct |
28 ms |
25316 KB |
Output is correct |
47 |
Correct |
218 ms |
30100 KB |
Output is correct |
48 |
Correct |
34 ms |
26780 KB |
Output is correct |
49 |
Correct |
32 ms |
26760 KB |
Output is correct |
50 |
Correct |
245 ms |
32248 KB |
Output is correct |
51 |
Correct |
34 ms |
28228 KB |
Output is correct |
52 |
Correct |
34 ms |
28100 KB |
Output is correct |
53 |
Correct |
315 ms |
34776 KB |
Output is correct |
54 |
Correct |
43 ms |
29700 KB |
Output is correct |
55 |
Correct |
42 ms |
29652 KB |
Output is correct |
56 |
Correct |
364 ms |
37352 KB |
Output is correct |
57 |
Correct |
44 ms |
31348 KB |
Output is correct |
58 |
Correct |
53 ms |
31252 KB |
Output is correct |
59 |
Correct |
426 ms |
40092 KB |
Output is correct |
60 |
Correct |
46 ms |
32964 KB |
Output is correct |
61 |
Correct |
55 ms |
32964 KB |
Output is correct |
62 |
Correct |
496 ms |
42928 KB |
Output is correct |
63 |
Correct |
229 ms |
36408 KB |
Output is correct |
64 |
Correct |
330 ms |
36420 KB |
Output is correct |
65 |
Correct |
325 ms |
36520 KB |
Output is correct |
66 |
Correct |
285 ms |
36648 KB |
Output is correct |
67 |
Correct |
232 ms |
36524 KB |
Output is correct |
68 |
Correct |
131 ms |
36456 KB |
Output is correct |
69 |
Correct |
129 ms |
36548 KB |
Output is correct |
70 |
Correct |
120 ms |
36556 KB |
Output is correct |
71 |
Correct |
102 ms |
36556 KB |
Output is correct |
72 |
Correct |
116 ms |
36460 KB |
Output is correct |
73 |
Correct |
213 ms |
52976 KB |
Output is correct |
74 |
Correct |
285 ms |
53044 KB |
Output is correct |
75 |
Correct |
300 ms |
52936 KB |
Output is correct |
76 |
Correct |
279 ms |
53048 KB |
Output is correct |
77 |
Correct |
298 ms |
53048 KB |
Output is correct |
78 |
Correct |
293 ms |
53028 KB |
Output is correct |
79 |
Correct |
256 ms |
53144 KB |
Output is correct |
80 |
Correct |
264 ms |
53032 KB |
Output is correct |
81 |
Correct |
287 ms |
52988 KB |
Output is correct |
82 |
Correct |
250 ms |
53236 KB |
Output is correct |
83 |
Correct |
370 ms |
52912 KB |
Output is correct |
84 |
Correct |
375 ms |
52820 KB |
Output is correct |
85 |
Correct |
353 ms |
52908 KB |
Output is correct |
86 |
Correct |
391 ms |
52916 KB |
Output is correct |
87 |
Correct |
354 ms |
52796 KB |
Output is correct |
88 |
Correct |
413 ms |
52836 KB |
Output is correct |
89 |
Correct |
401 ms |
52936 KB |
Output is correct |
90 |
Correct |
441 ms |
52900 KB |
Output is correct |
91 |
Correct |
382 ms |
52908 KB |
Output is correct |
92 |
Correct |
397 ms |
52936 KB |
Output is correct |