POJ - 3669 Meteor Shower(广搜)


Meteor Shower
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 20557 Accepted: 5345

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

Source

[Submit]   [Go Back]   [Status]   [Discuss]

题意:

        输入一个数,表示有多少个地方会被流行炸中(它和它的上下左右都会被塌陷,5个方向)

      下面每一行是爆炸地点的横坐标,纵坐标,和爆炸时间

      Bessie从原点出发(走上下左右四个方向),整个地图在第一象限(x>=0,y>=0),问Bessie至少需要多长时间能到安全的地方

思路:

        题目问最少时间,看见最少时间,可以想到用广搜,但是要注意把爆炸时间全部存在map数组中,先把数组全部赋值为-1(因为有的可能也会在时间为0的时候爆炸)。将最小的爆炸时间全部更新到map数组中,然后与走的步数进行比较,如果步数小于爆炸的时间并且没有标记走过,说明此时这个点能走。注意:走不出去要输出-1,用广搜进行搜索,直到找到安全的地方(map[now.x][now.y]==-1)为止。

   

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
int next[5][2]= {0,1,0,-1,1,0,-1,0,0,0};
int book[310][310];
int map[310][310];

struct node
{
    int x;
    int y;
    int step;
};

void bfs(int xx,int yy,int step)
{
    memset(book,0,sizeof(book));
    queue<node>q;
    node now,tmp;
    now.x=xx;
    now.y=yy;
    now.step=0;
    book[xx][yy]=1;
    q.push(now);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        if(map[now.x][now.y]==-1)   //此时可以判断为安全的地方
        {
            printf("%d\n",now.step);
            return ;
        }
        for(int i=0; i<5; i++)
        {
            tmp.x=now.x+next[i][0];
            tmp.y=now.y+next[i][1];
            tmp.step=now.step+1;
            if( tmp.x>=0&&tmp.y>=0&&(map[tmp.x][tmp.y]==-1||map[tmp.x][tmp.y]>tmp.step)&&!book[tmp.x][tmp.y])
            {
                //若此时为安全的地方(步数小于在那个点爆炸的时间或map[tmp.x][tmp.y]==-1)
                book[tmp.x][tmp.y]=1;
                q.push(tmp);
            }
        }
    }
    printf("-1\n");      //逃不出去输出-1
}

int main()
{
    int n,x,y,t,j;
    while(~scanf("%d",&n))
    {
        memset(map,-1,sizeof(map));
        for(int i=0; i<n; i++)
        {
            scanf("%d%d%d",&x,&y,&t);
            for(j=0; j<5; j++)
            {
                int nx=x+next[j][0];
                int ny=y+next[j][1];
                if(nx>=0&&ny>=0)
                {
                    if(map[nx][ny]==-1)   //更新地图上每个点爆炸的时间,它的上下左右和它本身全都更新为爆炸时间点
                        map[nx][ny]=t;
                    else
                        map[nx][ny]=min(t,map[nx][ny]);//选择最小的爆炸时间再次更新
                }
            }
        }
        if(map[0][0]==0)
        {
            printf("-1\n");
            continue;
        }
        bfs(0,0,0);
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值