(bfs)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 (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<string>
#include<string.h>
#include<queue>
#include<cstdio>
using namespace std;
#define maxn 302  //301就是不对,不清楚原因
#define INF 0x3f3f3f3f
int mp[maxn][maxn],vis[maxn][maxn];
int dir[4][2]={-1,0,1,0,0,1,0,-1};

typedef struct Node{
    int x,y,step;
}Node;

int bfs(int s,int t)
{
    memset(vis,0,sizeof(vis));
    queue<Node> q;
    Node tmp,nxt;
    nxt.x=s,nxt.y=t;
    nxt.step=0;
    vis[s][t]=1;
    q.push(nxt);
    while(!q.empty()){
        tmp=q.front();
        q.pop();
        if(mp[tmp.x][tmp.y]==INF)
            return tmp.step;
        for(int i=0;i<4;i++)
        {
            int a=tmp.x+dir[i][0],b=tmp.y+dir[i][1];
            if(a>=0&&a<maxn&&b>=0&&b<maxn)
            {
                    nxt.x=a;
                    nxt.y=b;
                    nxt.step=tmp.step+1;
                if(!vis[nxt.x][nxt.y]&&nxt.step<mp[nxt.x][nxt.y])
                {
                    vis[a][b]=1;
                    q.push(nxt);
                }
            }
        }
    }

    return -1;
}

int main()
{
    int m,x,y,tmp;
    for(int i=0;i<maxn;i++)
    {
        for(int j=0;j<maxn;j++)
            mp[i][j]=INF;
    }
    cin>>m;
    for(int i=0;i<m;i++)
    {
        scanf("%d%d%d",&x,&y,&tmp);

        mp[x][y]=min(tmp,mp[x][y]);
        for(int j=0;j<4;j++)
        {
            int a=x+dir[j][0],b=y+dir[j][1];
            if(a>=0&&a<maxn&&b>=0&&b<maxn)
            {
                mp[a][b]=min(tmp,mp[a][b]);
            }
        }
    }
    cout<<bfs(0,0)<<endl;
    return 0;
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37275680/article/details/79947772
文章标签: bfs
个人分类: 搜索 - BFS
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭