Meteor Shower

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 (Xi, Yi) (0 ≤ Xi ≤ 300; 0 ≤ Yi ≤ 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: Xi, Yi, 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

刚开始以为是个图论或者贪心题,就错了两遍,然后才发现是个搜索题。题意的话就是给你一个图,然后天上掉炸弹,炸弹的威力范围是自身下落地点的上下左右(包括下落地点),然后给你炸弹下落得时间,让你判断多少时间之后主人公是安全的。广搜的时候要注意那个方向数组,包括自身。

扫描二维码关注公众号,回复: 2883137 查看本文章

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
struct node
{
    int x,y,t;
};
int map[400][400],book[400][400],m;
int s[5][2]= {0,0,0,1,1,0,0,-1,-1,0};
int bfs()
{
    node st,en;
    queue<node> Q;
    st.x=0;
    st.y=0;
    st.t=0;
    book[0][0]=1;
    Q.push(st);
    while(!Q.empty())
    {
        st=Q.front();
        Q.pop();
        if(map[st.x][st.y]==-1)
        {
            return st.t;
        }
        for(int i=1; i<=4; i++)
        {
            int tx=st.x+s[i][0];
            int ty=st.y+s[i][1];
            if(tx<=m-1&&ty<=m-1&&tx>=0&&ty>=0&&(map[tx][ty]==-1||map[tx][ty]>st.t+1 )&&!book[tx][ty])
            {
                en.x=tx;
                en.y=ty;
                en.t=st.t+1;
                book[tx][ty]=1;
                Q.push(en);
            }
        }
    }
    return -1;
}
int main()
{
    while(~scanf("%d",&m))
    {
        memset(map,-1,sizeof(map));
        memset(book,0,sizeof(book));
        int x,y,z,flag=0;
        for(int i=0; i<m; i++)
        {
            scanf("%d%d%d",&x,&y,&z);
            for(int i=0; i<5; i++)
            {
                int tx =x+s[i][0];
                int ty =y+s[i][1];
                if(tx<0||ty<0||tx>=m||ty>=m)
                    continue;
                if(map[tx][ty]==-1)
                    map[tx][ty]=z;
                else
                    map[tx][ty]=min(map[tx][ty],z);
            }
        }
        if(map[0][0]==0)
        {
            flag=1;
            continue;
        }

        if(flag)
            printf("-1\n");
        else
        {
            int s=bfs();
            printf("%d\n",s);
        }
    }
    return 0;
}
 

猜你喜欢

转载自blog.csdn.net/aini875/article/details/81734886