# [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.

## 题意翻译

Translated by @跪下叫哥

## 输入输出样例

### 输入 #1

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


### 输出 #1

5


# 分析

1. 虽然这里说流星砸的位置 $0 \le X_i, Y_i \le 300$，但是因为流行会把相邻方块变成焦土，也就意味着牛的终点坐标可能大于300，数组不要开小。
2. 这里流星的顺序并不是按照T的顺序来的，也就是说输入一个流星的数值我们不能直接给数组赋值，而是要判断一下之前的流星是什么时候到的，如果之前的流星比现在的流星要早，也就是说之前的t小于现在的t，那么我们不能覆盖，只有相等或之前所有的流星都比现在这颗流星要晚（或者之前根本没有流星到过）那么我们才能赋值。我实际上并没有踩到这个坑，但据说踩到这个坑会拿到WA35的成绩。
3. 如果出不去要输出-1.这点其实不应该算坑点的，但万一忘了呢……

# 代码

/*
* @Author: crab-in-the-northeast
* @Date: 2020-06-20 15:21:17
*/
#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;
}


# 评测记录

posted @ 2020-07-07 11:33  东北小蟹蟹  阅读(124)  评论(0编辑  收藏  举报