Meteor Shower POJ - 3669

    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;
}

posted @ 2018-04-10 17:03  focus5679  阅读(195)  评论(0编辑  收藏  举报