栈和递归---解决迷宫问题

迷宫问题:

走过的路设置为2,未走过的路设置0,墙设置为1;

这里写图片描述

  • - 解决方法一:栈stack

    将每次你所走过的路都push进入栈里面,栈顶元素的坐标就是你所在的位置,取出你所在的位置然后判断当前位置上下左右四个方向是否有通路,如果没有就pop()顶层元素,然后再取出顶层元素(即退一步),然后接着判断其他方向是否有通路,直到你走出边界或者栈为空的时候退出;
    缺陷:只找到一条路径就退出,无法判断最短路径是哪一条。

这里写图片描述

   代码如下:
bool CheckPath(const Pos& cur)
    {
        if(cur._row >= 0 && cur._row < M && cur._col >= 0 && cur._col < N
            && _maze[cur._row][cur._col] == 0)
            return true;//这条路可以走
        return false;
    }


bool GetMazePath(Pos& entry)
    {
        stack<Pos> paths;
        paths.push(entry);
        while(!paths.empty())
        {
            Pos cur = paths.top();
            _maze[cur._row][cur._col] = 2;
            if(cur._row == M - 1)//已经到达出口
            {
                return true;
            }
            Pos next = cur;
            //上
            next._row -= 1;
            if(CheckPath(next))
            {
                paths.push(next);
                continue;
            }
            next = cur;
            //右
            next._col += 1;
            if(CheckPath(next))
            {
                paths.push(next);
                continue;
            }
            next = cur;
            //下
            next._row += 1;
            if(CheckPath(next))
            {
                paths.push(next);
                continue;
            }
            next = cur;
            //左
            next._col -= 1;
            if(CheckPath(next))
            {
                paths.push(next);
                continue;
            }
            paths.pop();
        }
        return false;
    }
  • 解决方法二:递归

    将当前的位置的上下左右状态分别分析,任何一方可以满足条件继续行走即回调当前函数,继续判断,这样一直递归到最后一层退出后,再返回上一次位置继续判断其他方向,保持这样的思路一直进行下去,直到回调到初始位置判断完四个方向都结束后退出。

这里写图片描述

代码如下:

    //递归法
    void GetMazePathR(Pos entry)
    {
        Pos cur = entry;
        _maze[entry._row][entry._col] = 2;
        if(entry._row == M - 1)
            return;
        Pos next = cur;
        //上
        next._row -= 1;
        if(CheckPath(next))
        {
            GetMazePathR(next);
        }
        next = cur;
        //右
        next._col += 1;
        if(CheckPath(next))
        {
            GetMazePathR(next);
        }
        next = cur;
        //下
        next._row += 1;
        if(CheckPath(next))
        {
            GetMazePathR(next);
        }
        next = cur;
        //左
        next._col -= 1;
        if(CheckPath(next))
        {
            GetMazePathR(next);
        }

    }
  • 求出最短路径:

    设置每次走过一条路径都让它是当前路径的值+1,判断条件是下一条路径的值 == 0 时,或者下一条路径的值比当前路径的值大,此路可通过。

这里写图片描述

   代码如下:
bool CheckPath(const Pos& cur, const Pos& pre)
    {
        if(cur._row >= 0 && cur._row < M && cur._col >= 0 && cur._col < N
            && _maze[cur._row][cur._col] != 1)
        {
            if(_maze[cur._row][cur._col] == 0)
                return true;
            else
                return _maze[cur._row][cur._col] > _maze[pre._row][pre._col];
        }
        return false;
    }

    void GetShortPathR(Pos entry)//递归求最短路径
    {
        Pos cur = entry;
        if(cur._row == M - 1)
            return;
        Pos next = cur;
        //上
        next._row -= 1;
        if(CheckPath(next,cur))
        {
            _maze[next._row][next._col] = _maze[cur._row][cur._col] + 1;
            GetShortPathR(next);
        }
        next = cur;
        //右
        next._col += 1;
        if(CheckPath(next,cur))
        {
            _maze[next._row][next._col] = _maze[cur._row][cur._col] + 1;
            GetShortPathR(next);
        }
        next = cur;
        //下
        next._row += 1;
        if(CheckPath(next,cur))
        {
            _maze[next._row][next._col] = _maze[cur._row][cur._col] + 1;
            GetShortPathR(next);
        }
        next = cur;
        //左
        next._col -= 1;
        if(CheckPath(next,cur))
        {
            _maze[next._row][next._col] = _maze[cur._row][cur._col] + 1;
            GetShortPathR(next);
        }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值