POJ 3669 - Meteor Shower(广搜)

11 篇文章 0 订阅

Meteor Shower
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6218 Accepted: 1836

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


================================

题目大意:流星在t时刻砸在(x,y)点以及周围四个点。人从(0,0)开始走,每秒走一步,上下左右都行,但是要在第一象限。问最少多少步才能走到安全区域。


思路:把map数组置为-1,预处理一下,把所有被砸的点记上砸下来的时间,依然为-1的点就是安全点了。如果到达某一点的时间大于该点被破坏的时间就不能走,直接continue。看最少多少步能走到一个-1点。

需要注意的是流星砸下来的区域在0<=x,y<=300,但是安全区域在整个第一象限。


#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cstring>

using namespace std;

int dir[4][2]={{-1,0},{0,-1},{0,1},{1,0}};
int map[333][333],vis[333][333];
int time,m;

struct point
{
    int x,y,t;
}p,s[55555];

queue <point> que;

bool cmp(point a,point b)
{
    return a.t<b.t;
}

bool check(int x,int y)
{
    if(x>=0&&y>=0) return 1;
    return 0;
}

void solve()
{
    for(int i=0;i<m;i++)
    {
        if(map[s[i].x][s[i].y]==-1)
            map[s[i].x][s[i].y]=s[i].t;
        if(map[s[i].x-1][s[i].y]==-1&&s[i].x>=1)//小心别出界
            map[s[i].x-1][s[i].y]=s[i].t;
        if(map[s[i].x][s[i].y-1]==-1&&s[i].y>=1)//小心别出界
            map[s[i].x][s[i].y-1]=s[i].t;
        if(map[s[i].x][s[i].y+1]==-1)
            map[s[i].x][s[i].y+1]=s[i].t;
        if(map[s[i].x+1][s[i].y]==-1)
            map[s[i].x+1][s[i].y]=s[i].t;
    }
}

int bfs()
{
    p.x=0;p.y=0;p.t=0;
    que.push(p);
    vis[0][0]=1;
    while(!que.empty())
    {
        point temp=que.front();
        que.pop();
        if (temp.t>=map[temp.x][temp.y]) continue;
        for(int i=0;i<4;i++)
        {
            p.x=temp.x+dir[i][0];
            p.y=temp.y+dir[i][1];
            p.t=temp.t+1;
            if(!vis[p.x][p.y])
            {
                time=p.t;
                if(check(p.x,p.y)&&map[p.x][p.y]==-1)//到达安全区域了
                {
                    return time;
                }
                else if(check(p.x,p.y)&&p.t<map[p.x][p.y])
                {
                    que.push(p);
                    vis[p.x][p.y]=1;//标记已经走过的点
                }
            }
        }
    }
    return -1;
}

int main()
{
    memset(map,-1,sizeof(map));
    memset(vis,0,sizeof(vis));
    scanf("%d",&m);
    for(int i=0;i<m;i++)
        scanf("%d%d%d",&s[i].x,&s[i].y,&s[i].t);
    sort(s,s+m,cmp);
    solve();
    /*for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
            cout<<map[i][j]<<" ";
        cout<<endl;
    }*/
    int ans=bfs();
    if(ans!=-1) printf("%d\n",ans);
    else printf("-1\n");
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值