poj 3669 Meteor Shower(bfs)

问题描述

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 meteori 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.


样例输入

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

样例输出

5


题目大意就是陨石雨撞地球,你坐上飞机开始逃跑,陨石砸过的点以及周围4个点都不能经过,问你最少时间到安全地方去,即陨石砸不到的地方。

首先把图预处理,先存inf,然后把输入的点以及周围四个点的值都改为陨石落下的时间,然后bfs,bfs的时候要注意:只有这个点为inf或者当前时间+1> 这个点的值才能走,否则就不能走,走过的地方把他改为0,就变相的不能走回头路了。


代码如下:


#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;

const int inf=9999999;

struct node
{
    int x,y,t;
    node(int a,int b,int c)
    {
        x=a;y=b;t=c;
    }
};

int n,ma[305][305];

queue<node>que;

int dx[]={1,0,0,-1};
int dy[]={0,1,-1,0};

int bfs()
{
    node no1(0,0,0);
    ma[0][0]=0;
    que.push(no1);
    while(!que.empty())
    {
        node no2=que.front();
        que.pop();
        for(int i=0;i<4;i++)
        {
            int xx=no2.x+dx[i];
            int yy=no2.y+dy[i];
            if(xx<0||yy<0||no2.t+1>=ma[xx][yy])
                continue;
            if(ma[xx][yy]==inf)
                return no2.t+1;
            ma[xx][yy]=0;
            node no3(xx,yy,no2.t+1);
            que.push(no3);
        }
    }
    return -1;
}

int main()
{
    scanf("%d",&n);
    for(int i=0;i<305;i++)
            for(int j=0;j<305;j++)
            ma[i][j]=inf;
    for(int i=0;i<n;i++)
    {
        int x,y,c;
        scanf("%d %d %d",&x,&y,&c);

        for(int i=0;i<4;i++)
        {
            int xx=x+dx[i];
            int yy=y+dy[i];
            if(xx>=0&&yy>=0)
                ma[xx][yy]=min(c,ma[xx][yy]);
        }
        ma[x][y]=min(c,ma[x][y]);
    }
    printf("%d\n",bfs());
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值