#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
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> > q;
vector< vector<int> > dist1(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') {
mr = i; mc = j;
} else if (grid[i][j] == 'H') {
q.push({i, j});
dist1[i][j] = 0;
} else if (grid[i][j] == 'D') {
dr = i; dc = j;
}
}
}
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] != -1 || grid[nr][nc] == 'D') {
continue;
}
dist1[nr][nc] = dist1[r][c] + s;
q.push({nr, nc});
}
}
int lo = 0, hi = n * n, ans = -1;
for (int i = 0; i < 50; i++) {
int mid = (lo + hi) / 2;
//cout << mid << endl;
vector< vector<int> > dist2(n, vector<int>(n, INF));
dist2[mr][mc] = s * mid;
if (dist2[mr][mc] >= dist1[mr][mc]) {
hi = mid - 1;
continue;
}
q.push({mr, mc});
while (!q.empty()) {
int r = q.front().first, c = q.front().second;
//cout << r << ' ' << c << endl;
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 || dist2[r][c] + 1 >= dist2[nr][nc] || ((nr != dr || nc != dc) && dist2[r][c] + 1 >= dist1[nr][nc]) || grid[nr][nc] == 'T') {
continue;
}
dist2[nr][nc] = dist2[r][c] + 1;
q.push({nr, nc});
}
}
if (dist2[dr][dc] < INF) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
cout << ans << '\n';
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:47:17: warning: 'mc' may be used uninitialized in this function [-Wmaybe-uninitialized]
47 | dist2[mr][mc] = s * mid;
| ^
mecho.cpp:47:13: warning: 'mr' may be used uninitialized in this function [-Wmaybe-uninitialized]
47 | dist2[mr][mc] = s * mid;
| ^
mecho.cpp:66:21: warning: 'dc' may be used uninitialized in this function [-Wmaybe-uninitialized]
66 | if (dist2[dr][dc] < INF) {
| ^
mecho.cpp:66:17: warning: 'dr' may be used uninitialized in this function [-Wmaybe-uninitialized]
66 | if (dist2[dr][dc] < INF) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
288 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
535 ms |
6252 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
1 ms |
384 KB |
Output is correct |
31 |
Correct |
1 ms |
384 KB |
Output is correct |
32 |
Correct |
1 ms |
384 KB |
Output is correct |
33 |
Correct |
13 ms |
1508 KB |
Output is correct |
34 |
Correct |
15 ms |
1688 KB |
Output is correct |
35 |
Correct |
138 ms |
1508 KB |
Output is correct |
36 |
Correct |
18 ms |
1848 KB |
Output is correct |
37 |
Correct |
22 ms |
2016 KB |
Output is correct |
38 |
Correct |
182 ms |
1844 KB |
Output is correct |
39 |
Correct |
23 ms |
2392 KB |
Output is correct |
40 |
Correct |
27 ms |
2300 KB |
Output is correct |
41 |
Correct |
234 ms |
2224 KB |
Output is correct |
42 |
Correct |
29 ms |
2840 KB |
Output is correct |
43 |
Correct |
32 ms |
2656 KB |
Output is correct |
44 |
Correct |
299 ms |
2824 KB |
Output is correct |
45 |
Correct |
33 ms |
3104 KB |
Output is correct |
46 |
Correct |
36 ms |
3296 KB |
Output is correct |
47 |
Correct |
351 ms |
3160 KB |
Output is correct |
48 |
Correct |
40 ms |
3776 KB |
Output is correct |
49 |
Correct |
41 ms |
3640 KB |
Output is correct |
50 |
Correct |
447 ms |
3648 KB |
Output is correct |
51 |
Correct |
49 ms |
4316 KB |
Output is correct |
52 |
Correct |
51 ms |
4312 KB |
Output is correct |
53 |
Correct |
520 ms |
4220 KB |
Output is correct |
54 |
Correct |
58 ms |
4912 KB |
Output is correct |
55 |
Correct |
64 ms |
4816 KB |
Output is correct |
56 |
Correct |
674 ms |
4944 KB |
Output is correct |
57 |
Correct |
68 ms |
5556 KB |
Output is correct |
58 |
Correct |
70 ms |
5608 KB |
Output is correct |
59 |
Correct |
759 ms |
5548 KB |
Output is correct |
60 |
Correct |
78 ms |
6212 KB |
Output is correct |
61 |
Correct |
79 ms |
6224 KB |
Output is correct |
62 |
Correct |
850 ms |
6400 KB |
Output is correct |
63 |
Correct |
766 ms |
6384 KB |
Output is correct |
64 |
Correct |
945 ms |
6260 KB |
Output is correct |
65 |
Correct |
904 ms |
6204 KB |
Output is correct |
66 |
Correct |
755 ms |
6212 KB |
Output is correct |
67 |
Correct |
683 ms |
6152 KB |
Output is correct |
68 |
Correct |
331 ms |
6184 KB |
Output is correct |
69 |
Correct |
376 ms |
6184 KB |
Output is correct |
70 |
Correct |
258 ms |
6252 KB |
Output is correct |
71 |
Correct |
267 ms |
6200 KB |
Output is correct |
72 |
Correct |
101 ms |
6232 KB |
Output is correct |
73 |
Correct |
419 ms |
6380 KB |
Output is correct |
74 |
Correct |
315 ms |
6224 KB |
Output is correct |
75 |
Correct |
390 ms |
6224 KB |
Output is correct |
76 |
Correct |
342 ms |
6268 KB |
Output is correct |
77 |
Correct |
337 ms |
6320 KB |
Output is correct |
78 |
Correct |
431 ms |
6272 KB |
Output is correct |
79 |
Correct |
280 ms |
6216 KB |
Output is correct |
80 |
Correct |
322 ms |
6204 KB |
Output is correct |
81 |
Correct |
376 ms |
6408 KB |
Output is correct |
82 |
Correct |
303 ms |
6196 KB |
Output is correct |
83 |
Correct |
468 ms |
6348 KB |
Output is correct |
84 |
Correct |
387 ms |
6232 KB |
Output is correct |
85 |
Correct |
433 ms |
6308 KB |
Output is correct |
86 |
Correct |
476 ms |
6180 KB |
Output is correct |
87 |
Correct |
429 ms |
6228 KB |
Output is correct |
88 |
Correct |
588 ms |
6240 KB |
Output is correct |
89 |
Correct |
528 ms |
6172 KB |
Output is correct |
90 |
Correct |
522 ms |
6240 KB |
Output is correct |
91 |
Correct |
561 ms |
6384 KB |
Output is correct |
92 |
Correct |
516 ms |
6260 KB |
Output is correct |