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

题意翻译

贝茜听说了一个骇人听闻的消息:一场流星雨即将袭击整个农场,由于流星体积过大,它们无法在撞击到地面前燃烧殆尽,届时将会对它撞到的一切东西造成毁灭性的打击。很自然地,贝茜开始担心自己的安全问题。

以 Farmer John 牧场中最聪明的奶牛的名誉起誓,她一定要在被流星砸到前,到达一个安全的地方(也就是说,一块不会被任何流星砸到的土地)。

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

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

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

请你计算一下,贝茜最少需要多少时间才能到达一个安全的格子。如果不可能到达输出 −1。

Translated by @奆奆的蒟蒻 @跪下叫哥

输入输出样例

输入 #1

4

0 0 2

2 1 2

1 1 2

0 3 5

输出 #1复制

5

题目链接P2895 [USACO08FEB]Meteor Shower S - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

算是一道bfs的进阶题目了,在bfs的同时加入了一些判定条件,首先我们需要知道流星什么时候坠落,那么我们就需要开一个二维数组mp[][]来存储流行坠落的时间,有生活常识的人差不多都知道,如果流星砸过一次之后,那么就已经寸草不生了,所有我们需要在mp中记录的就是流星最早可能降落的时间,而且不仅要把该点给记录了,这个点还会影响他的上下左右四个点,所有上下左右四个点也要一起赋值。然后就是加了这么一个判断,其次还有注意边界问题,剩下的就是非常经典的dfs问题了,本AC代码参考了题解,如有侵权请联系我删除,万分感谢!!!

#include<bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;    //无穷远,初始化用
int mp[307][307];    //存放流星坠落地面的时间(感觉好像jys的rap)
bool vis[307][307];    //判断该点是否访问过了
struct pt{
    int x,y,ti;    //x坐标,y坐标,时间 
};
int dx[5] = {0,0,0,-1,1},dy[5] = {0,-1,1,0,0};    //四个方向,下标从1开始
int bfs(pt a){    //起点为a
    queue<pt> q;    //bfs板子喽!
    q.push(a);
    vis[a.x][a.y] = true;    //代表走过了
    while(!q.empty()){
        pt b = q.front();
        q.pop();
        for(int i = 1;i<=4;i++){
            pt c;
            c.x = b.x+dx[i],c.y = b.y+dy[i],c.ti = b.ti+1;    //新走到的点的信息
            if(c.x<0||c.y<0)    continue;    //越过边界了
            if(mp[c.x][c.y] == inf)    return c.ti;    //找到终点了,因为初始化的时候
            //所有的值都是inf,所有说最后在这个区间内只有没有被砸过的点的值为inf
            if(c.ti<mp[c.x][c.y] && !vis[c.x][c.y]){    //陨石没有到达,并且没有被访问过 
                q.push(c);
                vis[c.x][c.y] = true;
            } 
        }
    }
//如果最后队列为空了还没有返回值,那么返回-1证明无解
    return -1; 
} 

int main(){
    int n;
    cin >> n;
    memset(mp,0x3f,sizeof(mp));    //初始化
    for(int i = 1;i<=n;i++){
        int x,y,t;
        cin >> x >> y >> t;
        mp[x][y] = min(mp[x][y],t);
        for(int j = 1 ;j<=4;j++){
            if(x+dx[j]<0||y+dy[j]<0)    continue;    //出边界了
            mp[x+dx[j]][y+dy[j]] = min(mp[x+dx[j]][y+dy[j]],t);        //陨石最早到来的时间 
        }
    }
    pt a;
    a.x = a.y = a.ti = 0;
//从0,0开始走嘛,题意
    cout << bfs(a);
    return 0;
}

//还是要多加练习的,现在属于有思路但是没有办法a掉题,说白了还是练的太少了,希望屏幕前好学的你也可以加油努力,希望在大厂或者是竞赛的现场见到你们!!!
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值