#include "bits/stdc++.h"
using namespace std;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, s;
cin >> n >> s;
vector<vector<char>> ar(n, vector<char>(n));
pair<int, int> start, end;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
cin >> ar[i][j];
if (ar[i][j] == 'M')
start = {i, j};
else if (ar[i][j] == 'D')
end = {i, j};
}
auto bee_dist_gen = [&]() {
vector<vector<int>> dist(n, vector<int>(n, 1e9));
queue<pair<int, int>> q;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (ar[i][j] == 'H')
q.push({i, j}), dist[i][j] = 0;
while (!q.empty()) {
pair<int, int> v = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int nx = v.first + dx[i], ny = v.second + dy[i];
if (nx < 0 || ny < 0 || nx >= n || ny >= n) continue;
if (ar[nx][ny] == 'T' || ar[nx][ny] == 'D') continue;
if (dist[nx][ny] <= dist[v.first][v.second] + 1) continue;
dist[nx][ny] = dist[v.first][v.second] + 1;
q.push({nx, ny});
}
}
return dist;
};
vector<vector<int>> bee_dist = bee_dist_gen();
auto test_honey = [&](int honey) {
vector<vector<int>> dist(n, vector<int>(n, 1e9));
dist[start.first][start.second] = 0;
queue<pair<int, int>> q;
q.push(start);
while (!q.empty()) {
pair<int, int> v = q.front();
q.pop();
if (bee_dist[v.first][v.second] <= honey + dist[v.first][v.second] / s) continue;
if (end == v) return true;
for (int i = 0; i < 4; i++) {
int nx = v.first + dx[i], ny = v.second + dy[i];
if (nx < 0 || ny < 0 || nx >= n || ny >= n) continue;
if (ar[nx][ny] == 'T') continue;
if (dist[nx][ny] <= dist[v.first][v.second] + 1) continue;
dist[nx][ny] = dist[v.first][v.second] + 1;
q.push({nx, ny});
}
}
return false;
};
int l = 0, r = 1e9;
while (l < r) {
int mid = l + (r - l + 1) / 2;
if (test_honey(mid))
l = mid;
else
r = mid - 1;
}
if (test_honey(l))
cout << l << '\n';
else
cout << -1 << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
114 ms |
6144 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
7 ms |
1428 KB |
Output is correct |
34 |
Correct |
8 ms |
1544 KB |
Output is correct |
35 |
Correct |
31 ms |
1516 KB |
Output is correct |
36 |
Correct |
9 ms |
1772 KB |
Output is correct |
37 |
Correct |
9 ms |
1652 KB |
Output is correct |
38 |
Correct |
33 ms |
1780 KB |
Output is correct |
39 |
Correct |
12 ms |
2148 KB |
Output is correct |
40 |
Correct |
12 ms |
2224 KB |
Output is correct |
41 |
Correct |
48 ms |
2148 KB |
Output is correct |
42 |
Correct |
15 ms |
2572 KB |
Output is correct |
43 |
Correct |
14 ms |
2580 KB |
Output is correct |
44 |
Correct |
61 ms |
2648 KB |
Output is correct |
45 |
Correct |
19 ms |
3040 KB |
Output is correct |
46 |
Correct |
19 ms |
3144 KB |
Output is correct |
47 |
Correct |
66 ms |
3060 KB |
Output is correct |
48 |
Correct |
21 ms |
3548 KB |
Output is correct |
49 |
Correct |
22 ms |
3552 KB |
Output is correct |
50 |
Correct |
82 ms |
3544 KB |
Output is correct |
51 |
Correct |
28 ms |
4200 KB |
Output is correct |
52 |
Correct |
27 ms |
4204 KB |
Output is correct |
53 |
Correct |
102 ms |
4176 KB |
Output is correct |
54 |
Correct |
29 ms |
4800 KB |
Output is correct |
55 |
Correct |
29 ms |
4796 KB |
Output is correct |
56 |
Correct |
109 ms |
4776 KB |
Output is correct |
57 |
Correct |
34 ms |
5512 KB |
Output is correct |
58 |
Correct |
34 ms |
5412 KB |
Output is correct |
59 |
Correct |
132 ms |
5404 KB |
Output is correct |
60 |
Correct |
38 ms |
6236 KB |
Output is correct |
61 |
Correct |
41 ms |
6268 KB |
Output is correct |
62 |
Correct |
160 ms |
6160 KB |
Output is correct |
63 |
Correct |
124 ms |
6196 KB |
Output is correct |
64 |
Correct |
214 ms |
6124 KB |
Output is correct |
65 |
Correct |
230 ms |
6104 KB |
Output is correct |
66 |
Correct |
164 ms |
6280 KB |
Output is correct |
67 |
Correct |
141 ms |
6104 KB |
Output is correct |
68 |
Correct |
75 ms |
6252 KB |
Output is correct |
69 |
Correct |
75 ms |
6108 KB |
Output is correct |
70 |
Correct |
67 ms |
6144 KB |
Output is correct |
71 |
Correct |
55 ms |
6128 KB |
Output is correct |
72 |
Correct |
58 ms |
6128 KB |
Output is correct |
73 |
Correct |
59 ms |
6120 KB |
Output is correct |
74 |
Correct |
112 ms |
6116 KB |
Output is correct |
75 |
Correct |
108 ms |
6180 KB |
Output is correct |
76 |
Correct |
91 ms |
6108 KB |
Output is correct |
77 |
Correct |
97 ms |
6156 KB |
Output is correct |
78 |
Correct |
112 ms |
6212 KB |
Output is correct |
79 |
Correct |
86 ms |
6120 KB |
Output is correct |
80 |
Correct |
99 ms |
6136 KB |
Output is correct |
81 |
Correct |
96 ms |
6280 KB |
Output is correct |
82 |
Correct |
86 ms |
6120 KB |
Output is correct |
83 |
Correct |
107 ms |
6120 KB |
Output is correct |
84 |
Correct |
100 ms |
6120 KB |
Output is correct |
85 |
Correct |
102 ms |
6164 KB |
Output is correct |
86 |
Correct |
133 ms |
6160 KB |
Output is correct |
87 |
Correct |
108 ms |
6136 KB |
Output is correct |
88 |
Correct |
116 ms |
6140 KB |
Output is correct |
89 |
Correct |
114 ms |
6132 KB |
Output is correct |
90 |
Correct |
113 ms |
6312 KB |
Output is correct |
91 |
Correct |
114 ms |
6244 KB |
Output is correct |
92 |
Correct |
115 ms |
6124 KB |
Output is correct |