HDU 1072(bfs)

题意:主人公在一块6秒钟后要爆炸的范围内,主人公没走一步需要1秒,0是墙壁,1是路,2是起点,3是目标,4是爆炸时间从新设置,如果刚好在0秒到达3或者4都算输,每个可到达位置都可以重复到达,求主人公多短时间能够走出去,如果走不出去输出-1.

 

使用优先队列:

 

#include<iostream>
#include<stdio.h>
#include<queue>
#include<cstring>
using namespace std;
int cas,n,m;
int map[10][10],vis[10][10][7],dir[4][2]={1,0,-1,0,0,1,0,-1},ans;
struct node
{
    int x,y,step,time;
    node(int _x=0,int _y=0,int _step=0,int _time=0):x(_x),y(_y),step(_step),time(_time){};
    friend bool operator<(const node&a,const node&b)
    {
        return a.step>b.step;
    }
};
node tar;
priority_queue <node > q;
void BFS()
{
    while(!q.empty())
    {
        node t=q.top();
        q.pop();
        if(t.time==1)continue;
         
        for(int k=0;k<4;k++)
        {
            int x=t.x+dir[k][0];
            int y=t.y+dir[k][1];
            if(x>=0&&x<n&&y>=0&&y<m&&!vis[x][y][t.time-1]&&map[x][y]!=0)
            {
                 if(map[x][y]==1||map[x][y]==2)
                 {
                     vis[x][y][t.time-1]=1;
                     q.push(node(x,y,t.step+1,t.time-1));
                 }
                 if(map[x][y]==3)
                 {
                     vis[x][y][t.time-1]=1;
                     ans=t.step+1;return;
                 }
                  
                 if(map[x][y]==4)
                 {
                     vis[x][y][t.time-1]=1;
                     q.push(node(x,y,t.step+1,t.time-1));
                     if(!vis[x][y][6])
                     {
                         vis[x][y][6]=1;
                         q.push(node(x,y,t.step+1,6));
                     }
                 }
            }
        }
    }
     
}
int main()
{
    //freopen("1.txt", "r", stdin);
    //freopen("out1.txt", "w", stdout);
     scanf("%d",&cas);
     while(cas--)
     {
         while (!q.empty()) q.pop();
         ans=-1;
         memset(vis,0,sizeof(vis));
         scanf("%d%d",&n,&m);
         for(int i=0;i<n;i++)
             for(int j=0;j<m;j++)
             {
                 scanf("%d",&map[i][j]);
                 if(map[i][j]==2)
                 {
                     q.push(node(i,j,0,6));
                     vis[i][j][6]=1;
                 }
                 
             }
             BFS();
           printf("%d\n",ans);
          
 
     }
}


不使用优先队列:

#include<iostream>
#include<queue>
using namespace std;
int dir[][2]={0,1,1,0,0,-1,-1,0}; //4个方向
struct node
{
    int x;
    int y;
    int time;//剩余的时间
    int step;//走过的步数
}begin;
int n,m,i,j;
int map[10][10]; //地图
int mark[10][10];//标记时间
void bfs();
int main()
{
    int cases;
    scanf("%d",&cases);
    while(cases--)
    {
        scanf("%d%d",&n,&m);
        for(i=0;i<n;i++)
            for(j=0;j<m;j++)
            {        
                scanf("%d",&map[i][j]);
                if(map[i][j]==2) //找出起点
                {
                    begin.x=i;
                    begin.y=j;
                    begin.time=6;
                    begin.step=0;
                }
                mark[i][j] = 0 ;//标记初始化为0
            }
        bfs();
    }
    return 0;
}
void bfs()
{
    queue<node>q;
    q.push(begin);  //起点入队
    mark[begin.x][begin.y] = begin.time ;
    node p,temp;
    while(!q.empty()) //队列为空还没找到就是-1了
    {
        p=q.front();
        q.pop();
        for(i=0;i<4;i++)
        {
            temp=p;
            temp.x+=dir[i][0];
            temp.y+=dir[i][1];
            if(temp.x>n||temp.x<0||temp.y>m||temp.y<0||map[temp.x][temp.y]==0)   //边界判断
                continue;
            temp.step++; //步数加1
            temp.time--; //剩余时间减1
            if(map[temp.x][temp.y]==3) //出口
            {
                printf("%d\n",temp.step);
                return ;  //退出函数
            }
            else if ( map[temp.x][temp.y] == 4 )   //还原点
            {
                temp.time = 6 ;
            }
            if( temp.time > 1 && mark[temp.x][temp.y] < temp.time ) //标记该点当剩余时间更大时就标记
            {
                mark[temp.x][temp.y] = temp.time;
                q.push(temp) ;
            }
        }
    }
    printf("%d\n",-1);//队列为空还没找到就是-1了
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值