POJ 3669-Meteor Shower(BFS)

C - Meteor Shower
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  POJ 3669
Appoint description:  System Crawler  (2016-04-28)

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

4
0 0 2
2 1 2
1 1 2
0 3 5

Sample Output

5



AC代码:

/*
这个题目一开始没看懂,原来是有流星下落会造成周围都
被破坏,而且破坏了的地方不能再走了。这题很明显就是
bfs了,而且也不算太难,一开始超时了,原来是漏了标记
是否走过,细心就能一A的题目。
*/

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<cstdlib>
#include<queue>
using namespace std;
#define T 500
#define inf 0x3f3f3f3fL
typedef long long ll;

int vis[T][T],ts[T][T];
int fx[][2]={{1,0},{0,1},{-1,0},{0,-1}};

struct node
{
	int x,y,t;
	node(){}
	node(int _1,int _2,int _3=0):x(_1),y(_2),t(_3){}
};

int bfs()
{
	queue<node> q;
	node p;
	q.push(node(0,0));
	vis[0][0] = 1;
	while(!q.empty())
	{
		p = q.front();q.pop();
			if(ts[p.x][p.y]==inf){
					return p.t;
			}
		for(int i=0;i<4;++i){
			int tx = p.x+fx[i][0];
			int ty = p.y+fx[i][1];
			int t = p.t+1;
			if(tx>=0&&ty>=0&&t<ts[tx][ty]&&!vis[tx][ty]){
				vis[tx][ty] = 1;
				q.push(node(tx,ty,t));
			}
		}
	}
	return -1;
}
int main()
{
#ifdef zsc
	freopen("input.txt","r",stdin);
#endif

	int n,i,j,k,u,v,w;
	while(~scanf("%d",&n))
	{
		memset(vis,0,sizeof(vis));
		memset(ts,inf,sizeof(ts));
		for(i=0;i<n;++i){
			scanf("%d%d%d",&u,&v,&w);
			ts[u][v] = min(w,ts[u][v]);
			for(j=0;j<4;++j){
				int tx = u+fx[j][0];
				int ty = v+fx[j][1];
				if(tx>=0&&ty>=0){
					//这里坑了我两次因为上面直接复制黏贴,所以min里的是ts[u][v],没改过来
					ts[tx][ty] = min(w,ts[tx][ty]);
				}
			}
		}
		  printf("%d\n",bfs());
	}
	return 0;
}


你可能感兴趣的:(poj,bfs)