POJ 3669 Meteor Shower【BFS + 预处理】

Meteor Shower
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12486 Accepted: 3374

Description

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 (XiYi) (0 ≤ X≤ 300; 0 ≤ Y≤ 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.

Input

* Line 1: A single integer: M
* Lines 2..M+1: Line i+1 contains three space-separated integers: XiYi, and Ti

Output

* Line 1: The minimum time it takes Bessie to get to a safe place or -1 if it is impossible.

Sample Input

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

Sample Output

5

恩,题目大意就是说,流星掉落下来砸到人很危险,然后一个流星落下,它所在位置包括周围4个位置都不安全,然后人的起点在原点,问人最快多长时间能到达一个绝对安全的位置。预处理,流星落下不安全的位置全标记为落下的时间,在此时间之前该点是安全的,然后若起点在0时刻就有流星落下则一定找不到安全的位置,若起点的标记仍为-1则表示该点一直不会有危险,只需0时间就找到


#include <iostream>
#include <cstdio>
#include <cstring>
#include<queue>
#define maxn 330
using namespace std;
int point[maxn][maxn];
bool vis[maxn][maxn];
int dir[5][2]={0,0,0,1,0,-1,1,0,-1,0};
int judge(int x,int y)
{
    if(x<0||y<0||x>=maxn||y>=maxn)
        return 0;
    return 1;
}
struct node
{
    int x,y,t;
};
int bfs()
{
    queue<node>q;
    if(point[0][0]==0)
        return -1;
    if(point[0][0]==-1)
        return 0;
    node now,next;
    now.x=0;
    now.y=0;
    now.t=0;
    vis[0][0]=true;
    q.push(now);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        if(point[now.x][now.y]==-1)
            return now.t;
        for(int i=1;i<5;++i)
        {
            next.x=now.x+dir[i][0];
            next.y=now.y+dir[i][1];
            next.t=now.t+1;
            if(!vis[next.x][next.y]&&judge(next.x,next.y))
            {
                if(point[next.x][next.y]==-1)
                    return next.t;
                else if(point[next.x][next.y]>next.t)
                {
                    vis[next.x][next.y]=true;
                    q.push(next);
                }
            }
        }
    }
    return -1;
}
int main()
{
    int m,x,y,t;
    while(~scanf("%d",&m))
    {
        memset(point,-1,sizeof(point));
        memset(vis,false,sizeof(vis));
        while(m--)
        {
            scanf("%d%d%d",&x,&y,&t);
            for(int i=0;i<5;++i)
            {
                int nx=x+dir[i][0];
                int ny=y+dir[i][1];
                if(judge(nx,ny))
                {
                    if(point[nx][ny]==-1)
                        point[nx][ny]=t;
                    else
                        point[nx][ny]=min(point[nx][ny],t);
                }
            }
        }
        printf("%d\n",bfs());
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值