POJ 3669 Meteor Shower(bfs+优先队列)

Meteor Shower

Time Limit: 1000MS


Memory Limit: 65536K

Total Submissions: 12487


Accepted: 3375

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 (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,每一颗流星落地时上下左右的格子也为遭受毁灭。贝茜经过的路程是不能被毁灭的。


题解:首先要建图,用map[][]数组储存好每个点遭受流星打击的时间,不受流星打击的用-1涵盖。(不能用0覆盖,部分点可能在0时刻遭受打击) 然后遍历整张图,找到不受流星打击的点即可,因为要求最短时间,所有要用到优先队列,优先时间最小的点出队列。根据题意我们知道在图内的不受流星打击的点和没有经过流星打击的点能进入队列。


代码如下:


#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int map[310][310];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int mark[310][310];
struct node
{
	int x,y,step;
}temp,a;
bool operator < (const node &a,const node &b)
{
	return a.step>b.step;
} 

int min(int a,int b)
{
	return a>b?b:a;
}

void bfs()
{
	int i;
	memset(mark,0,sizeof(mark));
	priority_queue<node>q;
	a.x=0;
	a.y=0;
	a.step=0;
	q.push(a);
	mark[0][0]=1;
	while(!q.empty())
	{
		a=q.top();
		q.pop();
		if(map[a.x][a.y]==-1)
		{
			printf("%d\n",a.step);
			return ;
		}
		for(i=0;i<4;++i)
		{
			temp.x=a.x+dir[i][0];
			temp.y=a.y+dir[i][1];
			temp.step=a.step+1;
			if(temp.x>=0&&temp.y>=0&&(map[temp.x][temp.y]==-1||map[temp.x][temp.y]>temp.step))
			{
				if(!mark[temp.x][temp.y])
				q.push(temp);
				mark[temp.x][temp.y]=1;
			}
		}
	}
	printf("-1\n");
}



int main()
{
	int n,x,y,t,i,j;
	while(scanf("%d",&n)!=EOF)
	{
		memset(map,-1,sizeof(map));
		for(i=0;i<n;++i)
		{
			scanf("%d%d%d",&x,&y,&t);
			if(map[x][y]==-1)//判断是否受过撞击时间的更新 
				map[x][y]=t;
			else
				map[x][y]=min(map[x][y],t);//受过撞击后的点不能再走了 
			for(j=0;j<4;++j)
			{
				int nx=x+dir[j][0];
				int ny=y+dir[j][1];
				if(nx>=0&&ny>=0)
				{
					if(map[nx][ny]==-1)
						map[nx][ny]=t;
					else
						map[nx][ny]=min(t,map[nx][ny]); 
				}
			}
		}
		bfs();
	}
	return 0;
} 




你可能感兴趣的:(POJ 3669 Meteor Shower(bfs+优先队列))