#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2")
#include<bits/stdc++.h>
#define all(x) begin(x), end(x)
using namespace std;
const int maxn = 808, C = 1;
using ll = long long;
int n, s;
char c[maxn][maxn];
array<int, 2> st, en;
int bdi[maxn][maxn];
vector<array<int, 2>> d {{1, 0}, {-1, 0}, {0, -1}, {0, 1}};
bool good(int x, int y) {
return !(min(x, y) < 1 || max(x, y) > n) && c[x][y] != 'T';
}
void bees() {
memset(bdi, -1, sizeof bdi);
queue<array<int, 2>> q;
auto add = [&](int x, int y, int d) {
if(bdi[x][y] >= 0) return;
if(c[x][y] == 'D') return;
bdi[x][y] = d;
q.push({x, y});
};
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
if(c[i][j] == 'H') add(i, j, 0);
while(!q.empty()) {
auto [x, y] = q.front(); q.pop();
for(auto [dx, dy] : d) {
int tx = x+dx, ty = y+dy;
if(good(tx, ty)) add(tx, ty, bdi[x][y]+1);
}
}
}
int dist[maxn][maxn];
bool check(int ti) {
queue<array<int, 2>> q;
memset(dist, -1, sizeof dist);
auto add = [&](int x, int y, int d) {
if(dist[x][y] >= 0) return;
//cout << x << " " << y << " " << d << " | " << bdi[x][y] << endl;
if(bdi[x][y] != -1 && ti + (d/s) >= bdi[x][y]) return;
dist[x][y] = d;
q.push({x, y});
};
add(st[0], st[1], 0);
while(!q.empty()) {
auto [x, y] = q.front(); q.pop();
for(auto [dx, dy] : d) {
int tx = x+dx, ty = y+dy;
if(good(tx, ty)) add(tx, ty, dist[x][y]+1);
}
}
return dist[en[0]][en[1]] >= 0;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> s;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++) {
cin >> c[i][j];
if(c[i][j] == 'M') st = {i, j};
if(c[i][j] == 'D') en = {i, j};
}
bees();
if(!check(0)) return cout << -1, 0;
int ans = 0;
for(int i = 1<<20; i>>=1;) ans += i*check(ans+i);
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
5484 KB |
Output is correct |
2 |
Correct |
5 ms |
5484 KB |
Output is correct |
3 |
Correct |
5 ms |
5484 KB |
Output is correct |
4 |
Correct |
5 ms |
5484 KB |
Output is correct |
5 |
Correct |
5 ms |
5484 KB |
Output is correct |
6 |
Correct |
5 ms |
5484 KB |
Output is correct |
7 |
Correct |
172 ms |
6252 KB |
Output is correct |
8 |
Correct |
3 ms |
5484 KB |
Output is correct |
9 |
Correct |
5 ms |
5484 KB |
Output is correct |
10 |
Correct |
5 ms |
5484 KB |
Output is correct |
11 |
Correct |
6 ms |
5484 KB |
Output is correct |
12 |
Correct |
7 ms |
5484 KB |
Output is correct |
13 |
Correct |
5 ms |
5484 KB |
Output is correct |
14 |
Correct |
3 ms |
5484 KB |
Output is correct |
15 |
Correct |
5 ms |
5484 KB |
Output is correct |
16 |
Correct |
5 ms |
5484 KB |
Output is correct |
17 |
Correct |
5 ms |
5484 KB |
Output is correct |
18 |
Correct |
5 ms |
5484 KB |
Output is correct |
19 |
Correct |
5 ms |
5484 KB |
Output is correct |
20 |
Correct |
5 ms |
5484 KB |
Output is correct |
21 |
Correct |
6 ms |
5484 KB |
Output is correct |
22 |
Correct |
5 ms |
5484 KB |
Output is correct |
23 |
Correct |
5 ms |
5484 KB |
Output is correct |
24 |
Correct |
6 ms |
5484 KB |
Output is correct |
25 |
Correct |
6 ms |
5484 KB |
Output is correct |
26 |
Correct |
5 ms |
5484 KB |
Output is correct |
27 |
Correct |
5 ms |
5484 KB |
Output is correct |
28 |
Correct |
5 ms |
5484 KB |
Output is correct |
29 |
Correct |
5 ms |
5484 KB |
Output is correct |
30 |
Correct |
5 ms |
5484 KB |
Output is correct |
31 |
Correct |
5 ms |
5484 KB |
Output is correct |
32 |
Correct |
5 ms |
5484 KB |
Output is correct |
33 |
Correct |
11 ms |
5740 KB |
Output is correct |
34 |
Correct |
12 ms |
5740 KB |
Output is correct |
35 |
Correct |
43 ms |
5740 KB |
Output is correct |
36 |
Correct |
12 ms |
5868 KB |
Output is correct |
37 |
Correct |
13 ms |
5740 KB |
Output is correct |
38 |
Correct |
53 ms |
5740 KB |
Output is correct |
39 |
Correct |
14 ms |
5868 KB |
Output is correct |
40 |
Correct |
16 ms |
5868 KB |
Output is correct |
41 |
Correct |
74 ms |
5868 KB |
Output is correct |
42 |
Correct |
19 ms |
5996 KB |
Output is correct |
43 |
Correct |
21 ms |
5868 KB |
Output is correct |
44 |
Correct |
85 ms |
5868 KB |
Output is correct |
45 |
Correct |
18 ms |
5868 KB |
Output is correct |
46 |
Correct |
22 ms |
5868 KB |
Output is correct |
47 |
Correct |
110 ms |
5868 KB |
Output is correct |
48 |
Correct |
20 ms |
5868 KB |
Output is correct |
49 |
Correct |
24 ms |
5996 KB |
Output is correct |
50 |
Correct |
154 ms |
5996 KB |
Output is correct |
51 |
Correct |
24 ms |
5996 KB |
Output is correct |
52 |
Correct |
27 ms |
5996 KB |
Output is correct |
53 |
Correct |
147 ms |
6124 KB |
Output is correct |
54 |
Correct |
28 ms |
5996 KB |
Output is correct |
55 |
Correct |
33 ms |
5996 KB |
Output is correct |
56 |
Correct |
180 ms |
5956 KB |
Output is correct |
57 |
Correct |
28 ms |
5996 KB |
Output is correct |
58 |
Correct |
33 ms |
5996 KB |
Output is correct |
59 |
Correct |
209 ms |
5996 KB |
Output is correct |
60 |
Correct |
31 ms |
6124 KB |
Output is correct |
61 |
Correct |
36 ms |
6124 KB |
Output is correct |
62 |
Correct |
231 ms |
6252 KB |
Output is correct |
63 |
Correct |
147 ms |
6272 KB |
Output is correct |
64 |
Correct |
218 ms |
6032 KB |
Output is correct |
65 |
Correct |
215 ms |
6124 KB |
Output is correct |
66 |
Correct |
197 ms |
6252 KB |
Output is correct |
67 |
Correct |
46 ms |
6124 KB |
Output is correct |
68 |
Correct |
63 ms |
6176 KB |
Output is correct |
69 |
Correct |
61 ms |
6124 KB |
Output is correct |
70 |
Correct |
61 ms |
6172 KB |
Output is correct |
71 |
Correct |
52 ms |
6124 KB |
Output is correct |
72 |
Correct |
48 ms |
6124 KB |
Output is correct |
73 |
Correct |
65 ms |
6380 KB |
Output is correct |
74 |
Correct |
131 ms |
6380 KB |
Output is correct |
75 |
Correct |
126 ms |
6508 KB |
Output is correct |
76 |
Correct |
121 ms |
6380 KB |
Output is correct |
77 |
Correct |
121 ms |
6380 KB |
Output is correct |
78 |
Correct |
50 ms |
6380 KB |
Output is correct |
79 |
Correct |
112 ms |
6380 KB |
Output is correct |
80 |
Correct |
121 ms |
6380 KB |
Output is correct |
81 |
Correct |
131 ms |
6508 KB |
Output is correct |
82 |
Correct |
115 ms |
6380 KB |
Output is correct |
83 |
Correct |
153 ms |
6252 KB |
Output is correct |
84 |
Correct |
148 ms |
6508 KB |
Output is correct |
85 |
Correct |
134 ms |
6380 KB |
Output is correct |
86 |
Correct |
147 ms |
6252 KB |
Output is correct |
87 |
Correct |
160 ms |
6252 KB |
Output is correct |
88 |
Correct |
151 ms |
6380 KB |
Output is correct |
89 |
Correct |
176 ms |
6308 KB |
Output is correct |
90 |
Correct |
164 ms |
6252 KB |
Output is correct |
91 |
Correct |
189 ms |
6380 KB |
Output is correct |
92 |
Correct |
159 ms |
6252 KB |
Output is correct |