您的位置:首页 > 其它

Meteor Shower POJ - 3669

2017-07-20 16:25 274 查看
Meteor Shower

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)点,为了防止
被流星击中,贝茜需要移动到安全的位置。一共有n颗流星,给
出流星落下的坐标(xi,yi)和时间ti,每一颗流星落地时上下
左右的格子也为遭受毁灭。贝茜经过的路程是不能被毁灭的。

思路:理解题意这个题就好做了,应使用一个二维数组,用来

存毁灭时间,并且需要注意的是初始化不能为零,因为可能在0

时间爆炸,所以要初始化为-1,因为毁灭后的路不能走,所以

数组应该存每个点爆炸的最小时间,且这个题应该使用book标

记路径,(我开始没标记,结果错了),可以不用优先队列。

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
int map[310][310],book[310][310];
int n,flag;
int vis[5][2]= {0,1,0,-1,1,0,-1,0,0,0};
struct note
{
int x;
int y;
int step;
};
void bfs()
{
queue<note>Q;
note p,q;
memset(book,0,sizeof(book));
p.x=0;
p.y=0;
p.step=0;
book[0][0]=1;
Q.push(p);
while(!Q.empty())
{
p=Q.front();
Q.pop();
if(map[p.x][p.y]==-1)              //-1为安全点。
{
printf("%d\n",p.step);
return ;
}
for(int i=0; i<4; i++)
{
q.x=p.x+vis[i][0];
q.y=p.y+vis[i][1];
q.step=p.step+1;
if(q.x>=0&&q.y>=0&&(map[q.x][q.y]==-1||map[q.x][q.y]>q.step))
{
if(book[q.x][q.y])
continue;
book[q.x][q.y]=1;
Q.push(q);
}
}
}
printf("-1\n");
}
int main()
{
while(~scanf("%d",&n))
{
memset(map,-1,sizeof(map));
int x,y,z;
for(int i=1; i<=n; i++)
{
scanf("%d %d %d",&x,&y,&z);
for(int j=0; j<5; j++)
{
int tx=x+vis[j][0];
int ty=y+vis[j][1];          //更新爆炸时间
if(tx<0||ty<0)
continue;
if(map[tx][ty]==-1)
map[tx][ty]=z;
else if(map[tx][ty]>z)
map[tx][ty]=z;
}
}
if(map[0][0]==0)
{
printf("-1\n");
continue;
}
bfs();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: