zoukankan      html  css  js  c++  java
  • POJ 3669 Meteor Shower BFS求最小时间

    Meteor Shower

    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 31358   Accepted: 8064

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

    Input

    * Line 1: A single integer: M
    * Lines 2..M+1: Line i+1 contains three space-separated integers: Xi, Yi, 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

     

    INPUT DETAILS:
    There are four meteors, which strike points (0, 0); (2, 1); (1, 1); and (0, 3) at times 2, 2, 2, and 5, respectively.
    
    
                                                                                   
        t = 0                t = 2              t = 5
    5|. . . . . . .     5|. . . . . . .     5|. . . . . . .    
    4|. . . . . . .     4|. . . . . . .     4|# . . . . . .   * = meteor impact
    3|. . . . . . .     3|. . . . . . .     3|* # . . . . .  
    2|. . . . . . .     2|. # # . . . .     2|# # # . . . .   # = destroyed pasture
    1|. . . . . . .     1|# * * # . . .     1|# # # # . . .   
    0|B . . . . . .     0|* # # . . . .     0|# # # . . . .   
      --------------      --------------      -------------- 
      0 1 2 3 4 5 6       0 1 2 3 4 5 6       0 1 2 3 4 5 6 
     
    
    Sample Output 
    
    5
    
    OUTPUT DETAILS:
    Examining the plot above at t=5, the closest safe point is (3, 0) -- but Bessie's path to that point is too quickly blocked off by the second meteor. The next closest point is (4,0) -- also blocked too soon. Next closest after that are lattice points on the
    (0,5)-(5,0) diagonal. Of those, any one of (0,5), (1,4), and (2,3) is reachable in 5 timeunits.
    
    
    
           5|. . . . . . .   
           4|. . . . . . .   
           3|3 4 5 . . . .    Bessie's locations over time
           2|2 . . . . . .    for one solution
           1|1 . . . . . .   
           0|0 . . . . . .   
             -------------- 
             0 1 2 3 4 5 6  

    题意:Bessie从原点出发,然后有N个流星会在某个时刻落下,它们会破坏砸到的这个方格还会破坏

    四边相邻的方块,输出多少时间之后他可以到达安全的地方。如果可能,输出最优解,不可能则输出-1。

    #include<iostream>
    #include<string.h>
    #include<string>
    #include<algorithm>
    #include<queue>
    using namespace std;
    int dir[5][2]={{0,0},{0,-1},{0,1},{1,0},{-1,0}};//原点停留(处理爆炸点),上下左右
    int a[505][505];
    int n,m,cnt;
    struct node
    {
        int x;
        int y;
        int t;
    }temp,now;
    
    int check(int x,int y)
    {
        if(x>=0&&x<500&&y>=0&&y<500)
            return 1;
        else
            return 0;
    }
    
    int bfs()
    {
        if(a[0][0]==0)//刚开始走就被炸死了
            return -1;
        if(a[0][0]==-1)//起点就是安全的地方,不用走了
            return 0;
    
        temp.x=0,temp.y=0,temp.t=0;//起点
        queue<node>p;
        p.push(temp);
    
        while(!p.empty())
        {
            now=p.front();
            p.pop();
            for(int i=1;i<5;i++)//不能原地停留
            {
                int dx,dy,dt;
                dx=now.x+dir[i][0];
                dy=now.y+dir[i][1];
                dt=now.t+1;
                if(check(dx,dy)==0)//走出边界
                    continue;
                if(a[dx][dy]==-1)//到达安全区域
                    return dt;
                if(dt>=a[dx][dy])//走进爆炸区域
                    continue;
                a[dx][dy]=dt;//更新时间
                temp.x=dx;
                temp.y=dy;
                temp.t=dt;
                p.push(temp);//下一个搜索的点进队列
            }
        }
        return -1;//到不了安全区域
    }
    //bfs是从最近的点开始搜索,所以得到的第一个答案就是最近
    int main()
    {
        cin>>n;
        memset(a,-1,sizeof(a));//初始化地图所有地方都可以走
        while(n--)
        {
            int x,y,t;
            cin>>x>>y>>t;
            for(int i=0;i<5;i++)//预处理所有爆炸结束之后的地图
            {
                int dx,dy;
                dx=x+dir[i][0];
                dy=y+dir[i][1];
                if(check(dx,dy)==0)//超出地图范围
                    continue;
                if(a[dx][dy]==-1)
                    a[dx][dy]=t;
                else//if(a[dx][dy]!=-1)
                    a[dx][dy]=min(a[dx][dy],t);//取最先爆炸的时间
            }
        }
        cout<<bfs()<<endl;
    
        return 0;
    }
  • 相关阅读:
    虚函数
    类的继承
    析构
    构造
    枚举类型
    c++中的静态类型 static
    c++中的类
    sizeof和strlen的区别
    剑指36 二叉搜索书与双向链表
    剑指35 复杂链表的复制
  • 原文地址:https://www.cnblogs.com/-citywall123/p/11291476.html
Copyright © 2011-2022 走看看