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
         这道题的难点是。。。地图是一直变的。。和以前做的不一样,该怎样更新地图呢。。。。爆炸范围还好处理,就是搜索的地图该怎样呢。

   这道题还有一点比较坑,,就是在第一象限活动。。。没有上限,只要x>=0&&y>=0就好了。。要是不小心写多了,只能面对现实T T……。

参考别人的思路:将爆炸时间当做地图上的数字。首先要将地图初始化,值都为-1.注意不能为0,这又是一个坑啊Q_Q.爆炸时间按最小的来,并且赋给上下左右的相邻点。。判断的时候,如果此时的时间+1还小与下一个点爆炸时间,这个点还是可以走的,其他的就和普通的广搜一样了。。。

#include<stdio.h>
#include<queue>
#include<algorithm>
using namespace std;
#include<string.h>
int dir[5][2]= {0,1,1,0,0,-1,-1,0,0,0};   //注意有五个方向,加上本身。。
int map[320][320],vis[320][320];        //地图和标记数组
struct note
{
    int x,y,t;
};
int bfs()
{
    queue<note>Q;
    note p,q;
    p.x=0;
    p.y=0;
    p.t=0;
    vis[0][0]=1;
    Q.push(p);
    while(!Q.empty())
    {
        p=Q.front();
        Q.pop();
        if(map[p.x][p.y]==-1)  //找到一个安全点就行,爆炸区域都不是-1.。。
         {
            return p.t;
        }
        for(int i=0;i<4;i++)   //注意这个方向变为了四个,不能不动对吧~
        {
            q.x=p.x+dir[i][0];
            q.y=p.y+dir[i][1];
            if(q.x>=0&&q.y>=0&&(map[q.x][q.y]==-1||p.t+1<map[q.x][q.y])&&!vis[q.x][q.y]) //第一象限。。。注意如果 p.t+1<爆炸时间,是可以走的

            {
                q.t=p.t+1;
                vis[q.x][q.y]=1;
                Q.push(q);
            }
        }
    }
    return -1;
}
int main()
{
    int m;
    while(~scanf("%d",&m))
    {
        int u,v,w;
        memset(vis,0,sizeof(vis));  
        memset(map,-1,sizeof(map));    //这里地图初始化<0就行,不过-1更好实现。
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            for(int j=0;j<5;j++)        //这里有五个方向。。。
            {
                int tx=u+dir[j][0];
                int ty=v+dir[j][1];
                if(tx<0||ty<0)continue;   //限定范围,超出第一象限了怎么办~~~~~
                if(map[tx][ty]==-1)map[tx][ty]=w;      //给地图赋值啦$$
                if(map[tx][ty]>w)map[tx][ty]=min(map[tx][ty],w);  //按爆炸时间最小的来,除非不要命了,,
            }
        }
        if(map[0][0]==0)                  //这个坑也要注意,只要0 0,点0秒爆炸,逃不掉的。。。。。。
        {
            printf("-1\n");
            continue;
        }
        printf("%d\n",bfs());
    }
 return 0;
}


posted on 2017-07-21 08:48  zitian246  阅读(104)  评论(0编辑  收藏  举报