#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) && bdi[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;
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;
if(ti + (d/s) >= bdi[x][y]) return;
//cout << x << " " << y << " " << d << " | " << bdi[x][y] << endl;
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();
int ans = 0;
for(int i = 1<<10; i>>=1;) ans += i*check(ans+i);
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5484 KB |
Output is correct |
2 |
Incorrect |
4 ms |
5484 KB |
Output isn't correct |
3 |
Correct |
4 ms |
5484 KB |
Output is correct |
4 |
Incorrect |
4 ms |
5484 KB |
Output isn't correct |
5 |
Correct |
5 ms |
5612 KB |
Output is correct |
6 |
Incorrect |
4 ms |
5484 KB |
Output isn't correct |
7 |
Incorrect |
146 ms |
7020 KB |
Output isn't correct |
8 |
Incorrect |
4 ms |
5484 KB |
Output isn't correct |
9 |
Incorrect |
4 ms |
5484 KB |
Output isn't correct |
10 |
Correct |
4 ms |
5484 KB |
Output is correct |
11 |
Correct |
4 ms |
5484 KB |
Output is correct |
12 |
Incorrect |
4 ms |
5484 KB |
Output isn't correct |
13 |
Correct |
4 ms |
5484 KB |
Output is correct |
14 |
Incorrect |
4 ms |
5484 KB |
Output isn't correct |
15 |
Incorrect |
4 ms |
5484 KB |
Output isn't correct |
16 |
Incorrect |
4 ms |
5484 KB |
Output isn't correct |
17 |
Incorrect |
5 ms |
5484 KB |
Output isn't correct |
18 |
Incorrect |
5 ms |
5500 KB |
Output isn't correct |
19 |
Incorrect |
5 ms |
5484 KB |
Output isn't correct |
20 |
Incorrect |
4 ms |
5484 KB |
Output isn't correct |
21 |
Incorrect |
4 ms |
5484 KB |
Output isn't correct |
22 |
Incorrect |
4 ms |
5484 KB |
Output isn't correct |
23 |
Incorrect |
4 ms |
5484 KB |
Output isn't correct |
24 |
Incorrect |
4 ms |
5484 KB |
Output isn't correct |
25 |
Incorrect |
4 ms |
5484 KB |
Output isn't correct |
26 |
Incorrect |
5 ms |
5484 KB |
Output isn't correct |
27 |
Incorrect |
5 ms |
5484 KB |
Output isn't correct |
28 |
Incorrect |
5 ms |
5484 KB |
Output isn't correct |
29 |
Incorrect |
5 ms |
5484 KB |
Output isn't correct |
30 |
Incorrect |
6 ms |
5484 KB |
Output isn't correct |
31 |
Incorrect |
5 ms |
5484 KB |
Output isn't correct |
32 |
Incorrect |
5 ms |
5484 KB |
Output isn't correct |
33 |
Incorrect |
11 ms |
5868 KB |
Output isn't correct |
34 |
Incorrect |
19 ms |
5868 KB |
Output isn't correct |
35 |
Incorrect |
43 ms |
5868 KB |
Output isn't correct |
36 |
Incorrect |
14 ms |
5868 KB |
Output isn't correct |
37 |
Incorrect |
24 ms |
5868 KB |
Output isn't correct |
38 |
Incorrect |
84 ms |
6144 KB |
Output isn't correct |
39 |
Incorrect |
15 ms |
5996 KB |
Output isn't correct |
40 |
Incorrect |
35 ms |
5996 KB |
Output isn't correct |
41 |
Incorrect |
71 ms |
5996 KB |
Output isn't correct |
42 |
Incorrect |
21 ms |
6124 KB |
Output isn't correct |
43 |
Incorrect |
46 ms |
6124 KB |
Output isn't correct |
44 |
Incorrect |
103 ms |
6252 KB |
Output isn't correct |
45 |
Incorrect |
23 ms |
6124 KB |
Output isn't correct |
46 |
Incorrect |
32 ms |
6252 KB |
Output isn't correct |
47 |
Incorrect |
116 ms |
6252 KB |
Output isn't correct |
48 |
Incorrect |
23 ms |
6252 KB |
Output isn't correct |
49 |
Incorrect |
47 ms |
6252 KB |
Output isn't correct |
50 |
Incorrect |
140 ms |
6252 KB |
Output isn't correct |
51 |
Incorrect |
42 ms |
6380 KB |
Output isn't correct |
52 |
Incorrect |
48 ms |
6380 KB |
Output isn't correct |
53 |
Incorrect |
161 ms |
6508 KB |
Output isn't correct |
54 |
Incorrect |
32 ms |
6508 KB |
Output isn't correct |
55 |
Incorrect |
72 ms |
6508 KB |
Output isn't correct |
56 |
Incorrect |
190 ms |
6656 KB |
Output isn't correct |
57 |
Incorrect |
50 ms |
6636 KB |
Output isn't correct |
58 |
Incorrect |
92 ms |
6636 KB |
Output isn't correct |
59 |
Incorrect |
225 ms |
6568 KB |
Output isn't correct |
60 |
Incorrect |
41 ms |
6764 KB |
Output isn't correct |
61 |
Incorrect |
87 ms |
6764 KB |
Output isn't correct |
62 |
Incorrect |
319 ms |
6764 KB |
Output isn't correct |
63 |
Incorrect |
48 ms |
6764 KB |
Output isn't correct |
64 |
Incorrect |
82 ms |
6764 KB |
Output isn't correct |
65 |
Incorrect |
55 ms |
6764 KB |
Output isn't correct |
66 |
Incorrect |
51 ms |
6764 KB |
Output isn't correct |
67 |
Incorrect |
51 ms |
6912 KB |
Output isn't correct |
68 |
Correct |
98 ms |
6892 KB |
Output is correct |
69 |
Incorrect |
143 ms |
6892 KB |
Output isn't correct |
70 |
Incorrect |
58 ms |
6892 KB |
Output isn't correct |
71 |
Incorrect |
50 ms |
6892 KB |
Output isn't correct |
72 |
Incorrect |
154 ms |
6892 KB |
Output isn't correct |
73 |
Incorrect |
37 ms |
7020 KB |
Output isn't correct |
74 |
Correct |
89 ms |
7020 KB |
Output is correct |
75 |
Incorrect |
102 ms |
7020 KB |
Output isn't correct |
76 |
Correct |
97 ms |
7148 KB |
Output is correct |
77 |
Incorrect |
103 ms |
7148 KB |
Output isn't correct |
78 |
Incorrect |
135 ms |
7132 KB |
Output isn't correct |
79 |
Incorrect |
79 ms |
7020 KB |
Output isn't correct |
80 |
Incorrect |
99 ms |
7020 KB |
Output isn't correct |
81 |
Incorrect |
104 ms |
7020 KB |
Output isn't correct |
82 |
Incorrect |
98 ms |
6932 KB |
Output isn't correct |
83 |
Incorrect |
111 ms |
6892 KB |
Output isn't correct |
84 |
Correct |
106 ms |
6892 KB |
Output is correct |
85 |
Incorrect |
103 ms |
6892 KB |
Output isn't correct |
86 |
Incorrect |
110 ms |
6892 KB |
Output isn't correct |
87 |
Incorrect |
111 ms |
6892 KB |
Output isn't correct |
88 |
Correct |
125 ms |
7020 KB |
Output is correct |
89 |
Incorrect |
117 ms |
7020 KB |
Output isn't correct |
90 |
Incorrect |
117 ms |
6892 KB |
Output isn't correct |
91 |
Incorrect |
110 ms |
6956 KB |
Output isn't correct |
92 |
Incorrect |
133 ms |
6892 KB |
Output isn't correct |