#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(nullptr);
int n, s; cin >> n >> s;
vector<string> grid(n);
queue<pair<int, int>> q;
queue<int> qq;
pair<int, int> start;
for (int i = 0; i < n; ++i) {
cin >> grid[i];
for (int j = 0; j < n; ++j) {
if (grid[i][j] == 'H') {
q.push({i, j});
qq.push(0);
} else if (grid[i][j] == 'M') {
start = {i, j};
}
}
}
vector<vector<int>> whenbee(n, vector<int>(n, INT32_MAX));
while (!q.empty()) {
pair<int, int> loc = q.front();
int d = qq.front();
q.pop(); qq.pop();
if (loc.first < 0 || loc.first >= n || loc.second < 0 || loc.second >= n) continue;
if (grid[loc.first][loc.second] == 'T' || grid[loc.first][loc.second] == 'D') continue;
if (whenbee[loc.first][loc.second] != INT32_MAX) continue;
whenbee[loc.first][loc.second] = d;
q.push({loc.first - 1, loc.second});
q.push({loc.first + 1, loc.second});
q.push({loc.first, loc.second - 1});
q.push({loc.first, loc.second + 1});
for (int i = 0; i < 4; ++i) qq.push(d + 1);
}
auto works = [&whenbee, &n, &s, &grid, &start](int st) -> bool {
queue<pair<int, int>> q;
queue<int> qq;
q.push(start);
qq.push(st);
vector<vector<bool>> vis(n, vector<bool>(n));
while (!q.empty()) {
pair<int, int> loc = q.front();
int d = qq.front();
q.pop(); qq.pop();
if (loc.first < 0 || loc.first >= n || loc.second < 0 || loc.second >= n) continue;
if ((d / s) >= whenbee[loc.first][loc.second]) continue;
if (grid[loc.first][loc.second] == 'T') continue;
if (vis[loc.first][loc.second]) continue;
vis[loc.first][loc.second] = 1;
if (grid[loc.first][loc.second] == 'D') {
return 1;
}
q.push({loc.first - 1, loc.second});
q.push({loc.first + 1, loc.second});
q.push({loc.first, loc.second - 1});
q.push({loc.first, loc.second + 1});
for (int i = 0; i < 4; ++i) qq.push(d + 1);
}
return 0;
};
if (!works(0)) {
cout << "-1\n";
return 0;
}
int lo = 0, hi = n * n;
while (lo < hi) {
int mi = (lo + hi + 1) / 2;
if (works(s * mi)) {
lo = mi;
} else {
hi = mi - 1;
}
}
cout << lo << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
144 ms |
4844 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
3 ms |
1116 KB |
Output is correct |
34 |
Correct |
6 ms |
1116 KB |
Output is correct |
35 |
Correct |
34 ms |
1236 KB |
Output is correct |
36 |
Correct |
4 ms |
1372 KB |
Output is correct |
37 |
Correct |
4 ms |
1364 KB |
Output is correct |
38 |
Correct |
45 ms |
1360 KB |
Output is correct |
39 |
Correct |
5 ms |
1628 KB |
Output is correct |
40 |
Correct |
5 ms |
1748 KB |
Output is correct |
41 |
Correct |
61 ms |
1628 KB |
Output is correct |
42 |
Correct |
7 ms |
1880 KB |
Output is correct |
43 |
Correct |
7 ms |
1884 KB |
Output is correct |
44 |
Correct |
78 ms |
2040 KB |
Output is correct |
45 |
Correct |
8 ms |
2136 KB |
Output is correct |
46 |
Correct |
9 ms |
2168 KB |
Output is correct |
47 |
Correct |
87 ms |
2136 KB |
Output is correct |
48 |
Correct |
9 ms |
2648 KB |
Output is correct |
49 |
Correct |
9 ms |
2652 KB |
Output is correct |
50 |
Correct |
107 ms |
2732 KB |
Output is correct |
51 |
Correct |
9 ms |
2904 KB |
Output is correct |
52 |
Correct |
11 ms |
2908 KB |
Output is correct |
53 |
Correct |
136 ms |
3284 KB |
Output is correct |
54 |
Correct |
13 ms |
3416 KB |
Output is correct |
55 |
Correct |
13 ms |
3420 KB |
Output is correct |
56 |
Correct |
152 ms |
3532 KB |
Output is correct |
57 |
Correct |
14 ms |
3928 KB |
Output is correct |
58 |
Correct |
15 ms |
3932 KB |
Output is correct |
59 |
Correct |
189 ms |
3984 KB |
Output is correct |
60 |
Correct |
15 ms |
4200 KB |
Output is correct |
61 |
Correct |
16 ms |
4184 KB |
Output is correct |
62 |
Correct |
219 ms |
4356 KB |
Output is correct |
63 |
Correct |
125 ms |
4328 KB |
Output is correct |
64 |
Correct |
240 ms |
4188 KB |
Output is correct |
65 |
Correct |
185 ms |
4436 KB |
Output is correct |
66 |
Correct |
166 ms |
4428 KB |
Output is correct |
67 |
Correct |
34 ms |
4440 KB |
Output is correct |
68 |
Correct |
55 ms |
4444 KB |
Output is correct |
69 |
Correct |
50 ms |
4444 KB |
Output is correct |
70 |
Correct |
47 ms |
4440 KB |
Output is correct |
71 |
Correct |
41 ms |
4444 KB |
Output is correct |
72 |
Correct |
30 ms |
4444 KB |
Output is correct |
73 |
Correct |
50 ms |
5724 KB |
Output is correct |
74 |
Correct |
98 ms |
5980 KB |
Output is correct |
75 |
Correct |
116 ms |
5976 KB |
Output is correct |
76 |
Correct |
115 ms |
5904 KB |
Output is correct |
77 |
Correct |
115 ms |
5980 KB |
Output is correct |
78 |
Correct |
39 ms |
5724 KB |
Output is correct |
79 |
Correct |
88 ms |
5720 KB |
Output is correct |
80 |
Correct |
100 ms |
5720 KB |
Output is correct |
81 |
Correct |
109 ms |
5724 KB |
Output is correct |
82 |
Correct |
97 ms |
5720 KB |
Output is correct |
83 |
Correct |
132 ms |
5464 KB |
Output is correct |
84 |
Correct |
136 ms |
5468 KB |
Output is correct |
85 |
Correct |
124 ms |
5552 KB |
Output is correct |
86 |
Correct |
137 ms |
5704 KB |
Output is correct |
87 |
Correct |
126 ms |
5464 KB |
Output is correct |
88 |
Correct |
129 ms |
5212 KB |
Output is correct |
89 |
Correct |
156 ms |
5212 KB |
Output is correct |
90 |
Correct |
155 ms |
5268 KB |
Output is correct |
91 |
Correct |
140 ms |
5212 KB |
Output is correct |
92 |
Correct |
132 ms |
5212 KB |
Output is correct |