#include <bits/stdc++.h>
using namespace std;
const vector<int> dx = {0, 0, -1, 1};
const vector<int> dy = {1, -1, 0, 0};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, s;
cin >> n >> s;
vector<string> grid(n);
queue< pair<int, int> > q1, q2;
vector< vector<int> > dist1(n, vector<int>(n, -1)), dist2(n, vector<int>(n, -1));
int mr, mc, dr, dc;
for (int i = 0; i < n; i++) {
cin >> grid[i];
for (int j = 0; j < n; j++) {
if (grid[i][j] == 'M') {
q1.push({i, j});
dist1[i][j] = 0;
mr = i; mc = j;
} else if (grid[i][j] == 'H') {
q2.push({i, j});
dist2[i][j] = 0;
} else if (grid[i][j] == 'D') {
dr = i; dc = j;
}
}
}
while (!q1.empty()) {
int r = q1.front().first, c = q1.front().second;
q1.pop();
for (int k = 0; k < 4; k++) {
int nr = r + dx[k], nc = c + dy[k];
if (nr < 0 || nr >= n || nc < 0 || nc >= n || grid[nr][nc] == 'T' || dist1[nr][nc] != -1) {
continue;
}
dist1[nr][nc] = dist1[r][c] + 1;
q1.push({nr, nc});
}
}
while (!q2.empty()) {
int r = q2.front().first, c = q2.front().second;
q2.pop();
for (int k = 0; k < 4; k++) {
int nr = r + dx[k], nc = c + dy[k];
if (nr < 0 || nr >= n || nc < 0 || nc >= n || grid[nr][nc] == 'T' || dist2[nr][nc] != -1) {
continue;
}
dist2[nr][nc] = dist2[r][c] + s;
q2.push({nr, nc});
}
}
int lo = 0, hi = n * n, ans = -1;
for (int i = 0; i < 50; i++) {
int mid = (lo + hi + 1) / 2;
vector< vector<bool> > was(n, vector<bool>(n, false));
queue< pair<int, int> > q;
q.push({mr, mc});
while (!q.empty()) {
int r = q.front().first, c = q.front().second;
q.pop();
for (int k = 0; k < 4; k++) {
int nr = r + dx[k], nc = c + dy[k];
if (nr < 0 || nr >= n || nc < 0 || nc >= n || grid[nr][nc] == 'T' || dist1[nr][nc] + 4 * mid > dist2[nr][nc] || was[nr][nc]) {
continue;
}
was[nr][nc] = true;
q.push({nr, nc});
}
}
if (was[dr][dc]) {
lo = mid;
ans = mid;
} else {
hi = mid - 1;
}
}
cout << ans << '\n';
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:73:15: warning: 'dr' may be used uninitialized in this function [-Wmaybe-uninitialized]
73 | if (was[dr][dc]) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
7 |
Incorrect |
626 ms |
6908 KB |
Output isn't correct |
8 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
9 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
11 |
Correct |
1 ms |
388 KB |
Output is correct |
12 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
13 |
Incorrect |
3 ms |
384 KB |
Output isn't correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
16 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
17 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
18 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
19 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
20 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
21 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
23 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
24 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
25 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
26 |
Incorrect |
3 ms |
384 KB |
Output isn't correct |
27 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
28 |
Incorrect |
3 ms |
384 KB |
Output isn't correct |
29 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
30 |
Incorrect |
3 ms |
384 KB |
Output isn't correct |
31 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
32 |
Incorrect |
4 ms |
384 KB |
Output isn't correct |
33 |
Incorrect |
11 ms |
1664 KB |
Output isn't correct |
34 |
Incorrect |
87 ms |
1664 KB |
Output isn't correct |
35 |
Incorrect |
165 ms |
1664 KB |
Output isn't correct |
36 |
Incorrect |
10 ms |
2176 KB |
Output isn't correct |
37 |
Incorrect |
111 ms |
2048 KB |
Output isn't correct |
38 |
Incorrect |
219 ms |
2048 KB |
Output isn't correct |
39 |
Incorrect |
11 ms |
2464 KB |
Output isn't correct |
40 |
Incorrect |
137 ms |
2432 KB |
Output isn't correct |
41 |
Incorrect |
279 ms |
2476 KB |
Output isn't correct |
42 |
Incorrect |
17 ms |
2944 KB |
Output isn't correct |
43 |
Incorrect |
175 ms |
2960 KB |
Output isn't correct |
44 |
Incorrect |
362 ms |
3064 KB |
Output isn't correct |
45 |
Incorrect |
20 ms |
3456 KB |
Output isn't correct |
46 |
Incorrect |
219 ms |
3512 KB |
Output isn't correct |
47 |
Incorrect |
458 ms |
3492 KB |
Output isn't correct |
48 |
Incorrect |
20 ms |
4096 KB |
Output isn't correct |
49 |
Incorrect |
249 ms |
4096 KB |
Output isn't correct |
50 |
Incorrect |
580 ms |
4216 KB |
Output isn't correct |
51 |
Incorrect |
23 ms |
4608 KB |
Output isn't correct |
52 |
Incorrect |
303 ms |
4736 KB |
Output isn't correct |
53 |
Incorrect |
642 ms |
4728 KB |
Output isn't correct |
54 |
Incorrect |
31 ms |
5368 KB |
Output isn't correct |
55 |
Incorrect |
350 ms |
5496 KB |
Output isn't correct |
56 |
Incorrect |
819 ms |
5396 KB |
Output isn't correct |
57 |
Incorrect |
33 ms |
6016 KB |
Output isn't correct |
58 |
Incorrect |
421 ms |
6120 KB |
Output isn't correct |
59 |
Incorrect |
914 ms |
6016 KB |
Output isn't correct |
60 |
Incorrect |
36 ms |
6784 KB |
Output isn't correct |
61 |
Incorrect |
505 ms |
6784 KB |
Output isn't correct |
62 |
Execution timed out |
1079 ms |
6792 KB |
Time limit exceeded |
63 |
Correct |
733 ms |
6904 KB |
Output is correct |
64 |
Incorrect |
987 ms |
6920 KB |
Output isn't correct |
65 |
Correct |
821 ms |
6904 KB |
Output is correct |
66 |
Incorrect |
642 ms |
7032 KB |
Output isn't correct |
67 |
Correct |
574 ms |
6784 KB |
Output is correct |
68 |
Incorrect |
257 ms |
6904 KB |
Output isn't correct |
69 |
Incorrect |
262 ms |
6784 KB |
Output isn't correct |
70 |
Incorrect |
195 ms |
6784 KB |
Output isn't correct |
71 |
Incorrect |
182 ms |
6784 KB |
Output isn't correct |
72 |
Incorrect |
68 ms |
6784 KB |
Output isn't correct |
73 |
Incorrect |
419 ms |
7032 KB |
Output isn't correct |
74 |
Incorrect |
343 ms |
7032 KB |
Output isn't correct |
75 |
Incorrect |
395 ms |
7160 KB |
Output isn't correct |
76 |
Incorrect |
353 ms |
7040 KB |
Output isn't correct |
77 |
Incorrect |
388 ms |
7160 KB |
Output isn't correct |
78 |
Correct |
493 ms |
7036 KB |
Output is correct |
79 |
Incorrect |
357 ms |
7036 KB |
Output isn't correct |
80 |
Incorrect |
425 ms |
7036 KB |
Output isn't correct |
81 |
Incorrect |
487 ms |
6904 KB |
Output isn't correct |
82 |
Incorrect |
379 ms |
7136 KB |
Output isn't correct |
83 |
Correct |
588 ms |
6992 KB |
Output is correct |
84 |
Incorrect |
491 ms |
7032 KB |
Output isn't correct |
85 |
Incorrect |
505 ms |
7000 KB |
Output isn't correct |
86 |
Incorrect |
606 ms |
6996 KB |
Output isn't correct |
87 |
Incorrect |
580 ms |
7000 KB |
Output isn't correct |
88 |
Incorrect |
827 ms |
6944 KB |
Output isn't correct |
89 |
Incorrect |
777 ms |
6904 KB |
Output isn't correct |
90 |
Incorrect |
720 ms |
6952 KB |
Output isn't correct |
91 |
Incorrect |
641 ms |
6904 KB |
Output isn't correct |
92 |
Incorrect |
725 ms |
6920 KB |
Output isn't correct |