【数据结构】用栈实现迷宫

本文介绍了如何利用栈数据结构解决迷宫问题。通过创建一个二维数组表示迷宫,使用栈来存储路径,并通过检查当前位置的上下左右四个方向是否可行,实现从起点到终点的路径搜索。如果找到出口则返回真,否则返回假。最后输出搜索过程中的迷宫状态。
摘要由CSDN通过智能技术生成
#include <iostream>
using namespace std;
#define N 10

#include <stack>

struct pos
{
     int _row;// 行
     int _col;// 列
};

bool cheakstep(pos a)
{
     if(a._col > -1||a._col < N-1||
         a._row > -1||a._row < N-1)
    {
          return true ;
    }
     return false ;
}

bool IsPath(int arr[][N],pos nowloction,stack<pos>& s)
{

    pos tmp = nowloction;
     //入栈标记  栈不为空
    s.push (nowloction);
    arr[tmp._row][tmp._col] = 2;

     while(!s.empty ())
    {

          //shang
         pos now = s.top ();

          if(now._row == N-1)
         {
              return true ;
         }

         now._row -=1;
          if(cheakstep(now)==true && arr[now._row][now._col]==0)
         {
             s.push (now);
             arr[now._row ][now._col ]=2;
              continue;
         }
          //you
         now = s.top ();
         now._col +=1
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值