洛谷—— P2895 [USACO08FEB]流星雨Meteor Shower

本文介绍了一道关于牛躲避流星雨袭击的安全路径寻找问题。通过预处理流星撞击影响并采用广度优先搜索(BFS),算法寻找从起点到安全地点的最短路径。输入包括流星的数量及各自的位置和时间,输出则是到达安全地点所需的最短时间。

 P2895 [USACO08FEB]流星雨Meteor Shower

题目描述

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.

 

输入输出样例

输入样例#1:
4
0 0 2
2 1 2
1 1 2
0 3 5
输出样例#1:
5

搜索
bfs,先预处理出每一个炸弹爆炸时对其每一个格子造成的影响,然后去一个最小值,然后bfs搜到第一个永远不会被炸的地方,输出答案,如果没有这样的地方,直接输出-1
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define  N 500
#define maxn 0x3f3f3f3f
using namespace std;
bool flag;
int n,x,y,t,fx,fy,dis[N][N],map[N][N];
int xx[4]={0,0,1,-1},yy[4]={1,-1,0,0};
int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();}
    return x*f;
}
struct Node
{
    int x,y;
}que;
int main()
{
    n=read();
    memset(map,maxn,sizeof(map));
    for(int i=1;i<=n;i++)
    {
        x=read(),y=read(),t=read();
        map[x][y]=min(map[x][y],t);
        for(int i=0;i<4;i++)
        {
            fx=x+xx[i],fy=y+yy[i];
            if(fx<0||fy<0) continue;
            map[fx][fy]=min(map[fx][fy],t);
        }
    }
    queue<Node>q;
    que.x=0,que.y=0;
    memset(dis,maxn,sizeof(dis));
    q.push(que);dis[0][0]=0;
    while(!q.empty())
    {
        Node p=q.front();q.pop();
        if(map[p.x][p.y]==maxn) {printf("%d",dis[p.x][p.y]); return 0;}
        for(int i=0;i<4;i++)
        {
            int nx=p.x+xx[i],ny=p.y+yy[i];
            if(nx<0||ny<0||map[nx][ny]<=dis[p.x][p.y]+1) continue;
            if(dis[nx][ny]<=dis[p.x][p.y]+1) continue;
            dis[nx][ny]=dis[p.x][p.y]+1;
            que.x=nx,que.y=ny;
            q.push(que);
        }
        flag=true;
    }
    if(flag) printf("-1");
    return 0;
}

 



转载于:https://www.cnblogs.com/z360/p/7594928.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值