Meteor Shower POJ - 3669 (bfs+優先隊列)

Meteor Shower
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 26455   Accepted: 6856

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

Inputcode

* Line 1: A single integer: M
* Lines 2..M+1: Line i+1 contains three space-separated integers: XiYi, and Tiblog

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


題意:流星來襲,在一個方格中,貝茜在(0,0)點,爲了防止被流星擊中,貝茜須要移動到安全的位置。一共有n顆流星,給出流星落下的座標(xi,yi)和時間ti,每一顆流星落地時上下左右的格子也爲遭受毀滅。貝茜通過的路程是不能被毀滅的。

 

題解:首先要建圖,用map[][]數組儲存好每一個點遭受流星打擊的時間,不受流星打擊的用-1涵蓋。(不能用0覆蓋,部分點可能在0時刻遭受打擊) 而後遍歷整張圖,找到不受流星打擊的點便可,由於要求最短期,全部要用到優先隊列,優先時間最小的點出隊列。根據題意咱們知道在圖內的不受流星打擊的點和沒有通過流星打擊的點能進入隊列。

 

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn=304;

int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
int maze[maxn][maxn];
int vis[maxn][maxn];

struct node
{
    int x,y,step;
}temp,a;
bool operator < (const node &a,const node &b)
{
    return a.step>b.step;
}

int min(int a,int b)
{
    return a>b?b:a;
}
void bfs()
{
    int i;
    memset(vis,0,sizeof(vis));
    priority_queue<node>q;
    a.x=0; a.y=0;
    a.step=0;
    q.push(a);
    vis[0][0]=1;
    while(!q.empty())
    {
        a=q.top();
        q.pop();
        if(maze[a.x][a.y]==-1)
        {
            cout<<a.step<<endl;
            return ;
        }
        for(i=0;i<4;i++)
        {
            temp.x=a.x+dx[i];
            temp.y=a.y+dy[i];
            temp.step=a.step+1;
            if(temp.x>=0 && temp.y>=0 && (maze[temp.x][temp.y]==-1 || maze[temp.x][temp.y]>temp.step))
            {
                if(!vis[temp.x][temp.y])
                    q.push(temp);
                vis[temp.x][temp.y]=1;
            }
        }
        
    }
    cout<<-1<<endl;
    
    
}

int main()
{
    int n,x,y,t;
    memset(maze,-1,sizeof(maze));
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>x>>y>>t;
        if(maze[x][y]==-1)
            maze[x][y]=t;
        else
            maze[x][y]=min(maze[x][y],t);
        for(int j=0;j<4;j++)
        {
            int nx=x+dx[j];
            int ny=y+dy[j];
            if(nx>=0 && ny>=0)
            {
              if(maze[nx][ny]==-1)
                  maze[nx][ny]=t;
              else
                  maze[nx][ny]=min(t,maze[nx][ny]);
            }
        }
        
    }
    bfs();
    return 0;
}
相關文章
相關標籤/搜索