Meteor Shower POJ

来源:互联网 发布:走台步的基本技巧 知乎 编辑:程序博客网 时间:2024/06/13 03:16

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

题意:

流星砸地,主人公需要跑到安全地方,题中数据是坐标和时间,流星匝地该点及其周围四个临近点都破坏,

#include<stdio.h>#include <string.h>#include <queue>#include <math.h>#define N 550using namespace std;int map[N][N];int Min(int x,int y){    return x<y?x:y;}int dir[5][2]= {0,0,0,1,0,-1,1,0,-1,0};struct node{    int x,y,t;};int bfs(){    if(map[0][0]==0) return -1;//危险地点    if(map[0][0]==-1) return 0;//安全    node tmp,now;    tmp.x=0;    tmp.y=0;    tmp.t=0;    queue<node >Q;    Q.push(tmp);    while(!Q.empty())    {        now=Q.front();        Q.pop();        for(int i=1; i<5; i++)        {            tmp.x=now.x+dir[i][0];            tmp.y=now.y+dir[i][1];            tmp.t=now.t+1;            if(tmp.x<0||tmp.y<0||tmp.x>=N||tmp.y>=N)//判断是否出界限                continue;            if(map[tmp.x][tmp.y]==-1)//找到安全地带退出                return tmp.t;            if(tmp.t>=map[tmp.x][tmp.y])//到达改点的时间大于等于被毁坏的时间都不行                continue;            map[tmp.x][tmp.y]=tmp.t;//更新走到该点的短时间            Q.push(tmp);        }    }    return -1;}int main(){    int m,x,y,t;    while(~scanf("%d",&m))    {        memset(map,-1,sizeof(map));        while(m--)        {            scanf("%d%d%d",&x,&y,&t);            int tmpx,tmpy;            for(int i=0; i<5; i++) //构建地图            {                tmpx=x+dir[i][0];                tmpy=y+dir[i][1];                if(tmpx<0||tmpx>=N||tmpy<0||tmpy>=N)                    continue;                if(map[tmpx][tmpy]==-1)                    map[tmpx][tmpy]=t;//输入点的覆盖,原先默认安全的地点                else                    map[tmpx][tmpy]=Min(map[tmpx][tmpy],t);            }        }        int sum=bfs();        printf("%d\n",sum);    }    return 0;}