// IOI2009 - Mecho
#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)
string G[NN];
int N, S, D[NN][NN];
bool Vis[NN][NN];
struct Point {
int x, y;
};
bool valid(int x, int y) {
return 0 <= x && x < N && 0 <= y && y < N;
// nx < 0 || nx >= N || ny < 0 || ny >= N
}
bool check(const Point& st, const Point& ed,
int delay) { // 小熊delay再出发够不?
if (delay * S >= D[st.x][st.y]) return false;
queue<pair<int, Point> > Q; // {当前时间,当前位置}
memset(Vis, 0, sizeof(Vis)), 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')
if (x == ed.x && y == ed.y) return true; // 到家
_for(i, 0, 4) {
int nx = x + DX[i], ny = y + DY[i];
if (valid(nx, ny) && G[nx][ny] != 'T' && d + 1 < D[nx][ny] &&
!Vis[nx][ny])
// if (nx < 0 || nx >= N || ny < 0 || ny >= N || G[nx][ny] == 'T' ||
// (d + 1) >= D[nx][ny] || Vis[nx][ny])
// continue;
Q.push({d + 1, {nx, ny}}), Vis[nx][ny] = true;
}
}
return false;
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> N >> S;
queue<Point> Q;
Point st, ed;
memset(D, -1, sizeof(D));
_for(i, 0, N) {
cin >> G[i];
_for(j, 0, N) {
char& c = G[i][j];
if (c == 'H') Q.push({i, j}), D[i][j] = 0;
if (c == 'M') st = {i, j}, c = 'G'; // 初始小熊位置也改成草
if (c == 'D') ed = {i, j}; // 家的位置
}
}
while (!Q.empty()) { // BFS计算每个点到最近的蜜蜂的距离
Point p = Q.front();
Q.pop();
_for(i, 0, 4) {
int nx = p.x + DX[i], ny = p.y + DY[i], &nd = D[nx][ny];
if (valid(nx, ny) && G[nx][ny] == 'G' && nd == -1)
// if (!valid(nx, ny) || G[nx][ny] != 'G' || D[nx][ny] != -1) continue;
nd = D[p.x][p.y] + S, Q.push({nx, ny});
}
}
D[ed.x][ed.y] = N * N * S;
int L = -1, R = 2 * N * N;
while (L + 1 < R) {
int mid = (L + R) / 2; // 推迟mid出发够不够
(check(st, ed, mid) ? L : R) = mid;
}
cout << L << endl;
return 0;
}
// 2021-12-20 Mecho (IOI09_mecho) C++11 100/100 180 ms 21600 KB
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19952 KB |
Output is correct |
2 |
Correct |
10 ms |
19892 KB |
Output is correct |
3 |
Correct |
10 ms |
19960 KB |
Output is correct |
4 |
Correct |
11 ms |
19916 KB |
Output is correct |
5 |
Correct |
8 ms |
19916 KB |
Output is correct |
6 |
Correct |
9 ms |
19968 KB |
Output is correct |
7 |
Correct |
81 ms |
21416 KB |
Output is correct |
8 |
Correct |
9 ms |
19972 KB |
Output is correct |
9 |
Correct |
10 ms |
19916 KB |
Output is correct |
10 |
Correct |
9 ms |
19916 KB |
Output is correct |
11 |
Correct |
8 ms |
19916 KB |
Output is correct |
12 |
Correct |
9 ms |
19916 KB |
Output is correct |
13 |
Correct |
8 ms |
19916 KB |
Output is correct |
14 |
Correct |
9 ms |
19940 KB |
Output is correct |
15 |
Correct |
9 ms |
19916 KB |
Output is correct |
16 |
Correct |
9 ms |
20012 KB |
Output is correct |
17 |
Correct |
8 ms |
19916 KB |
Output is correct |
18 |
Correct |
9 ms |
19908 KB |
Output is correct |
19 |
Correct |
10 ms |
19948 KB |
Output is correct |
20 |
Correct |
8 ms |
19916 KB |
Output is correct |
21 |
Correct |
10 ms |
19916 KB |
Output is correct |
22 |
Correct |
9 ms |
20028 KB |
Output is correct |
23 |
Correct |
9 ms |
19916 KB |
Output is correct |
24 |
Correct |
12 ms |
20028 KB |
Output is correct |
25 |
Correct |
9 ms |
19916 KB |
Output is correct |
26 |
Correct |
10 ms |
19916 KB |
Output is correct |
27 |
Correct |
11 ms |
19916 KB |
Output is correct |
28 |
Correct |
9 ms |
19964 KB |
Output is correct |
29 |
Correct |
11 ms |
20044 KB |
Output is correct |
30 |
Correct |
12 ms |
19972 KB |
Output is correct |
31 |
Correct |
9 ms |
19916 KB |
Output is correct |
32 |
Correct |
9 ms |
20044 KB |
Output is correct |
33 |
Correct |
15 ms |
20236 KB |
Output is correct |
34 |
Correct |
11 ms |
20228 KB |
Output is correct |
35 |
Correct |
26 ms |
20216 KB |
Output is correct |
36 |
Correct |
11 ms |
20264 KB |
Output is correct |
37 |
Correct |
11 ms |
20256 KB |
Output is correct |
38 |
Correct |
41 ms |
20332 KB |
Output is correct |
39 |
Correct |
13 ms |
20360 KB |
Output is correct |
40 |
Correct |
13 ms |
20364 KB |
Output is correct |
41 |
Correct |
41 ms |
20364 KB |
Output is correct |
42 |
Correct |
13 ms |
20488 KB |
Output is correct |
43 |
Correct |
15 ms |
20520 KB |
Output is correct |
44 |
Correct |
49 ms |
20468 KB |
Output is correct |
45 |
Correct |
18 ms |
20608 KB |
Output is correct |
46 |
Correct |
16 ms |
20620 KB |
Output is correct |
47 |
Correct |
50 ms |
20556 KB |
Output is correct |
48 |
Correct |
15 ms |
20740 KB |
Output is correct |
49 |
Correct |
13 ms |
20740 KB |
Output is correct |
50 |
Correct |
60 ms |
20736 KB |
Output is correct |
51 |
Correct |
16 ms |
20804 KB |
Output is correct |
52 |
Correct |
14 ms |
20872 KB |
Output is correct |
53 |
Correct |
92 ms |
20812 KB |
Output is correct |
54 |
Correct |
17 ms |
21016 KB |
Output is correct |
55 |
Correct |
15 ms |
20940 KB |
Output is correct |
56 |
Correct |
84 ms |
21060 KB |
Output is correct |
57 |
Correct |
17 ms |
21056 KB |
Output is correct |
58 |
Correct |
16 ms |
21128 KB |
Output is correct |
59 |
Correct |
95 ms |
21052 KB |
Output is correct |
60 |
Correct |
19 ms |
21256 KB |
Output is correct |
61 |
Correct |
16 ms |
21192 KB |
Output is correct |
62 |
Correct |
119 ms |
21208 KB |
Output is correct |
63 |
Correct |
106 ms |
21236 KB |
Output is correct |
64 |
Correct |
179 ms |
21308 KB |
Output is correct |
65 |
Correct |
139 ms |
21188 KB |
Output is correct |
66 |
Correct |
112 ms |
21224 KB |
Output is correct |
67 |
Correct |
106 ms |
21228 KB |
Output is correct |
68 |
Correct |
52 ms |
21332 KB |
Output is correct |
69 |
Correct |
52 ms |
21328 KB |
Output is correct |
70 |
Correct |
37 ms |
21248 KB |
Output is correct |
71 |
Correct |
40 ms |
21248 KB |
Output is correct |
72 |
Correct |
30 ms |
21264 KB |
Output is correct |
73 |
Correct |
30 ms |
21504 KB |
Output is correct |
74 |
Correct |
54 ms |
21560 KB |
Output is correct |
75 |
Correct |
70 ms |
21496 KB |
Output is correct |
76 |
Correct |
62 ms |
21560 KB |
Output is correct |
77 |
Correct |
58 ms |
21488 KB |
Output is correct |
78 |
Correct |
71 ms |
21468 KB |
Output is correct |
79 |
Correct |
48 ms |
21548 KB |
Output is correct |
80 |
Correct |
70 ms |
21556 KB |
Output is correct |
81 |
Correct |
67 ms |
21564 KB |
Output is correct |
82 |
Correct |
56 ms |
21552 KB |
Output is correct |
83 |
Correct |
78 ms |
21512 KB |
Output is correct |
84 |
Correct |
65 ms |
21572 KB |
Output is correct |
85 |
Correct |
72 ms |
21504 KB |
Output is correct |
86 |
Correct |
71 ms |
21512 KB |
Output is correct |
87 |
Correct |
71 ms |
21452 KB |
Output is correct |
88 |
Correct |
90 ms |
21388 KB |
Output is correct |
89 |
Correct |
74 ms |
21376 KB |
Output is correct |
90 |
Correct |
86 ms |
21464 KB |
Output is correct |
91 |
Correct |
78 ms |
21460 KB |
Output is correct |
92 |
Correct |
81 ms |
21444 KB |
Output is correct |