Meteor Shower

来源:互联网 发布:矩阵归一化处理 编辑:程序博客网 时间:2024/06/06 19:44





Meteor Shower
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 20653 Accepted: 5373

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

40 0 22 1 21 1 20 3 5

Sample Output

5

题意:有M炸弹,将在ti时间上在(xi,yi)点爆炸,爆炸方向为包括当前点在内的上下左右五个方向,问能否跑到安全区域。

注:走过的点不能再走。

思路:用BFS

#include<stdio.h>#include<algorithm>#include<queue>#include<stdio.h>#include<string.h>#include<queue>using namespace std;const int N = 305,INF=1010;struct node{    int x,y;    bool f;    int t;    int s;}p[N][N];bool vis[N][N];int step;bool cmp(int x,int y){    return x<=N&&x>=0&&y<=N&&y>=0;}int dx[5]={0,0,0,1,-1};//包括当前点在内的上下左右五个方向int dy[5]={0,1,-1,0,0};queue<node> q;node p1,p2;void bfs(){    int i,j,k;    while(!q.empty())    {        p1=q.front();        q.pop();        if(p1.f==0) {step=p1.s;break;}        for(k=1;k<5;k++)        {            i=p1.x+dx[k];            j=p1.y+dy[k];            if(!vis[i][j]&&cmp(i,j))            {                if(p1.s+1<p[i][j].t)                {                    p[i][j].s=p1.s+1;                    q.push(p[i][j]);                    vis[i][j]=1;                }            }        }    }}int main(){    int m,i,x,y,t,j;    while(scanf("%d",&m)!=EOF)    {        for(i=0;i<N;i++)            {            for(j=0;j<N;j++)            {                p[i][j].x=i;                p[i][j].y=j;                p[i][j].f=0;                p[i][j].t=INF;            }        }        for(i=0;i<m;i++)        {            scanf("%d%d%d",&x,&y,&t);            for(j=0;j<5;j++)            {                int a=x+dx[j],b=y+dy[j];                if(cmp(a,b))                {                    p[a][b].f=1;                    p[a][b].t=min(p[a][b].t,t);                }            }        }        while(!q.empty()) q.pop();        p[0][0].s=0;        q.push(p[0][0]);        vis[0][0]=1;        step=-1;        memset(vis,0,sizeof(vis));//初始化        bfs();        printf("%d\n",step);    }    return 0;}


原创粉丝点击