您的位置:首页 > 其它

Meteor Shower POJ - 3669

2017-07-21 09:19 302 查看





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



 题意:一个人从(0,0)点出发,出发时间也为0,每单位时间走单位长度,求用最少多少时间走到安全点,所谓安全点就是没有遭到轰炸的区域,先输入的一个数你,以下有n行,每行三个数分别是两个坐标和该坐标遭轰炸的时间点,轰炸范围包括改点以及该点的上下左右一共5个点。


思路及方法:本题用到广度优先搜索,首先将轰炸点的时间用一个二维数组标记出来,如果有重合的,将此点标记为较小时间的,因为此点轰炸前肯定能走轰炸后无论如何都不能再走了,再轰炸一次也是一样,然后就是一个标准的广搜题了,只不过判断的条件多了点,多了两个一个是部轰炸的区域可以进还有轰炸前的区域可以走,结束条件一定是不轰炸的区域,输出时间,如果走不出来就输出-1.




#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
int n,mp[303][303],book[303][303],f=0;
int nx[5][2]= {0,0,0,1,1,0,0,-1,-1,0};
struct node
{
int x,y,s;
} q[50010];
void bfs(int x,int y)
{
int head=1,tail=2;
q[head].x=x;
q[head].y=y;
q[head].s=0;
while(head<tail)
{
for(int i=1; i<5; i++)
{
int tx=q[head].x+nx[i][0];
int ty=q[head].y+nx[i][1];
if(tx>=0&&ty>=0&&!mp[ty][tx]&&(book[ty][tx]==INF||q[head].s+1<book[ty][tx]))
{
if(book[ty][tx]==INF)
{
f=1;
printf("%d\n",q[head].s+1);
return ;
}
mp[ty][tx]=1;
q[tail].x=tx;
q[tail].y=ty;
q[tail].s=q[head].s+1;
tail++;
}
}
head++;
}
}
void biaoji(int x,int y,int z)
{
for(int i=0; i<5; i++)
{
int tx=x+nx[i][0];
int ty=y+nx[i][1];
if(tx>=0&&ty>=0)
book[ty][tx]=min(book[ty][tx],z);
}
}
int main()
{
while(~scanf("%d",&n))
{
int a,b,c;
memset(mp,0,sizeof(mp));
memset(book,INF,sizeof(book));
for(int i=0; i<n; i++)
{
scanf("%d%d%d",&a,&b,&c);
biaoji(a,b,c);
}
f=0;
if(book[0][0]==0)
{
printf("-1\n");
continue;
}
bfs(0,0);
if(!f)printf("-1\n");
}
}




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法 广搜 搜索