#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<<12; i>>=1;) ans += i*check(ans+i);
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5484 KB |
Output is correct |
2 |
Correct |
5 ms |
5484 KB |
Output is correct |
3 |
Correct |
4 ms |
5484 KB |
Output is correct |
4 |
Correct |
4 ms |
5484 KB |
Output is correct |
5 |
Correct |
4 ms |
5484 KB |
Output is correct |
6 |
Correct |
4 ms |
5484 KB |
Output is correct |
7 |
Correct |
152 ms |
6252 KB |
Output is correct |
8 |
Correct |
3 ms |
5484 KB |
Output is correct |
9 |
Correct |
4 ms |
5484 KB |
Output is correct |
10 |
Correct |
4 ms |
5484 KB |
Output is correct |
11 |
Correct |
4 ms |
5484 KB |
Output is correct |
12 |
Correct |
4 ms |
5484 KB |
Output is correct |
13 |
Correct |
5 ms |
5484 KB |
Output is correct |
14 |
Correct |
4 ms |
5484 KB |
Output is correct |
15 |
Correct |
4 ms |
5484 KB |
Output is correct |
16 |
Correct |
4 ms |
5484 KB |
Output is correct |
17 |
Correct |
4 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 |
4 ms |
5484 KB |
Output is correct |
21 |
Correct |
4 ms |
5484 KB |
Output is correct |
22 |
Correct |
4 ms |
5484 KB |
Output is correct |
23 |
Correct |
5 ms |
5484 KB |
Output is correct |
24 |
Correct |
5 ms |
5484 KB |
Output is correct |
25 |
Correct |
5 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 |
5572 KB |
Output is correct |
33 |
Incorrect |
21 ms |
5740 KB |
Output isn't correct |
34 |
Incorrect |
29 ms |
5740 KB |
Output isn't correct |
35 |
Correct |
39 ms |
5740 KB |
Output is correct |
36 |
Incorrect |
26 ms |
5740 KB |
Output isn't correct |
37 |
Incorrect |
39 ms |
5740 KB |
Output isn't correct |
38 |
Correct |
51 ms |
5740 KB |
Output is correct |
39 |
Incorrect |
33 ms |
5868 KB |
Output isn't correct |
40 |
Incorrect |
47 ms |
5868 KB |
Output isn't correct |
41 |
Correct |
69 ms |
5872 KB |
Output is correct |
42 |
Incorrect |
46 ms |
5868 KB |
Output isn't correct |
43 |
Incorrect |
64 ms |
5868 KB |
Output isn't correct |
44 |
Correct |
79 ms |
5868 KB |
Output is correct |
45 |
Incorrect |
46 ms |
5868 KB |
Output isn't correct |
46 |
Incorrect |
70 ms |
5868 KB |
Output isn't correct |
47 |
Correct |
103 ms |
5868 KB |
Output is correct |
48 |
Incorrect |
55 ms |
5868 KB |
Output isn't correct |
49 |
Incorrect |
86 ms |
5868 KB |
Output isn't correct |
50 |
Correct |
123 ms |
5996 KB |
Output is correct |
51 |
Incorrect |
65 ms |
5996 KB |
Output isn't correct |
52 |
Incorrect |
100 ms |
6112 KB |
Output isn't correct |
53 |
Correct |
141 ms |
5996 KB |
Output is correct |
54 |
Incorrect |
72 ms |
5948 KB |
Output isn't correct |
55 |
Incorrect |
122 ms |
6064 KB |
Output isn't correct |
56 |
Correct |
174 ms |
5996 KB |
Output is correct |
57 |
Incorrect |
82 ms |
5996 KB |
Output isn't correct |
58 |
Incorrect |
128 ms |
5996 KB |
Output isn't correct |
59 |
Correct |
184 ms |
6124 KB |
Output is correct |
60 |
Incorrect |
95 ms |
6252 KB |
Output isn't correct |
61 |
Incorrect |
159 ms |
6252 KB |
Output isn't correct |
62 |
Correct |
207 ms |
6124 KB |
Output is correct |
63 |
Correct |
135 ms |
6252 KB |
Output is correct |
64 |
Correct |
234 ms |
6124 KB |
Output is correct |
65 |
Correct |
219 ms |
6124 KB |
Output is correct |
66 |
Correct |
178 ms |
6252 KB |
Output is correct |
67 |
Correct |
50 ms |
6252 KB |
Output is correct |
68 |
Correct |
78 ms |
6124 KB |
Output is correct |
69 |
Correct |
61 ms |
6124 KB |
Output is correct |
70 |
Correct |
66 ms |
6124 KB |
Output is correct |
71 |
Correct |
49 ms |
6124 KB |
Output is correct |
72 |
Correct |
45 ms |
6124 KB |
Output is correct |
73 |
Correct |
59 ms |
6380 KB |
Output is correct |
74 |
Correct |
132 ms |
6508 KB |
Output is correct |
75 |
Correct |
117 ms |
6380 KB |
Output is correct |
76 |
Correct |
109 ms |
6508 KB |
Output is correct |
77 |
Correct |
111 ms |
6380 KB |
Output is correct |
78 |
Correct |
49 ms |
6508 KB |
Output is correct |
79 |
Correct |
111 ms |
6380 KB |
Output is correct |
80 |
Correct |
119 ms |
6380 KB |
Output is correct |
81 |
Correct |
119 ms |
6380 KB |
Output is correct |
82 |
Correct |
114 ms |
6508 KB |
Output is correct |
83 |
Correct |
148 ms |
6380 KB |
Output is correct |
84 |
Correct |
136 ms |
6252 KB |
Output is correct |
85 |
Correct |
129 ms |
6252 KB |
Output is correct |
86 |
Correct |
140 ms |
6252 KB |
Output is correct |
87 |
Correct |
137 ms |
6252 KB |
Output is correct |
88 |
Correct |
150 ms |
6252 KB |
Output is correct |
89 |
Correct |
158 ms |
6252 KB |
Output is correct |
90 |
Correct |
151 ms |
6380 KB |
Output is correct |
91 |
Correct |
152 ms |
6252 KB |
Output is correct |
92 |
Correct |
146 ms |
6368 KB |
Output is correct |