您的位置:首页 > 其它

Meteor Shower POJ - 3669

2017-07-21 11:15 169 查看

 Meteor Sh

ower

 POJ- 3669 

Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors willcrash 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, saferlocation while avoiding being destroyed by meteors along her way.The reports say that M meteors (1 ≤ M ≤ 50,000) will strike, with meteor i willstriking 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 alsothe four rectilinearly adjacent lattice points.Bessie leaves the origin at time 0 and can travel in the first quadrant and parallel to theaxes 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 TiOutput* 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
题意:输入一个n,有n组坐标和n个时间,每组坐标将会在t时刻被轰炸,而且他的四个周边坐标也会被轰炸,轰炸时间以及轰炸后都不能行走,现在有一个人在原点,他可以上下左右移动,并且每动一步花费一时刻,问最少多少时间他能走到安全区(只要没被轰炸的地区都是安全区)。
题解:这题用广搜写,先将轰炸区和轰炸区的四周全部标记起来,然后开始广搜,广搜要注意区域被轰炸后无法经过,必须在区域轰炸之前走,直到走到安全区结束。
代码:
#include<stdio.h>#include<string.h>#include<queue>#include<algorithm>using namespace std;int mp[350][350],book[350][350];int n,sum,a[4][2]={0,1,1,0,0,-1,-1,0};struct node{int x,y,t;}q[50005];int cmp(node A,node B){return A.t<B.t;}int bfs(){queue<node>e;//定义queue容器node now,tmp;now.x=0;now.y=0;now.t=0;e.push(now);//将起点压入队列book[0][0]=1;while(!e.empty()){now=e.front();e.pop();//将队首元素出队if(mp[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&&mp[tx][ty]==-1)//如果是安全区结束搜索return tt;else if(tx>=0&&ty>=0&&book[tx][ty]==0&&mp[tx][ty]>tt)//tx,ty的轰炸时间在tt后,可以行走{tmp.x=tx;tmp.y=ty;tmp.t=tt;book[tx][ty]=1;e.push(tmp);}}}return -1;}int main(){while(~scanf("%d",&n)){int i,j;for(i=0;i<310;i++)for(j=0;j<310;j++){mp[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(mp[q[i].x][q[i].y]==-1)mp[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&&mp[tx][ty]==-1)mp[tx][ty]=q[i].t;}}int k=bfs();//广搜printf("%d\n",k);}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: