迷宫问题 回溯 C++

题目 2177: 信息学奥赛一本通T1252-走迷宫

信息学奥赛一本通T1252-走迷宫 - C语言网 (dotcpp.com)

#include<iostream>
#include<queue>

using namespace std;
int judge[41][41]={0};//标记地图
char a[41][41];//地图输入为字符
int r,c;
struct node{
    int x,y,step;
    node(int xx,int yy,int stepp)
    {
        x=xx;
        y=yy;
        step=stepp;
    }
};//便于存储
//下一步走的上下左右
int dir[4][2]={{0,1},{1,0},{-1,0},{0,-1}};
queue<node> q;//设置队列q
bool check(int x,int y)  //检查判断这个位置点是否满足条件
{
    //超出范围了,已经被标记过了,或者字符是#
    if(x<0 || x>=r || y<0 || y>=c || judge[x][y] || a[x][y]=='#')
    {
        return false;
    }
    return true;
}

int bfs(node start)
{
    node next(0,0,0),cur(0,0,0);
    while(!q.empty()){//如果q空还没有找到,就找不到了
        cur=q.front();//压入
        q.pop();//去除
        if(cur.x==r-1 && cur.y==c-1){
            return cur.step +1;
        }
        for(int i=0;i<4;i++)//向四个方向延展
        {
            next.x=cur.x+dir[i][0];
            next.y=cur.y+dir[i][1];
            next.step=cur.step+1;//每次遍历周围的一个点
            //就走一步,如果不符合下面if里面的条件
            //此时的next也不会被push进q里面,相当于回溯
            if(check(next.x,next.y))
            {
                q.push(next);
                judge[next.x][next.y]=1;//标记已走过
            }
        }
        
    }
        
    return -1;
}





int main()
{
    cin>>r>>c;
    for(int i=0;i<r;i++)
    {
        for(int j=0;j<c;j++)
        {
            cin>>a[i][j];
        }
    }
    node start(0,0,0);
    q.push(start);
    judge[0][0]=1;//对初始点进行标记
    cout<<bfs(start)<<endl;
    return 0;
}

题目 1672: 迷宫问题

迷宫问题 - C语言网 (dotcpp.com)

#include<iostream>
#include<queue>
#include<cstring>

using namespace std;
int judge[105][105]={0};//标记地图
char a[105][105];//地图输入为字符
int r,c,n;
int sx,sy;
struct node{
    int x,y,step;
    node(int xx,int yy,int stepp)
    {
        x=xx;
        y=yy;
        step=stepp;
    }
};//便于存储
//下一步走的上下左右
int dir[4][2]={{0,1},{1,0},{-1,0},{0,-1}};
queue<node> q;//设置队列q
bool check(int x,int y)  //检查判断这个位置点是否满足条件
{
    //超出范围了,已经被标记过了,或者字符是#
    if(x<0 || x>=r || y<0 || y>=c || judge[x][y] || a[x][y]=='#')
    {
        return false;
    }
    return true;
}

int bfs(node start)
{

    node next(0,0,0),cur(0,0,0);
    while(!q.empty()){//如果q空还没有找到,就找不到了
        cur=q.front();//压入
        q.pop();//去除

        if(a[cur.x][cur.y]=='E'){
            return cur.step;//注意此处什么时候需要加1
        }
        for(int i=0;i<4;i++)//向四个方向延展
        {
            next.x=cur.x+dir[i][0];
            next.y=cur.y+dir[i][1];
            next.step=cur.step+1;//每次遍历周围的一个点
            //就走一步,如果不符合下面if里面的条件
            //此时的next也不会被push进q里面,相当于回溯
            if(check(next.x,next.y))
            {
                q.push(next);
                judge[next.x][next.y]=1;//标记已走过
            }
        }
        
    }
        
    return -1;
}

int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
    {
        memset(judge,0,sizeof(judge));//注意每一次要把原来的判断数组恢复
        cin>>r>>c;
        for(int i=0;i<r;i++)
        {
        for(int j=0;j<c;j++)
        {
            cin>>a[i][j];
            if(a[i][j]=='S')
            {
                sx=i;sy=j;
            }
        }
        }
    judge[sx][sy]=1;//对初始点进行标记
    node start(sx,sy,0);
    q.push(start);
    cout<<bfs(start)<<endl;
    }

    return 0;
}

以上两个迷宫问题类型差不多,区别在于起始点和终点不同,要注意一下.基本框架代码都是一致的,下次遇到相同的可以正确理解和运用就可以.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值