Meteor Shower POJ - 3669 (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 meteor i will striking point (XiYi) (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.

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
  • 这道题的难点是。。。地图是一直变的。。和以前做的不一样,该怎样更新地图呢。。。。
  • 爆炸范围还好处理,就是搜索的地图该怎样呢。

     

  • 这道题还有一点比较坑,,就是在第一象限活动。。。没有上限,只要x>=0&&y>=0就好了。。要是不小心写多了,只能面对现实T T……。

  • 参考别人的思路:将爆炸时间当做地图上的数字。首先要将地图初始化,值都为-1.注意不能为0,这又是一个坑啊Q_Q.爆炸

  • 间按最小的来,并且赋给上下左右的相邻点。。判断的时候,如果此时的时间+1还小与下一个点爆炸时间,这个点还是可

  • 走的,其他的就和普通的广搜一样了。。。

  • #include <iostream>
    #include<queue>
    #include<stdio.h>
    #include<string.h>
    using namespace std;
    const int maxn=321;
    struct st
    {
        int x,y,t;
    };
    int n,mp[maxn][maxn],vis[maxn][maxn];///地图和标记数组
    int dr[5][2]= {-1,0,1,0,0,-1,0,1,0,0};///注意有五个方向,加上本身。。
    int bfs()
    {
        st p,q;
        p.x=0;
        p.y=0;
        p.t=0;
        vis[0][0]=1;
        queue<st>Q;
        Q.push(p);
        while(!Q.empty())
        {
            p=Q.front();
            Q.pop();
            if(mp[p.x][p.y]==-1)///找到一个安全点就行,爆炸区域都不是-1.。。
            {
                return p.t;
            }
            for(int i=0; i<4; i++)
            {
                q.x=p.x+dr[i][0];
                q.y=p.y+dr[i][1];
                if(q.x>=0&&q.y>=0&&(mp[q.x][q.y]==-1||p.t+1<mp[q.x][q.y])&&!vis[q.x][q.y])
                {
                    ///第一象限。。。注意如果 p.t+1<爆炸时间,是可以走的
                    q.t=p.t+1;
                    vis[q.x][q.y]=1;
                    Q.push(q);
                }
            }
        }
        return -1;
    }
    int main()
    {
        while(~scanf("%d",&n))
        {
            memset(vis,0,sizeof(vis));
            memset(mp,-1,sizeof(mp));///这里地图初始化<0就行,不过-1更好实现。
            int x,y,t;
            while(n--)
            {
                scanf("%d%d%d",&x,&y,&t);
                for(int i=0; i<5; i++) ///这里有五个方向。。。
                {
                    int tx=x+dr[i][0];
                    int ty=y+dr[i][1];
                    if(tx<0||ty<0)continue;///限定范围,超出第一象限了怎么办~~~~~
                    if(mp[tx][ty]==-1)
                    {
                        mp[tx][ty]=t;///给地图赋值啦$$
                    }
                    if(mp[tx][ty]>t)///按爆炸时间最小的来,除非不要命了,,
                        mp[tx][ty]=min(mp[tx][ty],t);
                }
            }
            if(mp[0][0]==0)
            {
                ///这个坑也要注意,只要0 0,点0秒爆炸,逃不掉的。。。。。。
                printf("-1\n");
                continue;
            }
            printf("%d\n",bfs());
        }
        return 0;
    }
    

     

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值