C++ 链栈函数模板解决 迷宫问题(DPS) 学习笔记


前言

教学视频来源 B站 懒猫老师
视频连接:
懒猫老师-数据结构-(7)栈的链式存储(链栈,链式堆栈)
https://www.bilibili.com/video/BV1cE411n7JX#reply4634549284
懒猫老师-数据结构-(8)栈的应用:迷宫问题(DFS)
https://www.bilibili.com/video/av92862714

本人 看完懒猫老师教学视频后,写出来的代码,均实现相关功能
(自己测试是这样的 doge,如果有问题的话 记得B站@我 原罪Suntre CSDN可能不常在)


提示:以下是本篇文章正文内容,下面案例可供参考

一、栈的链式存储(链栈,链式堆栈)是什么?

示例:百度:栈的链式存储结构,简称链栈。
  由于栈只是栈顶在做插入和删除操作,所以栈顶应该放在单链表的头部。另外,都有了栈顶在头部了,单链表中的头结点也就失去了意义,通常对于链栈来说,是不需要头结点的。
  个人理解: 用链表节点的方式,连接起来的栈,默认采用头插法。

二、链栈 的函数模板的编写

1.源码分享

视频来源 懒猫老师-数据结构-(7)栈的链式存储(链栈,链式堆栈)
https://www.bilibili.com/video/BV1cE411n7JX#reply4634549284

代码如下(示例):

/template<class DateType>  //链栈结构体
template<class DateType>
class LinkStack
{
    private:
        typedef struct Node{
            DateType date;      //数据域
            struct Node *next; //指针领域
        }node,*Link;
        Link top;// 链表节点指针
        static int LinkCount;
        //int LinkCount = 0;

    public:
        LinkStack();           //构造函数
        virtual ~LinkStack();  //析构函数
        void push(DateType x); //压入一个栈节点
        DateType gettop();     //得到最后一个节点内容
        DateType pop();        //弹出最后最后一个  节点的内容
        bool isEmpty();          //判断是否为空栈
        int getLinkCount();    //输出节点数量

        //bool isFull;         //一般不会满除非内存满了

    protected:

};
template<class DateType>
int LinkStack<DateType>::LinkCount = 0;  //节点记录数字

template<class DateType>
LinkStack<DateType>::LinkStack()
{
    Link s;
    s = new node;
    top =s;
    s->next =nullptr;
}
template<class DateType>
LinkStack<DateType>::~LinkStack()
{
    for(int i=0;i<LinkCount;i++)
    {
        delete top;
        top = top->next;
    }

}


template<class DateType>
void LinkStack<DateType>::push(DateType x){
    Link s;
    s = new node;
    s->date = x;
    s->next =top;
    top = s;
    LinkCount++;

}

template<class DateType>
DateType LinkStack<DateType>::pop(){
    DateType p = gettop();
    Link q = top->next;
    delete top;
    top = q;    //指向top下一地址
    LinkCount--;
    return p;  //返回top内容

}

template<class DateType>
DateType LinkStack<DateType>::gettop(){
    if(!isEmpty()){ //不为空
        return top->date;  //返回top内容
    }

}

template<class DateType>
bool LinkStack<DateType>::isEmpty(){
    return top == nullptr;

}
template<class DateType>
int LinkStack<DateType>::getLinkCount(){
    return LinkCount;
}

2.调用方式

代码如下(示例):

LinkStack<DateType> UseName;

个人吐槽: 废话。


三、路径函数的编写

1.源码分享

//寻找路径函数     迷宫数组        方向数组     链栈
bool findPath(int maze[M+2][N+2],box Direct[],LinkStack<box> &s)
{
    box temp ={1,1,-1}; //  x y di  出发点
    int x,y,di;    //当前位置
    int line,col;  //行 列
    maze[1][1]=-1; //第一次进入的地方要  由0 变成 -1  代表走过了
    s.push(temp);  //入栈  确保节点有一个  能够进入 while 循环
    while(!s.isEmpty())
    {
        temp = s.pop();  //退栈
        x = temp.x;y=temp.y;di=temp.di+1;  //装载第一个位置 信息  ,方向默认为 -1,需要+1 = 0
        while(di < 4)   //4个方向循环  尝试行走
        {
            line = x+ Direct[di].x;   //当前列x + 方向 x
            col = y+ Direct[di].y;  //当前行y +方向y
            if(maze[line][col] == 0)    //判断是否为空(是否走过)
            {
                temp ={x,y,di};//运动数据 装载
                s.push(temp);  //压入堆栈  记录
                x = line; y = col ;  //更新当前位置
                display(x,y,di,s.getLinkCount()); //打印   x y  方向  次数(节点个数)
                maze[line][col] =-1; //走过位置 变为-1
                if( x == M && y == N) return true;
                else di =0;  //复位  寻找下一个点 //到达最终位置 M,N 
            }
            else
            {
                di++; //换方向继续寻找
            }

        }

    }
    return false;
}

2.相关调用函数 以及define 和 struct

#define M 8
#define N 8
//迷宫数组
int maze[M+2][N+2]=
{
    1,1,1,1,1,1,1,1,1,1,
    1,0,0,0,0,0,1,0,0,1,
    1,0,1,0,1,0,0,0,0,1,
    1,0,1,0,1,0,1,1,0,1,
    1,0,1,0,1,0,0,0,0,1,
    1,1,1,0,1,0,1,1,1,1,
    1,1,0,0,0,0,0,0,0,1,
    1,0,0,1,1,1,1,0,0,1,
    1,0,0,0,0,0,0,0,0,1,
    1,1,1,1,1,1,1,1,1,1
};


typedef struct Box{
    int  x,y;      //方向  X偏移量  Y偏移量
    int di;
}box;
enum dir {RIGHT,DOWN,LEFT,UP};  //枚举   右=0 下=1 左=2 上=3
box direction[4] = {{0,1,RIGHT},{1,0,DOWN},{0,-1,LEFT},{-1,0,UP}}; // 右 下 左 上 一次运动一格

string turn(int num)  //数字转化 字符串
{
    if( num == UP)return "up";
    else if( num == DOWN)return "DOWN";
    else if( num == LEFT)return "LEFT";
    else if( num == RIGHT)return "RIGHT";
}
//打印显示  xy坐标  运动方向  移动次数
void display(int x,int y,int di,int num)
{
    cout<< "第";
    cout.setf(ios::right); //设置对齐方式为left
    cout.width(3); //设置宽度为7,不足用空格填充
    cout<<num << "次 向";
    cout.setf(ios::right); //设置对齐方式为left
    cout.width(7); //设置宽度为7,不足用空格填充
    cout<< turn(di)<<"移动"<<"  X=" << x << "  Y=" << y << endl;
}

3.主函数

int main()
{
        LinkStack<box> a;
        while(1)
        {
            if( findPath(maze,direction,a) )
            {
                cout <<"到达出口" << endl;
                break; //退出函数
            }
        }

    cout << "Hello world!" << endl;

    return 0;
}

四、测试效果

在这里插入图片描述
在这里插入图片描述

总结

本人之前尝试 专门给迷宫写个 栈类,结果出来各种各样的bug(彩笔的苦笑)。
后来直接调用之前课程的 写的链栈的函数模板,把类型定义成 box 结构体就解决栈调用的问题。
路径函数相关解释请看 (我肯定解释的没老师好):
懒猫老师-数据结构-(8)栈的应用:迷宫问题(DFS)
https://www.bilibili.com/video/av92862714

感谢 懒猫老师的指导,感谢懒猫老师的优秀教学资源,感谢B站,感谢CSDN。

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值