P2895 [USACO08FEB]Meteor Shower S

题目链接:https://www.luogu.com.cn/problem/P2895

题目描述

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之前的时刻在这个格子里出现。

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

输入输出样例

输入 #1

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

输出 #1

5

进入队列的操作

pair对组,入队操作

typedef pair<int, int> P;
queue<P> que;
que.push(P(0, 0))

struct结构体,入队操作

struct st{
	int x, y, t;
};
queue<st> que;
que.push({0, 0, 0});

这道题有一个坑点:陨石落下的范围是0——300,但是网格并不是只有0——300。

比较好的做法:

1)用 dest 数组来记录每个位置被毁灭的时间,先初始化为INF。如果某个位置 (x, y) 的 dest[x][y]=INF,就说明这个地方不会被毁灭。

2)如果能够到达不会被毁灭的地方,就能逃亡成功。如果不能到达不会被毁灭的地方,就不能逃亡成功。

#include <bits/stdc++.h>
using namespace std;
const int N = 305, INF = 0x3f3f3f3f;
 
typedef pair<int, int> P; 

int m;//陨石数目 
int dest[N][N];//每个位置毁灭的时间 
int t[N][N];//走过的距离,也可以看成时间 
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};

//记录每个位置毁灭的时间 
void op(int x, int y, int t)
{
    dest[x][y] = min(dest[x][y], t);
    for (int i = 0; i < 4; i++){
        int nx = x + dx[i], ny = y + dy[i];
        if (nx >= 0 && ny >= 0)
            dest[nx][ny] = min(dest[nx][ny], t);
    }
}

int bfs()
{
	//初始化距离数组
	memset(t, INF, sizeof t);
    int step = -1;//最终的步数
    queue<P> que;
    que.push(P(0, 0));
    t[0][0] = 0;
    
    while (que.size()){
	    
        P p = que.front(); que.pop();
        //最后没有被毁灭 
        if (dest[p.first][p.second] == INF){
            step = t[p.first][p.second];
            break;
        }
        //下一秒的走法
        for (int i = 0; i < 4; i++){
            int nx = p.first + dx[i], ny = p.second + dy[i];
            //dest[nx][ny]>t[p.first][p.second]+1 表示到达新位置时,新位置没有被毁灭 
            if (0 <= nx && nx <= 302 && ny >=0 && ny <= 302 && dest[nx][ny] > t[p.first][p.second] + 1 && t[nx][ny] == INF){
                que.push(P(nx, ny));
                t[nx][ny] = t[p.first][p.second] + 1;
            }
        }
    }
    
    return step;
}

int main(void)
{
	int x, y, t; 
	memset(dest, INF, sizeof dest);
	cin >> m;
    for (int i = 0; i < m; i++){
        cin >> x >> y >> t;
        //记录每个位置毁灭的时间 
        op(x, y, t);
    }
    int res = bfs();
    cout << res << endl;
    
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43772166/article/details/106708721