D - Meteor Shower POJ - 3669

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

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

题意,流星会在某一时刻落在坐标上,并且该点上下左右都会有危险,求最少需要多少时间才能从0,0,移到安全的地方。

刚开始的时候进行排序,让每个坐标记录时间最小的那个数,提前用一个数组把那些流星落下时间记录下来,用数组map来保存,二维数组,最后进行搜索,map数组的初始化为-1,如果找到一个点为-1,则输出,否则,如果now.t小于改点的时间,则继续搜索。

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
int map[310][310],book[310][310];
int a[4][2]={0,1,0,-1,1,0,-1,0};
struct note
{
    int x;
    int y;
    int t;
}q[50050];
int cmp(note a,note b)
{
    return a.t<b.t;
}
int bfs()
{
    queue<note>Q;
    note now,tmp;
    now.x=0;
    now.y=0;
    now.t=0;
    Q.push(now);
    book[0][0]=1;
    while(!Q.empty())
    {
        now=Q.front();
        Q.pop();
        if(map[now.x][now.y]<=now.t)continue;
        for(int i=0;i<4;i++)
        {
            int tx=now.x+a[i][0];
            int ty=now.y+a[i][1];
            int tt=now.t+1;
            if(tx>=0&&ty>=0&&book[tx][ty]==0&&map[tx][ty]==-1)return tt;
            else if(tx>=0&&ty>=0&&book[tx][ty]==0&&map[tx][ty]>tt)
            {
                tmp.x=tx;
                tmp.y=ty;
                tmp.t=tt;
                book[tx][ty]=1;
                Q.push(tmp);
            }
        }
    }
    return -1;
}
int main()
{
    int n,i,j;
    while(~scanf("%d",&n))
    {
        for(i=0;i<302;i++)
            for(j=0;j<302;j++)
        {
            map[i][j]=-1;
            book[i][j]=0;
        }
        for(i=0;i<n;i++)
            scanf("%d%d%d",&q[i].x,&q[i].y,&q[i].t);
        sort(q,q+n,cmp);
        for(i=0;i<n;i++)
        {
            if(map[q[i].x][q[i].y]==-1)
                map[q[i].x][q[i].y]=q[i].t;
            for(j=0;j<4;j++)
            {
                int tx=q[i].x+a[j][0];
                int ty=q[i].y+a[j][1];
                if(tx>=0&&ty>=0&&map[tx][ty]==-1)
                {
                    map[tx][ty]=q[i].t;
                }
            }
        }
        int k=bfs();
        printf("%d\n",k);
    }
}

猜你喜欢

转载自blog.csdn.net/guoshuyan12/article/details/75576968