题目: poj – 3669(D题)

Bessie hears thatan extraordinary meteor shower is coming; reports say that these meteors willcrash into earth and destroy anything they hit. Anxious for her safety, shevows to find her way to a safe location (one that is never destroyed by ameteor) . She is currently grazing at the origin in the coordinate plane andwants to move to a new, safer location while avoiding being destroyed bymeteors along her way.

The reports saythat 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 fourrectilinearly adjacent lattice points.

Bessie leaves theorigin at time 0 and can travel in the first quadrant and parallel to the axesat the rate of one distance unit per second to any of the (often 4) adjacentrectilinear points that are not yet destroyed by a meteor. She cannot belocated on a point at any time greater than or equal to the time it isdestroyed).

Determine theminimum time it takes Bessie to get to a safe place.

Input

* Line 1: A singleinteger: M
* Lines 2..M+1: Line i+1 contains three space-separatedintegers: XiYi, and Ti

Output

* Line 1: Theminimum 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,下面有m行,每行有三个数,前两个是流星撞地球的坐标,第三个是撞时间,撞到这一点它周围的四点,都跟着爆炸,Bessie开始时位于(0, 0)位置,并希望逃到一处不会被袭击到的地方(在第一象限内),找逃到安全的地方的最短时间;


思路:我用的 bfs写的,题意上没说 x,y的上限,只要让x,y>=0即可,我写的主要是代码主要是两个数组b[][]数组是标记走过的点,book[][]数组是存这个点毁灭的最小时间,代码如下:

#include<stdio.h>        //用广搜写的,这道题
#include<string.h>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f

int book[350][350],a[5][2]= {0,0,0,-1,1,0,0,1,-1,0},f,b[350][350];
// b[][]数组是标记走过的点,book[][]数组是存这个点毁灭的最小时间,a[][]数组是方向
struct st
{
    int x,y,t;   //x,y,坐标,t为走到这一点的时间
} stu[160000];

void bfs()
{
    stu[0].x=0;
    stu[0].y=0;
    stu[0].t=0;
    int star=0,end=1;
    while(star<end)
    {
        for(int i=1; i<5; i++)
        {
            int tx=stu[star].x+a[i][0];
            int ty=stu[star].y+a[i][1];
            if(tx>=0&&ty>=0&&!b[ty][tx]&&(book[ty][tx]==INF||stu[star].t+1<book[ty][tx]))
            {
                b[ty][tx]=1;
                stu[end].x=tx;
                stu[end].y=ty;
                stu[end].t=stu[star].t+1;
                if(book[ty][tx]==INF)
                {
                    f=1;
                    printf("%d\n",stu[end].t);
                    return ;
                }
                end++;
            }
        }
        star++;
    }
}
void fu(int x,int y,int t)
{
    for(int i=0; i<5; i++)
    {
        int tx=x+a[i][0];
        int ty=y+a[i][1];
        if(tx>=0&&ty>=0)
            book[ty][tx]=min(book[ty][tx],t);
    }
}
int main()
{
    int i,j,k,t,n,x,y;
    while(~scanf("%d",&n))
    {
        memset(book,INF,sizeof(book));
        memset(b,0,sizeof(b));
        for(i=0; i<n; i++)
        {
            scanf("%d%d%d",&x,&y,&t);
            fu(x,y,t);
        }
        f=0;
        if(book[0][0]==0)
        {
            printf("-1\n");
            continue;
        }
        bfs();
        if(!f) printf("-1\n");
    }
    return 0;
}


希望博友提些建议,小编会倍加开心,会更加努力,谢谢!



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值