#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 = 1e6, 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] = -1;
}
}
queue <pair <int, int>> q;
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] != -1) {
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] == -1) {
r = mid - 1;
}
else {
l = mid + 1;
ans = mid;
}
}
cout << ans;
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:83:21: warning: 'ey' may be used uninitialized in this function [-Wmaybe-uninitialized]
83 | if(dm[ex][ey] == -1) {
| ~~~~~~~~~^
mecho.cpp:83:21: warning: 'ex' may be used uninitialized in this function [-Wmaybe-uninitialized]
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
84 ms |
6168 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is 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 |
Correct |
0 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
724 KB |
Output is correct |
28 |
Correct |
1 ms |
724 KB |
Output is correct |
29 |
Correct |
1 ms |
724 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
1 ms |
852 KB |
Output is correct |
32 |
Correct |
1 ms |
852 KB |
Output is correct |
33 |
Correct |
5 ms |
2772 KB |
Output is correct |
34 |
Correct |
5 ms |
2772 KB |
Output is correct |
35 |
Correct |
18 ms |
2772 KB |
Output is correct |
36 |
Correct |
6 ms |
3156 KB |
Output is correct |
37 |
Correct |
5 ms |
3076 KB |
Output is correct |
38 |
Correct |
26 ms |
3176 KB |
Output is correct |
39 |
Correct |
6 ms |
3540 KB |
Output is correct |
40 |
Correct |
9 ms |
3528 KB |
Output is correct |
41 |
Correct |
35 ms |
3540 KB |
Output is correct |
42 |
Correct |
10 ms |
3796 KB |
Output is correct |
43 |
Correct |
9 ms |
3796 KB |
Output is correct |
44 |
Correct |
43 ms |
3864 KB |
Output is correct |
45 |
Correct |
8 ms |
4180 KB |
Output is correct |
46 |
Correct |
8 ms |
4180 KB |
Output is correct |
47 |
Correct |
51 ms |
4180 KB |
Output is correct |
48 |
Correct |
10 ms |
4564 KB |
Output is correct |
49 |
Correct |
11 ms |
4564 KB |
Output is correct |
50 |
Correct |
65 ms |
4516 KB |
Output is correct |
51 |
Correct |
11 ms |
4956 KB |
Output is correct |
52 |
Correct |
12 ms |
4948 KB |
Output is correct |
53 |
Correct |
70 ms |
4948 KB |
Output is correct |
54 |
Correct |
12 ms |
5324 KB |
Output is correct |
55 |
Correct |
15 ms |
5324 KB |
Output is correct |
56 |
Correct |
89 ms |
5332 KB |
Output is correct |
57 |
Correct |
13 ms |
5692 KB |
Output is correct |
58 |
Correct |
14 ms |
5588 KB |
Output is correct |
59 |
Correct |
91 ms |
5588 KB |
Output is correct |
60 |
Correct |
13 ms |
5972 KB |
Output is correct |
61 |
Correct |
15 ms |
5936 KB |
Output is correct |
62 |
Correct |
107 ms |
6024 KB |
Output is correct |
63 |
Correct |
101 ms |
6032 KB |
Output is correct |
64 |
Correct |
156 ms |
6028 KB |
Output is correct |
65 |
Correct |
134 ms |
5944 KB |
Output is correct |
66 |
Correct |
140 ms |
6024 KB |
Output is correct |
67 |
Correct |
118 ms |
5940 KB |
Output is correct |
68 |
Correct |
41 ms |
5964 KB |
Output is correct |
69 |
Correct |
42 ms |
5960 KB |
Output is correct |
70 |
Correct |
35 ms |
5972 KB |
Output is correct |
71 |
Correct |
30 ms |
5968 KB |
Output is correct |
72 |
Incorrect |
29 ms |
5964 KB |
Output isn't correct |
73 |
Incorrect |
26 ms |
6212 KB |
Output isn't correct |
74 |
Correct |
50 ms |
6228 KB |
Output is correct |
75 |
Correct |
55 ms |
6300 KB |
Output is correct |
76 |
Correct |
51 ms |
6276 KB |
Output is correct |
77 |
Correct |
57 ms |
6228 KB |
Output is correct |
78 |
Correct |
66 ms |
6312 KB |
Output is correct |
79 |
Correct |
64 ms |
6276 KB |
Output is correct |
80 |
Correct |
58 ms |
6224 KB |
Output is correct |
81 |
Correct |
60 ms |
6184 KB |
Output is correct |
82 |
Correct |
45 ms |
6188 KB |
Output is correct |
83 |
Correct |
75 ms |
6236 KB |
Output is correct |
84 |
Correct |
64 ms |
6236 KB |
Output is correct |
85 |
Correct |
65 ms |
6236 KB |
Output is correct |
86 |
Correct |
76 ms |
6148 KB |
Output is correct |
87 |
Correct |
63 ms |
6240 KB |
Output is correct |
88 |
Correct |
68 ms |
6180 KB |
Output is correct |
89 |
Correct |
75 ms |
6192 KB |
Output is correct |
90 |
Correct |
74 ms |
6224 KB |
Output is correct |
91 |
Correct |
71 ms |
6184 KB |
Output is correct |
92 |
Correct |
75 ms |
6180 KB |
Output is correct |