POJ 3669 Meteor Shower BFS求最小時間

Meteor Shower

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

Descriptionnode

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

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).ui

Determine the minimum time it takes Bessie to get to a safe place.spa

Inputcode

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

Outputthree

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

Sample Inputip

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;
}
相關文章
相關標籤/搜索