POJ 3669 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 ≤ 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


题意:

巨大流星雨即将袭来。每个流星会对击中的地方以及周围(上下左右四格)造成破坏。Bessie开始时位于(0, 0)位置,并希望逃到一处不会被袭击到的地方(在第一象限内)。已知每移动一格需要1个时间单位,被流星破坏后的地方不能再进入。给出M个流星在T时刻击中的地方(X, Y),问Bessie能否逃到安全的地方,若能输出最短时间,否则输出-1。


解题思路:

首先对矩阵进行预处理,先将矩阵置为-1表示从没有流星雨来临,然后将流星雨来临的点及其周围四格置为流星雨降临的时间,同一格有两次以上的流星雨时取时间最早的,因为求最短路不会走走过的点。然后从(0,0)开始BFS。原点处的值为-1则说明已经是安全的地方不需要移动,值为0说明开始就挂了。


感觉把矩阵的值置为INF代码要稍微简洁一点,预处理那儿就不需要分类了,BFS那儿也不需要分类了直接用maps[i][j] > metor.t 就可以了。


#include <iostream>
#include <queue>
#include <cstring>
using namespace std;

int M;
int maps[305][305];
bool vis[305][305];
int dx[5] = {0,1,0,-1,0},dy[5] = {1,0,-1,0,0};
struct metor
{
	int x,y,t;
};

int BFS()
{
	if(maps[0][0] == 0)
		return -1;
	if(maps[0][0] == -1)
		return 0;

	queue<metor> que;
	metor m;
	m.x = m.y = m.t = 0;
	que.push(m);

	memset(vis,0,sizeof(vis));
	vis[0][0] = true;

	while(que.size())
	{
		metor p = que.front();
		que.pop();

		if(maps[p.x][p.y] == -1)
			return p.t;

		for(int i = 0;i < 4;i++)
		{
			metor q;
			q.x = p.x + dx[i],q.y = p.y + dy[i];
			q.t = p.t + 1;
			if(q.x >= 0 && q.y >= 0 && !vis[q.x][q.y])
			{
				if(maps[q.x][q.y] == -1)
					return q.t;
				
				if(maps[q.x][q.y] > q.t)
				{
					vis[q.x][q.y] = true;
					que.push(q);
				}
			}
		}
	}

	return -1;
}

int main(int argc, char const *argv[])
{
	while(cin >> M)
	{
		memset(maps,-1,sizeof(maps));
		int x,y,t;
		for(int i = 0;i < M;i++)
		{
			cin >> x >> y >> t;
			for(int j = 0;j < 5;j++)
			{
				int nx = x + dx[j],ny = y + dy[j];
				if(nx >= 0 && ny >= 0)
				{
					if(maps[nx][ny] == -1)
						maps[nx][ny] = t;
					else
						maps[nx][ny] = min(maps[nx][ny],t);
				}
				//cout << maps[nx][ny] << endl;
			}
		}


			cout << BFS() << endl;
		
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值