Meteor Shower POJ - 3669

Bessie需要在一个被流星雨袭击的坐标平面上找到安全的位置。本篇介绍了一个算法解决方案,通过广度优先搜索(BFS)来寻找从原点到达安全地点所需的最短时间。考虑了流星撞击的时间与位置,确保Bessie能够避开所有危险。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    Meteor Shower

  POJ - 3669 

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

题解+解析:

#include <iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include <cstring>
#include <queue>
typedef long long ll;
using namespace std;
int move[5][2] = {1,0,0,1,-1,0,0,-1,0,0};
struct Meteor
{
	int x, y, t;
	Meteor(int _x, int _y, int _t){x = _x; y= _y; t=_t;}
	Meteor(){}
};
typedef Meteor P;
Meteor input[50000];
int map[350][350];
bool visit[350][350]; //不走重复的,重复走的点说明该走法一定不是最优的。 不判重的话回TLE。
int last = -1;
int bfs()
{
	memset(visit, 0, sizeof(visit));
	queue<P> que;
	P cor(0,0,0);
	que.push(cor);
	while (que.size())
	{
		cor = que.front();
		for (int i = 0; i < 4; i++)
		{
			int nx = cor.x+move[i][0], ny = cor.y+move[i][1];
			if (0<=nx&&0<=ny&&cor.t+1<map[nx][ny] && !visit[nx][ny])
			{
				visit[nx][ny] = true;
				que.push(P(nx,ny,cor.t+1));
				if (last < map[nx][ny])
				{
					return cor.t+1;
				}
			}
		}
		que.pop();
	}
	return -1;
}
int main()
{
	memset(map, 0x7f, sizeof(map));//用最大值初始化地图。
	int m;
	scanf("%d", &m);
	for (int i = 0; i < m; i++)
	{
		scanf("%d%d%d",&input[i].x, &input[i].y, &input[i].t);
		if (last < input[i].t)
		{
			last = input[i].t;
		}
	}
	for (int i = 0; i < m; i++)//将流星雨填入地图
	{
		for (int j = 0; j < 5; j++)
		{
			int nx = input[i].x + move[j][0], ny = input[i].y + move[j][1];
			if (0<=nx&&nx<301 && 0<=ny&&ny<301)
			{
				map[nx][ny] = min(map[nx][ny],input[i].t);//一个点有可能会被轰炸两次,必须取最小的那一次。
			}
		}
	}
	printf("%d\n", bfs());

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值