POJ - 3669(自建迷宫)——广搜

Meteor Shower
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 20648 Accepted: 5373

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


题意:在一个可以无限大(本题所允许的范围内)的平面上,有许多定时炸弹(是根据我们手动输入数据,然后自己构建出一个有

定时炸弹的迷宫),我们从(0,0)出发,每到一点(是炸弹的话t2)就把到此点所用的步数(step)与之比较,若step<t2,则

可以通过,否则不能通过,问是否能从(0,0)出发到达安全地带。

 

解题思路:(1)从(0,0)出发,如果(0,0)点是安全地带的话,就没必要往下走了,因为已经达到“目的地”。

(2)从(0,0)出发,如果此点是炸弹,且爆炸时间是0秒,map【0】【0】(是炸弹的话)代表炸弹爆炸的时刻。

(3)以上两种是特殊情况,接下来就是一般情况,首先把地图map【x】【y】初始化为-1,-1代表安全地带,在是安全地带的情

况下将手动输入数据更新地图,如果是同一个点有两个爆炸时刻的话,应该选选那个小的,时间小的话,提前爆炸后,该点我们就

不用走了。

(4)我们每走一步,满足入队条件,标记后入队,千万别忘了标记,标记后的点不能再重复走,不然会内纯超限。


代码如下:

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define N 310
struct node
{
    int x;
    int y;
    int s;
};
int map[N][N],d[4][2]= {0,1,1,0,0,-1,-1,0};
int book[N][N];
int bfs()//返回的是输出的结果,成功返回步数,失败返回-1
{
    queue<node> q;
    node tmp,now;
    if(map[0][0]==0) return -1;//起点是爆炸区域
    if(map[0][0]==-1) return 0;//起点点安全
    now.x=0;
    now.y=0;
    now.s=0;
    book[0][0]=1;
    q.push(now);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        if(map[now.x][now.y]==-1)//-1代表安全地带,即到达终点
            return now.s;
        for(int i=0; i<4; i++)
        {
            tmp.x=now.x+d[i][0];
            tmp.y=now.y+d[i][1];
            tmp.s=now.s+1;
            if(book[tmp.x][tmp.y]||tmp.x<0||tmp.y<0)//本体没有上限,是无限大的,只有下限
                continue;
            if(tmp.s<map[tmp.x][tmp.y]||map[tmp.x][tmp.y]==-1)//炸弹此时不会爆炸,或者为空地
            {
                book[tmp.x][tmp.y]=1;//标记,只走一次
                q.push(tmp);
            }
        }
    }
    return -1;
}
int main()
{
    int m,x,y,tx,ty,l;
    while(~scanf("%d",&m))
    {
        memset(map,-1,sizeof(map));//初始化为-1
        memset(book,0,sizeof(book));//初始化为0
        for(int i=0; i<m; i++)//构建迷宫
        {
            scanf("%d %d %d",&x,&y,&l);
            if(map[x][y]==-1)
                map[x][y]=l;
            else if(map[x][y]>l)
                map[x][y]=l;
            for(int j=0; j<4; j++)
            {
                tx=x+d[j][0];
                ty=y+d[j][1];
                if(tx<0||ty<0)//千万不要忘了,不然Runtime error  

                  continue;
                if(map[tx][ty]==-1)
                    map[tx][ty]=l;
                else if(map[tx][ty]>l)
                    map[tx][ty]=l;
            }
        }
        printf("%d\n",bfs());
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值