#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 810, INF = 1e9 + 7, d[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
char s[MAX_N][MAX_N];
int bee[MAX_N][MAX_N], bear[MAX_N][MAX_N], sx, sy, ex, ey;
queue<pair<int, int>> q;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int N, S;
cin >> N >> S;
for(int i = 1; i <= N; i++) cin >> (s[i] + 1);
for(int i = 1; i <= N; i++) {
for(int j = 1; j <= N; j++) {
bee[i][j] = INF;
if(s[i][j] == 'H') {
bee[i][j] = 0;
q.emplace(i, j);
}
else if(s[i][j] == 'M') sx = i, sy = j;
else if(s[i][j] == 'D') ex = i, ey = j;
}
}
while(!q.empty()) {
int ux = q.front().first, uy = q.front().second; q.pop();
for(int i = 0; i < 4; i++) {
int vx = ux + d[i][0], vy = uy + d[i][1];
if(vx < 1 || vx > N || vy < 1 || vy > N || s[vx][vy] == 'T' || s[vx][vy] == 'D' || bee[vx][vy] != INF) continue;
bee[vx][vy] = bee[ux][uy] + 1;
q.emplace(vx, vy);
}
}
int l = 0, r = N * N, ans = -1;
while(l <= r) {
int mid = (l + r) / 2;
for(int i = 1; i <= N; i++) for(int j = 1; j <= N; j++) bear[i][j] = INF;
if(bee[sx][sy] > mid) {
q.emplace(sx, sy);
bear[sx][sy] = 0;
}
while(!q.empty()) {
int ux = q.front().first, uy = q.front().second; q.pop();
for(int i = 0; i < 4; i++) {
int vx = ux + d[i][0], vy = uy + d[i][1];
if(vx < 1 || vx > N || vy < 1 || vy > N || s[vx][vy] == 'T' || bear[vx][vy] != INF) continue;
if((bear[ux][uy] + 1) / S >= bee[vx][vy] - mid) continue;
bear[vx][vy] = bear[ux][uy] + 1;
q.emplace(vx, vy);
}
}
if(bear[ex][ey] == INF) r = mid - 1;
else{
l = mid + 1;
ans = mid;
}
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
70 ms |
6352 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2504 KB |
Output is correct |
10 |
Correct |
1 ms |
2512 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
4696 KB |
Output is correct |
15 |
Correct |
1 ms |
2516 KB |
Output is correct |
16 |
Correct |
1 ms |
2508 KB |
Output is correct |
17 |
Correct |
1 ms |
2548 KB |
Output is correct |
18 |
Correct |
1 ms |
2516 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2508 KB |
Output is correct |
24 |
Correct |
1 ms |
2652 KB |
Output is correct |
25 |
Correct |
1 ms |
4700 KB |
Output is correct |
26 |
Correct |
1 ms |
4700 KB |
Output is correct |
27 |
Correct |
1 ms |
4700 KB |
Output is correct |
28 |
Correct |
1 ms |
4700 KB |
Output is correct |
29 |
Correct |
1 ms |
4708 KB |
Output is correct |
30 |
Correct |
1 ms |
4700 KB |
Output is correct |
31 |
Correct |
1 ms |
4704 KB |
Output is correct |
32 |
Correct |
1 ms |
4708 KB |
Output is correct |
33 |
Correct |
5 ms |
4968 KB |
Output is correct |
34 |
Correct |
6 ms |
4964 KB |
Output is correct |
35 |
Correct |
14 ms |
5112 KB |
Output is correct |
36 |
Correct |
4 ms |
5012 KB |
Output is correct |
37 |
Correct |
6 ms |
5088 KB |
Output is correct |
38 |
Correct |
22 ms |
5148 KB |
Output is correct |
39 |
Correct |
7 ms |
5060 KB |
Output is correct |
40 |
Correct |
6 ms |
5092 KB |
Output is correct |
41 |
Correct |
23 ms |
5268 KB |
Output is correct |
42 |
Correct |
6 ms |
5268 KB |
Output is correct |
43 |
Correct |
7 ms |
5572 KB |
Output is correct |
44 |
Correct |
31 ms |
5316 KB |
Output is correct |
45 |
Correct |
8 ms |
5216 KB |
Output is correct |
46 |
Correct |
9 ms |
5220 KB |
Output is correct |
47 |
Correct |
33 ms |
5216 KB |
Output is correct |
48 |
Correct |
8 ms |
5440 KB |
Output is correct |
49 |
Correct |
9 ms |
5548 KB |
Output is correct |
50 |
Correct |
38 ms |
5476 KB |
Output is correct |
51 |
Correct |
10 ms |
5732 KB |
Output is correct |
52 |
Correct |
11 ms |
5752 KB |
Output is correct |
53 |
Correct |
51 ms |
5932 KB |
Output is correct |
54 |
Correct |
11 ms |
6008 KB |
Output is correct |
55 |
Correct |
17 ms |
5964 KB |
Output is correct |
56 |
Correct |
70 ms |
5988 KB |
Output is correct |
57 |
Correct |
14 ms |
6144 KB |
Output is correct |
58 |
Correct |
14 ms |
6232 KB |
Output is correct |
59 |
Correct |
82 ms |
6276 KB |
Output is correct |
60 |
Correct |
15 ms |
6604 KB |
Output is correct |
61 |
Correct |
23 ms |
6684 KB |
Output is correct |
62 |
Correct |
84 ms |
6620 KB |
Output is correct |
63 |
Correct |
78 ms |
6576 KB |
Output is correct |
64 |
Correct |
154 ms |
6556 KB |
Output is correct |
65 |
Correct |
108 ms |
6596 KB |
Output is correct |
66 |
Correct |
89 ms |
6564 KB |
Output is correct |
67 |
Correct |
84 ms |
6592 KB |
Output is correct |
68 |
Correct |
36 ms |
6644 KB |
Output is correct |
69 |
Correct |
38 ms |
6548 KB |
Output is correct |
70 |
Correct |
28 ms |
6656 KB |
Output is correct |
71 |
Correct |
34 ms |
6484 KB |
Output is correct |
72 |
Correct |
27 ms |
6636 KB |
Output is correct |
73 |
Correct |
23 ms |
6872 KB |
Output is correct |
74 |
Correct |
47 ms |
6868 KB |
Output is correct |
75 |
Correct |
58 ms |
6912 KB |
Output is correct |
76 |
Correct |
58 ms |
6744 KB |
Output is correct |
77 |
Correct |
60 ms |
7048 KB |
Output is correct |
78 |
Correct |
73 ms |
6836 KB |
Output is correct |
79 |
Correct |
35 ms |
6700 KB |
Output is correct |
80 |
Correct |
39 ms |
6804 KB |
Output is correct |
81 |
Correct |
46 ms |
6816 KB |
Output is correct |
82 |
Correct |
59 ms |
6656 KB |
Output is correct |
83 |
Correct |
51 ms |
6824 KB |
Output is correct |
84 |
Correct |
48 ms |
6644 KB |
Output is correct |
85 |
Correct |
73 ms |
6812 KB |
Output is correct |
86 |
Correct |
48 ms |
6848 KB |
Output is correct |
87 |
Correct |
50 ms |
6620 KB |
Output is correct |
88 |
Correct |
48 ms |
6744 KB |
Output is correct |
89 |
Correct |
62 ms |
6556 KB |
Output is correct |
90 |
Correct |
57 ms |
6632 KB |
Output is correct |
91 |
Correct |
67 ms |
6568 KB |
Output is correct |
92 |
Correct |
62 ms |
6792 KB |
Output is correct |