#include <bits/stdc++.h>
#define DEBUG false
using namespace std;
const int MAX_N = 810;
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;
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] = -1;
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] != -1) {
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(db[vx][vy] != -1 and (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:84:21: warning: 'ey' may be used uninitialized in this function [-Wmaybe-uninitialized]
84 | if(dm[ex][ey] == -1) {
| ~~~~~~~~~^
mecho.cpp:84: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 |
1 ms |
340 KB |
Output isn't correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
7 |
Incorrect |
79 ms |
6136 KB |
Output isn't correct |
8 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
11 |
Incorrect |
0 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 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
15 |
Incorrect |
0 ms |
468 KB |
Output isn't correct |
16 |
Correct |
0 ms |
468 KB |
Output is correct |
17 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
18 |
Correct |
0 ms |
468 KB |
Output is correct |
19 |
Incorrect |
0 ms |
468 KB |
Output isn't correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
22 |
Correct |
0 ms |
596 KB |
Output is correct |
23 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
28 |
Correct |
1 ms |
724 KB |
Output is correct |
29 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Incorrect |
1 ms |
852 KB |
Output isn't correct |
32 |
Correct |
1 ms |
852 KB |
Output is correct |
33 |
Incorrect |
4 ms |
2772 KB |
Output isn't correct |
34 |
Correct |
5 ms |
2772 KB |
Output is correct |
35 |
Correct |
19 ms |
2840 KB |
Output is correct |
36 |
Incorrect |
4 ms |
3156 KB |
Output isn't correct |
37 |
Correct |
4 ms |
3136 KB |
Output is correct |
38 |
Correct |
26 ms |
3188 KB |
Output is correct |
39 |
Incorrect |
6 ms |
3540 KB |
Output isn't correct |
40 |
Correct |
6 ms |
3540 KB |
Output is correct |
41 |
Correct |
32 ms |
3524 KB |
Output is correct |
42 |
Incorrect |
8 ms |
3796 KB |
Output isn't correct |
43 |
Correct |
8 ms |
3796 KB |
Output is correct |
44 |
Correct |
41 ms |
3796 KB |
Output is correct |
45 |
Incorrect |
8 ms |
4180 KB |
Output isn't correct |
46 |
Correct |
7 ms |
4256 KB |
Output is correct |
47 |
Correct |
54 ms |
4176 KB |
Output is correct |
48 |
Incorrect |
9 ms |
4564 KB |
Output isn't correct |
49 |
Correct |
10 ms |
4564 KB |
Output is correct |
50 |
Correct |
61 ms |
4608 KB |
Output is correct |
51 |
Incorrect |
11 ms |
4956 KB |
Output isn't correct |
52 |
Correct |
13 ms |
4972 KB |
Output is correct |
53 |
Correct |
69 ms |
4956 KB |
Output is correct |
54 |
Incorrect |
13 ms |
5284 KB |
Output isn't correct |
55 |
Correct |
15 ms |
5204 KB |
Output is correct |
56 |
Correct |
85 ms |
5292 KB |
Output is correct |
57 |
Incorrect |
13 ms |
5588 KB |
Output isn't correct |
58 |
Correct |
16 ms |
5608 KB |
Output is correct |
59 |
Correct |
91 ms |
5620 KB |
Output is correct |
60 |
Incorrect |
13 ms |
5972 KB |
Output isn't correct |
61 |
Correct |
15 ms |
5932 KB |
Output is correct |
62 |
Correct |
108 ms |
6032 KB |
Output is correct |
63 |
Incorrect |
136 ms |
6028 KB |
Output isn't correct |
64 |
Correct |
153 ms |
6020 KB |
Output is correct |
65 |
Incorrect |
144 ms |
6032 KB |
Output isn't correct |
66 |
Incorrect |
127 ms |
5964 KB |
Output isn't correct |
67 |
Incorrect |
136 ms |
6028 KB |
Output isn't correct |
68 |
Incorrect |
44 ms |
6052 KB |
Output isn't correct |
69 |
Correct |
45 ms |
6092 KB |
Output is correct |
70 |
Incorrect |
28 ms |
5960 KB |
Output isn't correct |
71 |
Incorrect |
30 ms |
6048 KB |
Output isn't correct |
72 |
Incorrect |
26 ms |
5960 KB |
Output isn't correct |
73 |
Incorrect |
28 ms |
6260 KB |
Output isn't correct |
74 |
Correct |
51 ms |
6220 KB |
Output is correct |
75 |
Incorrect |
57 ms |
6300 KB |
Output isn't correct |
76 |
Incorrect |
52 ms |
6228 KB |
Output isn't correct |
77 |
Incorrect |
55 ms |
6228 KB |
Output isn't correct |
78 |
Incorrect |
67 ms |
6220 KB |
Output isn't correct |
79 |
Correct |
48 ms |
6228 KB |
Output is correct |
80 |
Incorrect |
53 ms |
6208 KB |
Output isn't correct |
81 |
Incorrect |
60 ms |
6276 KB |
Output isn't correct |
82 |
Incorrect |
49 ms |
6268 KB |
Output isn't correct |
83 |
Incorrect |
71 ms |
6220 KB |
Output isn't correct |
84 |
Correct |
59 ms |
6220 KB |
Output is correct |
85 |
Incorrect |
59 ms |
6156 KB |
Output isn't correct |
86 |
Incorrect |
79 ms |
6232 KB |
Output isn't correct |
87 |
Correct |
65 ms |
6228 KB |
Output is correct |
88 |
Incorrect |
70 ms |
6104 KB |
Output isn't correct |
89 |
Correct |
69 ms |
6096 KB |
Output is correct |
90 |
Correct |
76 ms |
6212 KB |
Output is correct |
91 |
Correct |
75 ms |
6284 KB |
Output is correct |
92 |
Incorrect |
70 ms |
6184 KB |
Output isn't correct |