Meteor Shower (poj 3669 bfs)

46 篇文章 0 订阅

Language:
Meteor Shower
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9929 Accepted: 2771

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

Source


题意:M个陨石撞击地球,给出M个陨石的撞击坐标和时刻,每个陨石撞击点的相邻四个点也被摧毁,一旦被摧毁就不能走了,问在(0,0)处的人最少要费多长时间走到安全点。

思路:一个地方可能会被多个陨石摧毁多次,那么就先求出每个点被摧毁的最早时间,然后再bfs。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 305
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

struct Meteor
{
    int x,y;
    int t;
};

int dir[4][2]={1,0,0,1,-1,0,0,-1};
int MeNum;
bool vis[maxn][maxn];
int time[maxn][maxn];   //记录(x,y)处被摧毁的最早时间

bool IsOk(int x,int y)
{
    if (x>=0&&x<=maxn&&y>=0&&y<=maxn)
        return true;
    return false;
}

int bfs()
{
    int i;
    if (time[0][0]==0) return -1;  //如果起点一开始就被摧毁就不可能逃出,返回-1
    queue<Meteor>Q;
    mem(vis,false);
    Meteor st,now;
    st.x=0;
    st.y=0;
    st.t=0;
    vis[0][0]=true;
    Q.push(st);
    while (!Q.empty())
    {
        st=Q.front();
        Q.pop();
        if (time[st.x][st.y]==INF)
            return st.t;
        FRL(i,0,4)
        {
            now.x=st.x+dir[i][0];
            now.y=st.y+dir[i][1];
            now.t=st.t+1;
            if (IsOk(now.x,now.y)&&!vis[now.x][now.y]&&now.t<time[now.x][now.y]) //走到当前点的时候要没有被访问且该点还没有被摧毁
            {
                vis[now.x][now.y]=true;
                Q.push(now);
            }
        }
    }
    return -1;
}

int main()
{
    int i,j,x,y,t;
    while (~sf(MeNum))
    {
        mem(time,INF);
        FRL(i,0,MeNum)
        {
            sfff(x,y,t);
            if (t<time[x][y]) time[x][y]=t;
            FRL(j,0,4)
            {
                int dx=x+dir[j][0];
                int dy=y+dir[j][1];
                if (IsOk(dx,dy)&&time[dx][dy]>t)
                    time[dx][dy]=t;
            }
        }
        pf("%d\n",bfs());
    }
    return 0;
}
/*
4
0 0 2
2 1 2
1 1 2
0 3 5
*/



  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值