POJ3669 Meteor Shower【BFS】

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

 解题思路

题目大意:有m(50000)个陨石落到一个300*300的地图上,(x,y,t),其在时刻t落到(x,y),摧毁该区域及四方向相邻的区域。一个区域被摧毁后不能再进入。问从原点、0时刻开始最少需要多少时间能够逃离到安全位置。

思路:这里我们使用BFS,先用一个time[][]数组保存所有坐标的最早摧毁时间,如果不会被摧毁这初始化为inf。然后用一个safe[][]数组保存到达每一个点的早时间。

接下来开始进行BFS,注意当我们到达一个点的time[][]==inf表示该点是安全的,这时只需要输出结果即可。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
#define inf 0x3f3f3f3f

int dx[4] = { 0,0,-1,1 };
int dy[4] = { -1,1,0,0 };

int time[330][330], safe[330][330];
int m;

struct Point
{
	int x;
	int y;
};

void BFS()
{
	queue<Point> myque;
	Point p = { 0,0 };
	safe[0][0] = 0;
	myque.push(p);
	while (!myque.empty())
	{
		Point tmp = myque.front();
		myque.pop();
		if (time[tmp.x][tmp.y] == inf)
		{
			printf("%d\n", safe[tmp.x][tmp.y]);
			return;
		}
		for (int i = 0; i < 4; i++)
		{
			int xtmp = tmp.x + dx[i];
			int ytmp = tmp.y + dy[i];
			if (xtmp < 0 || xtmp >= 330 || ytmp < 0 || ytmp >= 330) continue;   //越界
			if (safe[tmp.x][tmp.y] + 1 >= time[xtmp][ytmp]) continue;  //危险区域
            if (safe[xtmp][ytmp] < inf) continue;  //该区域已被搜索到
			safe[xtmp][ytmp] = safe[tmp.x][tmp.y] + 1;
			Point p;
			p.x = xtmp;
			p.y = ytmp;
			myque.push(p);
		}
	}
	printf("-1\n");
}

int main()
{
	fill(time[0], time[0] + 330 * 330, inf);   //现将每个点的最早危险时间初始化为无穷大
	fill(safe[0], safe[0] + 330 * 330, inf);   //现将每个点的最早到达时间初始化为无穷大
	scanf("%d", &m);
	while (m--)    //先预处理每个陨石造成的每个点的最早摧毁时间
	{
		int x, y, t;
		scanf("%d%d%d", &x, &y, &t);
		time[x][y] = min(time[x][y], t);
		for (int i = 0; i < 4; i++)
		{
			int xtmp = x + dx[i];
			int ytmp = y + dy[i];
			if(xtmp>=0&&xtmp<=330&&ytmp>=0&&ytmp<=330)
				time[xtmp][ytmp] = min(time[xtmp][ytmp], t);
		}
	}
	BFS();
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胡小涛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值