POJ3669_Meteor Shower_宽搜

Meteor Shower
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 18904 Accepted: 4912

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


一个矩形区域在一段时间内下流星雨。留在某一个格内的流星会破坏这个格和相邻的四个格。0时刻从(0,0)出发,逃到一个不会背流星毁掉的安全区域,一秒钟走一格。求问至少需要多长时间。


/****************************************************************
看起来简单,做起来吼麻烦的一个题。更何况弱一开始思路就确定的不好
样例都过不了,服气后看了网上聚聚的代码,才算A掉了

首先,用一个二维数组 Map 保存所有点被毁掉的最早时间
也就是保存地图信息

然后在这个地图上进行宽搜就可以了
和普通宽搜不同的一点是,如果点被毁掉的时间早于到达点的时间
Map[x][y] <= Step[t.first][t.second] + 1
则这个点不能走

安全的点就是地图上被毁掉的时间为 无穷大 的点
也就把问题转化成 求到这样的点的最小时间
****************************************************************/
#include<cstdio>
#include<iostream>
#include<queue>

using namespace std;

const int inf = 33333;//无穷大
int Map[400][400];//地图记录每个点被毁掉的最早时间
int Step [400][400];//记录到达这个点的最早时间
int M;//流星个数
queue<P> qu;//宽搜用的队列
typedef pair<int, int> P;
int my[5] = {0, 1, -1, 0, 0};//表示点的邻居用的遍历数组
int mx[5] = {0, 0, 0, 1, -1};

void bfs ()//宽搜
{
	while( qu.size() ){

		P t = qu.front();qu.pop();

		for(int i=1; i<5; i++){

			int x = t.first + mx[i], y = t.second + my[i];

			//点不在第一象限,点之前已经到达过,点的毁灭时间比到达时间早
			if( x < 0 || y < 0 || Step[x][y] != inf ||
				 Map[x][y] <= Step[t.first][t.second] + 1 ) continue;

			//记录点的最早到达时间
			Step[x][y] = Step[t.first][t.second] + 1;

			//如果地图上这个点被毁掉的最早时间是 无穷大
			//这个点始终没有被流星撞击,是安全位置
			//输出到达此点的最小时间
			if( Map[x][y] == inf ){
				printf("%d\n", Step[x][y]);
				return;
			}

			qu.push( P(x, y) );
		}

	}

	//一直走到无路可走也没有发现不曾毁灭的点
	printf("-1\n");
}

//撞毁函数
void destory (int x, int y, int t)
{
	for(int i=0; i<5; i++){//遍历它本身以及周围的四个点

		int xx = x + mx[i], yy = y + my[i];

		// Map[xx][yy]<t 表示这个点之前就被毁掉了
		if( xx < 0 || yy < 0 || Map[xx][yy] < t) continue;

		//记录被毁掉的最早时间
		Map[xx][yy] = t;
	}
}

//初始化函数
//把点被毁的时间初始化为 inf
//把到达点的时间初始化为 inf
void init ()
{
	for(int i= 0; i<= 390; i++)
		for(int j= 0; j<= 390; j++)
			Map[i][j] = Step[i][j] = inf;
}

int main ()
{
	init();

	scanf("%d", &M);

	//输入流星信息,毁掉点
	int x, y, t;
	while( M-- ){
		scanf("%d %d %d", &x, &y, &t);
		destory(x, y, t);
	}

	if( Map[0][0] == 0 ){
		printf("-1\n");
		return 0;
	}

	qu.push( P(0,0) );
	Step[0][0] = 0;
	bfs();

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值