[luogu p2895] [USACO08FEB]Meteor Shower S
[USACO08FEB]Meteor Shower S
题目描述
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.
牛去看流星雨,不料流星掉下来会砸毁上下左右中五个点。每个流星掉下的位置和时间都不同,求牛能否活命,如果能活命,最短的逃跑时间是多少?
输入格式
-
Line 1: A single integer: M
-
Lines 2..M+1: Line i+1 contains three space-separated integers: Xi, Yi, and Ti
输出格式
- Line 1: The minimum time it takes Bessie to get to a safe place or -1 if it is impossible.
题意翻译
贝茜听说了一个骇人听闻的消息:一场流星雨即将袭击整个农场,由于流星体积过大,它们无法在撞击到地面前燃烧殆尽,届时将会对它撞到的一切东西造成毁灭性的打击。很自然地,贝茜开始担心自己的安全问题。以FJ牧场中最聪明的奶牛的名誉起誓,她一定要在被流星砸到前,到达一个安全的地方(也就是说,一块不会被任何流星砸到的土地)。如果将牧场放入一个直角坐标系中,贝茜现在的位置是原点,并且,贝茜不能踏上一块被流星砸过的土地。 根据预报,一共有M颗流星(1 <= M <= 50,000)会坠落在农场上,其中第i颗流星会在时刻T_i (0 <= T_i <= 1,000)砸在坐标为(X_i, Y_i) (0 <= X_i <= 300;0 <= Y_i <= 300)的格子里。流星的力量会将它所在的格子,以及周围4个相邻的格子都化为焦土,当然贝茜也无法再在这些格子上行走。
贝茜在时刻0开始行动,它只能在第一象限中,平行于坐标轴行动,每1个时刻中,她能移动到相邻的(一般是4个)格子中的任意一个,当然目标格子要没有被烧焦才行。如果一个格子在时刻t被流星撞击或烧焦,那么贝茜只能在t之前的时刻在这个格子里出现。
请你计算一下,贝茜最少需要多少时间才能到达一个安全的格子。
Translated by @跪下叫哥
输入输出样例
输入 #1
4
0 0 2
2 1 2
1 1 2
0 3 5
输出 #1
5
分析
一道看似比较简单的bfs,但是实际上这道题还是有很多需要注意的点。
- 虽然这里说流星砸的位置 \(0 \le X_i, Y_i \le 300\),但是因为流行会把相邻方块变成焦土,也就意味着牛的终点坐标可能大于300,数组不要开小。
- 这里流星的顺序并不是按照T的顺序来的,也就是说输入一个流星的数值我们不能直接给数组赋值,而是要判断一下之前的流星是什么时候到的,如果之前的流星比现在的流星要早,也就是说之前的t小于现在的t,那么我们不能覆盖,只有相等或之前所有的流星都比现在这颗流星要晚(或者之前根本没有流星到过)那么我们才能赋值。我实际上并没有踩到这个坑,但据说踩到这个坑会拿到WA35的
好成绩。 - 如果出不去要输出-1.这点其实不应该算坑点的,但万一忘了呢……
但是实际上,我上边三个坑都没有踩,却犯了一个特别低级的错误。这个错误光用文字不好说,上代码才能解释明白。
那就上代码吧。
代码
/*
* @Author: crab-in-the-northeast
* @Date: 2020-06-20 15:21:17
* @Last Modified by: crab-in-the-northeast
* @Last Modified time: 2020-07-07 11:27:11
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <climits>
const int inf = INT_MAX;
struct node {
int x, y, t;
};
const int maxn = 305;
const int dx[] = {0, 1, -1, 0, 0};
const int dy[] = {0, 0, 0, 1, -1};
int T[maxn][maxn];
bool valid(int x, int y, int t) {
return x >= 0 && y >= 0 && T[x][y] > t;
}
int bfs() {
std :: queue <node> q;
node now;
now.x = now.y = now.t = 0;
q.push(now);
while (!q.empty()) {
now = q.front();
q.pop();
//T[now.x][now.y] = -1
//我之前是在这里写的,目的是判重
//但是这种判重效果并不明显,因为他是在i+1层才判重的
//也就是说,到这种状态的时候已经扩展了好多一样的状态了
for (int i = 1; i <= 4; ++i) {
int nxtx = now.x + dx[i];
int nxty = now.y + dy[i];
int nxtt = now.t + 1;
if (valid(nxtx, nxty, nxtt)) {
node nxt;
nxt.x = nxtx; nxt.y = nxty; nxt.t = nxtt;
q.push(nxt);
if (T[nxtx][nxty] == inf) return nxtt;
else T[nxtx][nxty] = -1;//在这里写才可以,在第i层判重才能保证不重复。
}
}
}
return -1;
}
int main() {
int m;
std :: scanf("%d", &m);
for (int i = 0; i < maxn; i++)
for (int j = 0; j < maxn; j++)
T[i][j] = inf;
while (m--) {
int x, y, t;
std :: scanf("%d %d %d", &x, &y, &t);
if (t < T[x][y]) T[x][y] = t;
for (int i = 1; i <= 4; i++)
if (valid(x + dx[i], y + dy[i], t))
T[x + dx[i]][y + dy[i]] = t;
}
std :: printf("%d\n", bfs());
return 0;
}