ACM Meteor Shower(挑战程序设计竞赛)

Meteor Shower

Time Limit: 1000ms
Memory Limit: 65536KB
This problem will be judged on  PKU. Original ID:  3669
64-bit integer IO format:  %lld      Java class name:  Main
Type:   

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.php

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.ios

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).app

Determine the minimum time it takes Bessie to get to a safe place.ide

Input

* Line 1: A single integer: M* Lines 2..M+1: Line i+1 contains three space-separated integers: XiYi, and Tiui

Output

* Line 1: The minimum time it takes Bessie to get to a safe place or -1 if it is impossible.spa

Sample Input

4
0 0 2
2 1 2
1 1 2
0 3 5

Sample Output

5

Source

#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;

#define MAX_X 310
#define MAX_T 1001
#define MAX_M 50010
#define MAX_Q 100000
#define ADD(x) x=(x+1)%MAX_Q

struct Node
{
	int x,y;
	int time;
} queue[MAX_Q];
struct Dat
{
	int x,y,t;
} Data[MAX_M];
int front,rear;
int M;
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
bool map[MAX_X][MAX_X];
bool nosafe[MAX_X][MAX_X];
bool visited[MAX_X][MAX_X];
bool dT[MAX_T];

int cmp(const void *a,const void *b)
{
	Dat *pa,*pb;

	pa=(Dat *)a;
	pb=(Dat *)b;

	return pa->t-pb->t;
}

int main()
{
	int now=0;
	scanf("%d",&M);

	for(int i=0;i<M;i++)
	{
		scanf("%d%d%d",&Data[i].x,&Data[i].y,&Data[i].t);
		nosafe[Data[i].x][Data[i].y]=true;
		for(int j=0;j<4;j++)
		{
			int nx=Data[i].x+dx[j],ny=Data[i].y+dy[j];
			if(nx>=0 && nx<=301 && ny>=0 && ny<=301)
				nosafe[nx][ny]=true;
		}
	}

	qsort(Data,M,sizeof(Dat),cmp);

	front=rear=0;
	queue[rear]=Node{0,0,0};
	visited[0][0]=true;
	ADD(rear);

	int ans=-1;
	Node q;

	while(front!=rear)
	{
		q=queue[front];
		ADD(front);

		if(!dT[q.time])
		{
			dT[q.time]=true;
			for(;Data[now].t==q.time;now++)
			{
				map[Data[now].x][Data[now].y]=true;
				for(int i=0;i<4;i++)
				{
					int nx=Data[now].x+dx[i],ny=Data[now].y+dy[i];

					if(nx>=0 && nx<=301 && ny>=0 && ny<=301)
					{
						map[nx][ny]=true;
					}
				}
			}
		}

		if(map[q.x][q.y])
		{
			continue;
		}

		if(!nosafe[q.x][q.y])
		{
			ans=q.time;

			break;
		}

		for(int i=0;i<4;i++)
		{
			int nx=q.x+dx[i],ny=q.y+dy[i];
			if(nx>=0 && nx<=301 && ny>=0 && ny<=301 && !visited[nx][ny] && !map[nx][ny])
			{
				queue[rear]=Node{nx,ny,q.time+1};
				ADD(rear);
				visited[nx][ny]=true;
			}
		}
	}

	cout<<ans<<endl;

	return 0;
}