poj3669 流星雨

来源:互联网 发布:中信国安 大数据 编辑:程序博客网 时间:2024/04/28 05:50

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 meteori will striking point (Xi,Yi) (0 ≤ Xi≤ 300; 0 ≤ Yi ≤ 300) at timeTi (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:Xi, Yi, andTi

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 <cstdio>#include <queue>#include <algorithm>#include <cstring>#define MAX_M 50100using namespace std;const int dx[] = {-1,1,0,0,0};const int dy[] = {0,0,1,-1,0};typedef struct{int x,y,t;}P;queue<P> que;int map[500][500],m,last_mer;P mer[MAX_M];bool vis[500][500];inline bool inmap(int x,int y){return x >= 0 && y >= 0;}int bfs(){    P current;current.x = current.y = current.t = 0;    que.push(current);    while(!que.empty())    {        current = que.front();que.pop();        for(int i = 0;i < 4;i++)        {            int nx = current.x + dx[i],ny = current.y + dy[i];            int time = current.t + 1;            if(inmap(nx,ny) && map[nx][ny] > time && !vis[nx][ny])            {                vis[nx][ny] = true;                if(map[nx][ny] > last_mer)                {                    return time;                }                P now;now.x = nx;now.y = ny;now.t = time;                que.push(now);            }        }    }    return -1;}int main(){    scanf("%d",&m);    for(int i = 1;i <= m;i++)    {        scanf("%d%d%d",&mer[i].x,&mer[i].y,&mer[i].t);    }    memset(map,0x3f,sizeof(map));    for(int i = 1;i <= m;i++)    {        last_mer = max(last_mer,mer[i].t);        for(int j = 0;j < 5;j++)        {            int nx = mer[i].x + dx[j],ny = mer[i].y + dy[j];            if(inmap(nx,ny) && map[nx][ny] > mer[i].t)            {                map[nx][ny] = mer[i].t;            }        }    }    if(map[0][0] == 0)    {        printf("-1\n");    }    else    {        printf("%d\n",bfs());    }    return 0;}


 

 

 

0 0
原创粉丝点击