#include <bits/stdc++.h>
using namespace std;
const int NN = 2000 + 4, DX[4] = {1, -1, 0, 0}, DY[4] = {0, 0, 1, -1};
#define _for(i, a, b) for (int i = (a); i < (int)(b); ++i)
char G[NN][NN];
int N, S, B[NN][NN];
bool Vis[NN][NN];
struct Point {
int x, y;
};
bool check(const Point& st, int delay) {
if (delay * S >= B[st.x][st.y]) return false;
memset(Vis, 0, sizeof(Vis));
queue<pair<int, Point> > Q;
Q.push({delay * S, st}), Vis[st.x][st.y] = true;
while (!Q.empty()) {
auto p = Q.front();
Q.pop();
int d = p.first, x = p.second.x, y = p.second.y;
if (G[x][y] == 'D') return true;
_for(i, 0, 4) {
int nx = x + DX[i], ny = y + DY[i];
if (nx < 0 || nx >= N || ny < 0 || ny >= N || G[nx][ny] == 'T' ||
(d + 1) >= B[nx][ny] || Vis[nx][ny])
continue;
Q.push({d + 1, {nx, ny}}), Vis[nx][ny] = true;
}
}
return false;
}
int main() {
cin >> N >> S;
queue<Point> Q;
Point Honey, Home;
memset(B, -1, sizeof(B));
_for(i, 0, N) {
cin >> ws;
_for(j, 0, N) {
char& c = G[i][j];
cin >> c;
if (c == 'H') Q.push({i, j}), B[i][j] = 0;
if (c == 'M') Honey = {i, j}, c = 'G';
if (c == 'D') Home = {i, j};
}
}
while (!Q.empty()) {
Point p = Q.front();
Q.pop();
_for(i, 0, 4) {
Point np{p.x + DX[i], p.y + DY[i]};
if (np.x < 0 || np.x >= N || np.y < 0 || np.y >= N ||
G[np.x][np.y] != 'G' || B[np.x][np.y] != -1)
continue;
B[np.x][np.y] = B[p.x][p.y] + S, Q.push({np.x, np.y});
}
}
B[Home.x][Home.y] = N * N * S;
int L = -1, R = 2 * N * N;
while (L + 1 < R) {
int mid = (L + R) / 2;
(check(Honey, mid) ? L : R) = mid;
}
cout << L << endl;
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:57:21: warning: 'Home.Point::x' may be used uninitialized in this function [-Wmaybe-uninitialized]
57 | B[Home.x][Home.y] = N * N * S;
| ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
mecho.cpp:57:21: warning: 'Home.Point::y' may be used uninitialized in this function [-Wmaybe-uninitialized]
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19916 KB |
Output is correct |
2 |
Correct |
9 ms |
19880 KB |
Output is correct |
3 |
Correct |
9 ms |
19856 KB |
Output is correct |
4 |
Correct |
9 ms |
19880 KB |
Output is correct |
5 |
Correct |
9 ms |
19968 KB |
Output is correct |
6 |
Correct |
9 ms |
19916 KB |
Output is correct |
7 |
Correct |
112 ms |
22280 KB |
Output is correct |
8 |
Correct |
8 ms |
19916 KB |
Output is correct |
9 |
Correct |
8 ms |
19884 KB |
Output is correct |
10 |
Correct |
9 ms |
19916 KB |
Output is correct |
11 |
Correct |
9 ms |
19880 KB |
Output is correct |
12 |
Correct |
9 ms |
20056 KB |
Output is correct |
13 |
Correct |
9 ms |
19984 KB |
Output is correct |
14 |
Correct |
9 ms |
20064 KB |
Output is correct |
15 |
Correct |
9 ms |
19904 KB |
Output is correct |
16 |
Correct |
9 ms |
20044 KB |
Output is correct |
17 |
Correct |
10 ms |
19916 KB |
Output is correct |
18 |
Correct |
9 ms |
19916 KB |
Output is correct |
19 |
Correct |
10 ms |
19916 KB |
Output is correct |
20 |
Correct |
9 ms |
19888 KB |
Output is correct |
21 |
Correct |
12 ms |
20008 KB |
Output is correct |
22 |
Correct |
9 ms |
19916 KB |
Output is correct |
23 |
Correct |
9 ms |
20044 KB |
Output is correct |
24 |
Correct |
10 ms |
20036 KB |
Output is correct |
25 |
Correct |
9 ms |
19960 KB |
Output is correct |
26 |
Correct |
10 ms |
20044 KB |
Output is correct |
27 |
Correct |
12 ms |
20044 KB |
Output is correct |
28 |
Correct |
10 ms |
20044 KB |
Output is correct |
29 |
Correct |
11 ms |
20044 KB |
Output is correct |
30 |
Correct |
10 ms |
20044 KB |
Output is correct |
31 |
Correct |
11 ms |
20104 KB |
Output is correct |
32 |
Correct |
10 ms |
20044 KB |
Output is correct |
33 |
Correct |
18 ms |
20664 KB |
Output is correct |
34 |
Correct |
19 ms |
20676 KB |
Output is correct |
35 |
Correct |
37 ms |
20736 KB |
Output is correct |
36 |
Correct |
21 ms |
20784 KB |
Output is correct |
37 |
Correct |
21 ms |
20864 KB |
Output is correct |
38 |
Correct |
38 ms |
20840 KB |
Output is correct |
39 |
Correct |
24 ms |
20904 KB |
Output is correct |
40 |
Correct |
24 ms |
20932 KB |
Output is correct |
41 |
Correct |
51 ms |
21020 KB |
Output is correct |
42 |
Correct |
26 ms |
21040 KB |
Output is correct |
43 |
Correct |
26 ms |
21060 KB |
Output is correct |
44 |
Correct |
65 ms |
21060 KB |
Output is correct |
45 |
Correct |
31 ms |
21288 KB |
Output is correct |
46 |
Correct |
31 ms |
21264 KB |
Output is correct |
47 |
Correct |
66 ms |
21316 KB |
Output is correct |
48 |
Correct |
36 ms |
21444 KB |
Output is correct |
49 |
Correct |
33 ms |
21444 KB |
Output is correct |
50 |
Correct |
82 ms |
21432 KB |
Output is correct |
51 |
Correct |
38 ms |
21572 KB |
Output is correct |
52 |
Correct |
36 ms |
21548 KB |
Output is correct |
53 |
Correct |
92 ms |
21556 KB |
Output is correct |
54 |
Correct |
43 ms |
21756 KB |
Output is correct |
55 |
Correct |
46 ms |
21844 KB |
Output is correct |
56 |
Correct |
116 ms |
21788 KB |
Output is correct |
57 |
Correct |
49 ms |
21864 KB |
Output is correct |
58 |
Correct |
47 ms |
21936 KB |
Output is correct |
59 |
Correct |
134 ms |
21916 KB |
Output is correct |
60 |
Correct |
55 ms |
22244 KB |
Output is correct |
61 |
Correct |
51 ms |
22048 KB |
Output is correct |
62 |
Correct |
139 ms |
22048 KB |
Output is correct |
63 |
Correct |
135 ms |
22088 KB |
Output is correct |
64 |
Correct |
229 ms |
22148 KB |
Output is correct |
65 |
Correct |
186 ms |
22172 KB |
Output is correct |
66 |
Correct |
142 ms |
22044 KB |
Output is correct |
67 |
Correct |
157 ms |
22016 KB |
Output is correct |
68 |
Correct |
85 ms |
22100 KB |
Output is correct |
69 |
Correct |
86 ms |
22128 KB |
Output is correct |
70 |
Correct |
69 ms |
22072 KB |
Output is correct |
71 |
Correct |
78 ms |
22044 KB |
Output is correct |
72 |
Correct |
61 ms |
22084 KB |
Output is correct |
73 |
Correct |
64 ms |
22316 KB |
Output is correct |
74 |
Correct |
94 ms |
22308 KB |
Output is correct |
75 |
Correct |
93 ms |
22316 KB |
Output is correct |
76 |
Correct |
89 ms |
22356 KB |
Output is correct |
77 |
Correct |
88 ms |
22340 KB |
Output is correct |
78 |
Correct |
111 ms |
22428 KB |
Output is correct |
79 |
Correct |
80 ms |
22340 KB |
Output is correct |
80 |
Correct |
93 ms |
22376 KB |
Output is correct |
81 |
Correct |
96 ms |
22340 KB |
Output is correct |
82 |
Correct |
93 ms |
22436 KB |
Output is correct |
83 |
Correct |
111 ms |
22272 KB |
Output is correct |
84 |
Correct |
95 ms |
22340 KB |
Output is correct |
85 |
Correct |
101 ms |
22216 KB |
Output is correct |
86 |
Correct |
101 ms |
22336 KB |
Output is correct |
87 |
Correct |
101 ms |
22320 KB |
Output is correct |
88 |
Correct |
104 ms |
22212 KB |
Output is correct |
89 |
Correct |
111 ms |
22212 KB |
Output is correct |
90 |
Correct |
119 ms |
22340 KB |
Output is correct |
91 |
Correct |
110 ms |
22180 KB |
Output is correct |
92 |
Correct |
115 ms |
22244 KB |
Output is correct |