天天看點

POJ 3669 - Meteor Shower(廣搜)

Meteor Shower

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6218 Accepted: 1836

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      

================================

題目大意:流星在t時刻砸在(x,y)點以及周圍四個點。人從(0,0)開始走,每秒走一步,上下左右都行,但是要在第一象限。問最少多少步才能走到安全區域。

思路:把map數組置為-1,預處理一下,把所有被砸的點記上砸下來的時間,依然為-1的點就是安全點了。如果到達某一點的時間大于該點被破壞的時間就不能走,直接continue。看最少多少步能走到一個-1點。

需要注意的是流星砸下來的區域在0<=x,y<=300,但是安全區域在整個第一象限。

#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cstring>

using namespace std;

int dir[4][2]={{-1,0},{0,-1},{0,1},{1,0}};
int map[333][333],vis[333][333];
int time,m;

struct point
{
    int x,y,t;
}p,s[55555];

queue <point> que;

bool cmp(point a,point b)
{
    return a.t<b.t;
}

bool check(int x,int y)
{
    if(x>=0&&y>=0) return 1;
    return 0;
}

void solve()
{
    for(int i=0;i<m;i++)
    {
        if(map[s[i].x][s[i].y]==-1)
            map[s[i].x][s[i].y]=s[i].t;
        if(map[s[i].x-1][s[i].y]==-1&&s[i].x>=1)//小心别出界
            map[s[i].x-1][s[i].y]=s[i].t;
        if(map[s[i].x][s[i].y-1]==-1&&s[i].y>=1)//小心别出界
            map[s[i].x][s[i].y-1]=s[i].t;
        if(map[s[i].x][s[i].y+1]==-1)
            map[s[i].x][s[i].y+1]=s[i].t;
        if(map[s[i].x+1][s[i].y]==-1)
            map[s[i].x+1][s[i].y]=s[i].t;
    }
}

int bfs()
{
    p.x=0;p.y=0;p.t=0;
    que.push(p);
    vis[0][0]=1;
    while(!que.empty())
    {
        point temp=que.front();
        que.pop();
        if (temp.t>=map[temp.x][temp.y]) continue;
        for(int i=0;i<4;i++)
        {
            p.x=temp.x+dir[i][0];
            p.y=temp.y+dir[i][1];
            p.t=temp.t+1;
            if(!vis[p.x][p.y])
            {
                time=p.t;
                if(check(p.x,p.y)&&map[p.x][p.y]==-1)//到達安全區域了
                {
                    return time;
                }
                else if(check(p.x,p.y)&&p.t<map[p.x][p.y])
                {
                    que.push(p);
                    vis[p.x][p.y]=1;//标記已經走過的點
                }
            }
        }
    }
    return -1;
}

int main()
{
    memset(map,-1,sizeof(map));
    memset(vis,0,sizeof(vis));
    scanf("%d",&m);
    for(int i=0;i<m;i++)
        scanf("%d%d%d",&s[i].x,&s[i].y,&s[i].t);
    sort(s,s+m,cmp);
    solve();
    /*for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
            cout<<map[i][j]<<" ";
        cout<<endl;
    }*/
    int ans=bfs();
    if(ans!=-1) printf("%d\n",ans);
    else printf("-1\n");
    return 0;
}