即日起在codingBlog上分享您的技术经验即可获得积分,积分可兑换现金哦。

POJ – 3669 Meteor Shower(图论/BFS无权最短路)

编程语言 karry_zzj 15℃ 0评论
本文目录
[隐藏]

问题描述


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

1.分析:

这题6啊

代码如下:

#include
#include
#include
#include
using namespace std;
const int maxn = 300+10;
int map[maxn][maxn];
int M;
int dir[5][2] = {{-1,0},{1,0},{0,-1},{0,1},{0,0}};
struct place{
    int x,y,t;
}s;

int BFS()
{
    if(map[0][0]==0) return -1;
    if(map[0][0]==-1) return 0;
    queue q;
    q.push(s);
    while(!q.empty())
    {
        place v = q.front(); q.pop();
        for(int i=0; i<5; i++)
        {
            s.x = v.x + dir[i][0];
            s.y = v.y + dir[i][1];
            s.t = v.t + 1;
            if(s.x<0||s.x>maxn||s.y<0||s.y>maxn) continue;
            if(map[s.x][s.y]==-1) return s.t;
            if(map[s.x][s.y]>s.t)
            {
                map[s.x][s.y] = s.t;
                q.push(s);
            }
        }
    }
    return -1;
}

void Init()
{
    s.x = 0; s.y = 0; s.t = 0;
    memset(map,-1,sizeof(map));
    scanf("%d",&M);
    while(M--)
    {
        int x,y,t;
        scanf("%d%d%d",&x,&y,&t);
        for(int i=0; i<5; i++)
        {
            int dx = x + dir[i][0];
            int dy = y + dir[i][1];
            if(dx < 0||dx >= maxn||dy < 0||dy >= maxn)  continue;  
            if(map[dx][dy]==-1) map[dx][dy] = t;
            else map[dx][dy] = min(map[dx][dy],t);
        }
    }
}

int main()
{
    Init();
    printf("%d\n",BFS());
    return 0;
}

转载请注明:CodingBlog » POJ – 3669 Meteor Shower(图论/BFS无权最短路)

喜欢 (0)or分享 (0)
发表我的评论
取消评论

*

表情