⭐每日一题⭐专栏

written by SJTU-XHW

本人学识有限,解析难免有错,恳请读者能够批评指正,本人将不胜感激!


LuoGu P2895. [USACO08FEB] Meteor Shower S

Description

Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they hit. Anxious for her safety, she vows to find her way to a safe location (one that is never destroyed by a meteor) . She is currently grazing at the origin in the coordinate plane and wants to move to a new, safer location while avoiding being destroyed by meteors along her way.

The reports say that M meteors (1 ≤ M ≤ 50,000) will strike, with meteor i will striking point (Xi, Yi) (0 ≤ Xi ≤ 300; 0 ≤ Yi ≤ 300) at time Ti (0 ≤ Ti ≤ 1,000). Each meteor destroys the point that it strikes and also the four rectilinearly adjacent lattice points.

Bessie leaves the origin at time 0 and can travel in the first quadrant and parallel to the axes at the rate of one distance unit per second to any of the (often 4) adjacent rectilinear points that are not yet destroyed by a meteor. She cannot be located on a point at any time greater than or equal to the time it is destroyed).

Determine the minimum time it takes Bessie to get to a safe place.

Traslation

贝茜听说一场特别的流星雨即将到来:这些流星会撞向地球,并摧毁它们所撞击的任何东西。她为自己的安全感到焦虑,发誓要找到一个安全的地方(一个永远不会被流星摧毁的地方)。

如果将牧场放入一个直角坐标系中,贝茜现在的位置是原点,并且,贝茜不能踏上一块被流星砸过的土地。

根据预报,一共有 $M$ 颗流星 $(1\leq M\leq 50,000)$ 会坠落在农场上,其中第 $i$ 颗流星会在时刻 $T_i$ 砸在坐标为 $(X_i,Y_i)(0\leq X_i\leq 300$,$0\leq Y_i\leq 300)$ 的格子里。流星的力量会将它所在的格子,以及周围 $4$ 个相邻的格子都化为焦土,当然贝茜也无法再在这些格子上行走。

贝茜在时刻 $0$ 开始行动,它只能在第一象限中,平行于坐标轴行动,每 $1$ 个时刻中,她能移动到相邻的(一般是 $4$ 个)格子中的任意一个,当然目标格子要没有被烧焦才行。如果一个格子在时刻 $t$ 被流星撞击或烧焦,那么贝茜只能在 $t$ 之前的时刻在这个格子里出现。 贝西一开始在 $(0,0)$。

请你计算一下,贝茜最少需要多少时间才能到达一个安全的格子。

Rules

Input Format

* Line 1: A single integer: M

* Lines 2..M+1: Line i+1 contains three space-separated integers: Xi, Yi, and Ti

Output Format

* Line 1: The minimum time it takes Bessie to get to a safe place or -1 if it is impossible.

Sample Input #1

1
2
3
4
5
4
0 0 2
2 1 2
1 1 2
0 3 5

Sample Output #1

1
5

Ideas & Solution

流星坠落的位置和时间有关,如果给定的数据是按时间顺序排序的,那么离散事件驱动模拟;否则直接将最早坠落时间作为数组的值。本题没有说明是按照时间排序,本人也不打算排序,所以采用后者的方法。不妨开一个 $301\times301$ 的数组用以存储坐标地图;

如果使用最早坠落时间作为数组的值,那么遍历的时候应该使用 BFS 队列,确保同一时间的同样距离;

另外有个重要的点,题目中说 “流星坠落的范围是 $(0,0)\sim(300,300)$”,但 Bessie 可以在第一象限 $(0,0)\sim(+\infty,+\infty)$、平行坐标轴移动,这意味着 Bessie 最终的位置可以在流星坠落的区域外(如果跑出去就立即安全,无需再读入流星坠落的事件);

下面我还想到一个优化方法,就是 BFS 遍历的时候,可以加上一个 VISITED 标记,减小遍历的计算量;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

constexpr int INF = -1;
constexpr int VISITED = -2;
int area[301][301];

struct footprint {
int x;
int y;
int curTime;
bool isValid() const {
return (x >= 0 && x <= 300 && y >= 0 && y <= 300);
}
};

inline void disable(int x, int y, int t) {
if (x >= 0 && x <= 300 && y >= 0 && y <= 300 && (area[x][y] == INF || t < area[x][y]))
area[x][y] = t;
}

int main() {
memset(area, INF, sizeof(area));
int M; scanf("%d", &M);
for (int i = 0; i < M; ++i) {
int xi, yi, ti;
scanf("%d%d%d", &xi, &yi, &ti);
disable(xi, yi, ti);
disable(xi - 1, yi, ti);
disable(xi, yi - 1, ti);
disable(xi + 1, yi, ti);
disable(xi, yi + 1, ti);
}
queue<footprint> tasks; footprint start = {0,0,0};
tasks.push(start);
while (!tasks.empty()) {
footprint cur = tasks.front(), next; tasks.pop();
// 脱离区域 或者 永无陨石,成功
if (!cur.isValid() || area[cur.x][cur.y] == INF) {
printf("%d", cur.curTime); return 0;
}
if (cur.curTime >= area[cur.x][cur.y]) continue; // 此路不行
next.x = cur.x; next.y = cur.y - 1; next.curTime = cur.curTime + 1;
if (next.y >= 0 && area[next.x][next.y] != VISITED)
tasks.push(next); // 不允许出第一象限
next.y = cur.y + 1;
if (area[next.x][next.y] != VISITED)
tasks.push(next);
next.y = cur.y; next.x = cur.x - 1;
if (next.x >= 0 && area[next.x][next.y] != VISITED)
tasks.push(next);
next.x = cur.x + 1;
if (area[next.x][next.y] != VISITED)
tasks.push(next);
area[cur.x][cur.y] = VISITED; // 不走回头路,可以减小计算量
}
printf("-1");
return 0;
}

轻松一遍过 ~


评论
昼夜切换阅读模式