[POJ](3669)Meteor Shower ---- bfs+预处理

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 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

题意:
有M颗流星将会在 ti时刻的(xi,yi)处掉落,掉落后,它周围的四个点(上下左右)将无法行走。现在一个小女孩从(0,0)处,0时刻开始移动,每移动一下,时刻+1,她要在不被流星砸到的前提下,找到一个安全的地方(没有流星和流星毁灭的周围)。问找到安全地方的最短时刻是多少,找不到则输出-1。

思路:既然要寻找最短时刻,就用bfs。我们可以把所有图中所有点的时刻置为INF,而且要对图中每个流星掉落点和其周围进行预处理,即流星下落的最小时刻。这要保证当前的时刻t < 流星下落的时刻,则可以一直移动,直到找到一个安全的位置(t = INF时)。

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
typedef struct Node
{
    int x;
    int y;
}node;
const int MAX = 300+5;
const int INF = 0x3f3f3f3f;
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
int time[MAX][MAX];
bool vis[MAX][MAX];//标记
int bfs()
{
    queue<node>q;
    node st,now,nex;
    st.x = st.y =0;
    if(time[st.x][st.y] == 0)
        return -1;
    q.push(st);
    int t[MAX][MAX];
    memset(t,0,sizeof(t));
    memset(vis,false,sizeof(vis));
    while(!q.empty())
    {
        now = q.front();
        q.pop();
        if(time[now.x][now.y] == INF)
            return t[now.x][now.y];
        for(int i=0;i<4;i++)
        {
            nex.x = now.x+dir[i][0];
            nex.y = now.y+dir[i][1];
            if(nex.x>=0 && nex.y>=0 && (t[now.x][now.y]+1<time[nex.x][nex.y]) && !vis[nex.x][nex.y])
            {
                vis[nex.x][nex.y] = true;
                t[nex.x][nex.y] = t[now.x][now.y]+1;
                q.push(nex);
            }
        }
    }
    return -1;//到达不了最安全的地方
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL),cout.tie(NULL);
    int m;
    while(cin>>m){
        int x,y,t;
        memset(time,0x3f,sizeof(time));
        for(int i=0;i<m;i++)
        {
            cin>>x>>y>>t;
            time[x][y] = min(time[x][y],t);
            for(int i=0;i<4;i++) //预处理每个地点有流星下落的最小时刻
            {
                int nx = x+dir[i][0];
                int ny = y+dir[i][1];
                if(nx>=0 && ny>=0)
                    time[nx][ny] = min(time[nx][ny],t);
            }
        }
        int ans = bfs();
        cout<<ans<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值