#include <bits/stdc++.h>
#define DEBUG false
using namespace std;
const int MAX_N = 810;
const int INF = 1e9 + 7;
const int d[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
char s[MAX_N][MAX_N];
int db[MAX_N][MAX_N], dm[MAX_N][MAX_N];
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);
}
int sx, sy, ex, ey;
queue <pair <int, int>> q;
for(int i = 1; i <= N; i++) {
for(int j = 1; j <= N; j++) {
db[i][j] = INF;
if(s[i][j] == 'H') {
db[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()) {
auto [ux, uy] = q.front();
q.pop();
for(int i = 0; i < 4; i++) {
int vx = ux + d[i][0], vy = uy + d[i][1];
if(vx < 1 or vx > N or vy < 1 or vy > N or s[vx][vy] == 'T' or s[vx][vy] == 'D' or db[vx][vy] != INF) {
continue;
}
db[vx][vy] = db[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++) {
dm[i][j] = INF;
}
}
queue <pair <int, int>> q;
if(db[sx][sy] < mid) {
q.emplace(sx, sy);
dm[sx][sy] = 0;
}
while(!q.empty()) {
auto [ux, uy] = q.front();
q.pop();
for(int i = 0; i < 4; i++) {
int vx = ux + d[i][0], vy = uy + d[i][1];
if(vx < 1 or vx > N or vy < 1 or vy > N or s[vx][vy] == 'T' or dm[vx][vy] != INF) {
continue;
}
if((dm[ux][uy] + 1) / S >= db[vx][vy] - mid) {
continue;
}
dm[vx][vy] = dm[ux][uy] + 1;
q.emplace(vx, vy);
}
}
if(dm[ex][ey] == INF) {
r = mid - 1;
}
else {
l = mid + 1;
ans = mid;
}
}
cout << ans;
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:85:21: warning: 'ey' may be used uninitialized in this function [-Wmaybe-uninitialized]
85 | if(dm[ex][ey] == INF) {
| ~~~~~~~~~^
mecho.cpp:85:21: warning: 'ex' may be used uninitialized in this function [-Wmaybe-uninitialized]
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
2 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
4 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
5 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
6 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
7 |
Incorrect |
23 ms |
6052 KB |
Output isn't correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
10 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
11 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
12 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Incorrect |
0 ms |
468 KB |
Output isn't correct |
16 |
Incorrect |
0 ms |
468 KB |
Output isn't correct |
17 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
18 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
19 |
Incorrect |
0 ms |
468 KB |
Output isn't correct |
20 |
Incorrect |
0 ms |
468 KB |
Output isn't correct |
21 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
22 |
Incorrect |
0 ms |
596 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
24 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
25 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
27 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
29 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
30 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
31 |
Incorrect |
1 ms |
852 KB |
Output isn't correct |
32 |
Incorrect |
1 ms |
852 KB |
Output isn't correct |
33 |
Incorrect |
4 ms |
2772 KB |
Output isn't correct |
34 |
Incorrect |
4 ms |
2772 KB |
Output isn't correct |
35 |
Incorrect |
4 ms |
2772 KB |
Output isn't correct |
36 |
Incorrect |
5 ms |
3156 KB |
Output isn't correct |
37 |
Incorrect |
5 ms |
3156 KB |
Output isn't correct |
38 |
Incorrect |
6 ms |
3156 KB |
Output isn't correct |
39 |
Incorrect |
6 ms |
3540 KB |
Output isn't correct |
40 |
Incorrect |
6 ms |
3524 KB |
Output isn't correct |
41 |
Incorrect |
7 ms |
3540 KB |
Output isn't correct |
42 |
Incorrect |
6 ms |
3796 KB |
Output isn't correct |
43 |
Incorrect |
7 ms |
3796 KB |
Output isn't correct |
44 |
Incorrect |
8 ms |
3796 KB |
Output isn't correct |
45 |
Incorrect |
9 ms |
4180 KB |
Output isn't correct |
46 |
Incorrect |
8 ms |
4180 KB |
Output isn't correct |
47 |
Incorrect |
9 ms |
4184 KB |
Output isn't correct |
48 |
Incorrect |
9 ms |
4564 KB |
Output isn't correct |
49 |
Incorrect |
10 ms |
4564 KB |
Output isn't correct |
50 |
Incorrect |
11 ms |
4568 KB |
Output isn't correct |
51 |
Incorrect |
11 ms |
4956 KB |
Output isn't correct |
52 |
Incorrect |
10 ms |
4948 KB |
Output isn't correct |
53 |
Incorrect |
13 ms |
4948 KB |
Output isn't correct |
54 |
Incorrect |
12 ms |
5204 KB |
Output isn't correct |
55 |
Incorrect |
12 ms |
5228 KB |
Output isn't correct |
56 |
Incorrect |
15 ms |
5292 KB |
Output isn't correct |
57 |
Incorrect |
14 ms |
5644 KB |
Output isn't correct |
58 |
Incorrect |
14 ms |
5608 KB |
Output isn't correct |
59 |
Incorrect |
19 ms |
5612 KB |
Output isn't correct |
60 |
Incorrect |
15 ms |
5940 KB |
Output isn't correct |
61 |
Incorrect |
15 ms |
5936 KB |
Output isn't correct |
62 |
Incorrect |
18 ms |
5972 KB |
Output isn't correct |
63 |
Incorrect |
26 ms |
5932 KB |
Output isn't correct |
64 |
Incorrect |
25 ms |
5972 KB |
Output isn't correct |
65 |
Incorrect |
25 ms |
6020 KB |
Output isn't correct |
66 |
Incorrect |
25 ms |
5936 KB |
Output isn't correct |
67 |
Correct |
25 ms |
5940 KB |
Output is correct |
68 |
Incorrect |
26 ms |
5964 KB |
Output isn't correct |
69 |
Incorrect |
26 ms |
5956 KB |
Output isn't correct |
70 |
Incorrect |
27 ms |
5956 KB |
Output isn't correct |
71 |
Incorrect |
27 ms |
5972 KB |
Output isn't correct |
72 |
Incorrect |
27 ms |
5948 KB |
Output isn't correct |
73 |
Incorrect |
25 ms |
6228 KB |
Output isn't correct |
74 |
Incorrect |
25 ms |
6220 KB |
Output isn't correct |
75 |
Incorrect |
24 ms |
6216 KB |
Output isn't correct |
76 |
Incorrect |
23 ms |
6220 KB |
Output isn't correct |
77 |
Incorrect |
24 ms |
6224 KB |
Output isn't correct |
78 |
Correct |
25 ms |
6196 KB |
Output is correct |
79 |
Incorrect |
26 ms |
6224 KB |
Output isn't correct |
80 |
Incorrect |
29 ms |
6252 KB |
Output isn't correct |
81 |
Incorrect |
24 ms |
6188 KB |
Output isn't correct |
82 |
Incorrect |
25 ms |
6192 KB |
Output isn't correct |
83 |
Incorrect |
25 ms |
6156 KB |
Output isn't correct |
84 |
Incorrect |
24 ms |
6228 KB |
Output isn't correct |
85 |
Incorrect |
24 ms |
6128 KB |
Output isn't correct |
86 |
Incorrect |
30 ms |
6228 KB |
Output isn't correct |
87 |
Incorrect |
25 ms |
6152 KB |
Output isn't correct |
88 |
Incorrect |
23 ms |
6092 KB |
Output isn't correct |
89 |
Incorrect |
24 ms |
6092 KB |
Output isn't correct |
90 |
Incorrect |
23 ms |
6204 KB |
Output isn't correct |
91 |
Incorrect |
23 ms |
6100 KB |
Output isn't correct |
92 |
Incorrect |
25 ms |
6184 KB |
Output isn't correct |