admin管理员组

文章数量:1532252

Meteor Shower

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 25860 Accepted: 6688

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 ≤ 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: 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

Source

USACO 2008 February Silver

题意:

有M个流星会分别在Ti时刻落下,落下的位置以及其上下左右都被毁掉,不能再走,求到达安全位置的时间

注意:

1、这个地图是不断变化的,我们不妨刚开始就把地图初始化好,初始把mape数组赋值为INF表示不会被炸,输入数据时,把mape[i][j]赋值为被炸掉的时间,若可能被重复炸,那么值为最小时间。

2、题中说流星降落的坐标范围:0 ≤ Xi ≤ 300; 0 ≤ Yi ≤ 300,并不是地图的范围,所以在判断点是否在地图上时,只用判断下界就可,即是否>=0,其次,定义mape,d 数组时要开的稍微大一点,300过不去……

3、注意在初始化mape、d 数组为INF时要注意你定义的数组范围,越界的话会出现 Time Limit Exceeding  或  Runtime Error 

4、bfs的结束条件是

if(mape[x][y]==INF)return d[x][y]; 

就是当走到一个永远都不会被炸的点的时候是绝对安全的,否则那个点将在某一时刻被炸,还是不安全

5、这个题隐含了一个条件,不能走走过的点

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int INF=1000000,maxn=400;
int M,maxT=0,mape[maxn][maxn],d[maxn][maxn];
typedef pair<int,int> P;
int dx[]={-1,0,1,0},dy[]={0,1,0,-1};

int bfs(){
	if(mape[0][0]==0)return -1;
	
	queue<P> que;
	que.push(P(0,0));
	d[0][0]=0;
	
	while(!que.empty()){
		P p=que.front();que.pop();
		int x=p.first,y=p.second;
		if(mape[x][y]==INF)return d[x][y]; 
		for(int i=0;i<4;i++){
			int nx=x+dx[i],ny=y+dy[i];
			if(nx>=0&&ny>=0&&d[nx][ny]==INF&&d[x][y]+1<mape[nx][ny]){
				que.push(P(nx,ny));
				d[nx][ny]=d[x][y]+1; 
			}
		}
	}
	return -1;
}

int main(){
	scanf("%d",&M);
	int x,y,t;
	for(int i=0;i<maxn;i++){
		for(int j=0;j<maxn;j++){
			d[i][j]=INF;
			mape[i][j]=INF;
		}
	}
	for(int i=0;i<M;i++){
		scanf("%d%d%d",&x,&y,&t);
		//maxT=max(maxT,t);
		if(mape[x][y]>t)mape[x][y]=t;
		for(int j=0;j<4;j++){
			int nx=x+dx[j],ny=y+dy[j];
			if(nx>=0&&ny>=0&&mape[nx][ny]>t){
				mape[nx][ny]=t;
			}
		}
	}
	
	int ans=bfs();
	printf("%d\n",ans);
}

 

本文标签: showermeteorBFS