2020-9-18

下图为测试代码的图例(对应main函数的maze数组):
在这里插入图片描述

//严蔚敏《数据结构》
//栈应用:迷宫求解,之前在家的时候不理解,现在终于可以敲出代码了,555
//自学中,加油啊!!!
#include<iostream>
using namespace std;
typedef struct
{
    int x;
    int y;
}PosType;

typedef struct
{
    int ord;
    PosType seat;
    int di;
}SElemType;

typedef struct SNode
{
    SElemType data;
    struct SNode* next,* prior;
}SNode,* Link;

typedef struct
{
    Link head,tail;
    int len;
}LinkStack;

bool Init_Stack(LinkStack& s)
{
    s.head=new SNode;
    if(!s.head)
        return false;
    s.head->next=s.head->prior=nullptr;
    s.tail=s.head;
    s.len=0;
    return true;
}

void Output_Stack(LinkStack s)
{
    Link p=s.head->next;
    cout<<"路径:"<<endl;
    while(p){
        cout<<"第"<<p->data.ord<<"步:("<<p->data.seat.x<<","<<p->data.seat.y<<")\n";
        p=p->next;
    }
    cout<<"===="<<endl;
}

bool IsEmpty_Stack(LinkStack s)
{
    if(s.len)
        return false;
    return true;
}

bool Push_Stack(LinkStack& s,SElemType e)
{
    Link p=new SNode;
    if(!p)
        return false;
    p->data=e;
    p->next=s.tail->next;
    p->prior=s.tail;
    s.tail->next=p;
    s.tail=p;
    s.len++;
    return true;
}

bool Pop_Stack(LinkStack& s,SElemType& e)
{
    Link p;
    e=s.tail->data;
    p=s.tail;
    s.tail=s.tail->prior;
    free(p);
    s.tail->next=nullptr;
    s.len--;
    return true;
}

bool Pass(int maze[10][10],PosType curpos)
{
    if(maze[curpos.x][curpos.y]==0)
        return true;
    return false;
}

void FootPrint(int maze[10][10],PosType curpos)
{
    maze[curpos.x][curpos.y]=9;
}
PosType NextPos(PosType curpos,int m)
{
    PosType nextPos=curpos;
    switch(m)
    {
        case 1:nextPos.y++;break;//bug1:1!='1'  bug2:137行代码de e.seat不允许动
        case 2:nextPos.x++;break;
        case 3:nextPos.y--;break;
        case 4:nextPos.x--;
    }
    return nextPos;
}

void MarkPrint(int maze[10][10],PosType curpos)
{
    maze[curpos.x][curpos.y]=1;
}
void MazePathPrint(int maze[10][10])
{
    cout<<"迷宫为:(其中9为可通路径)\n";
    for(int i=0;i!=10;i++){
        for(int j=0;j!=10;j++)
            cout<<maze[i][j]<<' ';
        cout<<endl;
    }
    cout<<endl;
}
bool MazePath(int maze[10][10],PosType start,PosType end)
{
    LinkStack s;
    Init_Stack(s);
    PosType curpos=start;
    int curstep=1;
    SElemType e;
    do{
        if(Pass(maze,curpos)){
            e.ord=curstep;e.di=1;e.seat=curpos;
            Push_Stack(s,e);
            FootPrint(maze,e.seat);
            if(e.seat.x==end.x&&e.seat.y==end.y){
                Output_Stack(s);
                MazePathPrint(maze);
                return true;
            }
            curpos=NextPos(e.seat,e.di);
            curstep++;
        }
        else{
            if(!IsEmpty_Stack(s)){
                Pop_Stack(s,e);
                while(e.di==4&&!IsEmpty_Stack(s)){
                    MarkPrint(maze,e.seat);
                    curstep--;
                    Pop_Stack(s,e);
                }
                if(e.di<4){
                    e.di++;Push_Stack(s,e);
                    curpos=NextPos(e.seat,e.di);
                }
            }
        }
    }while(!IsEmpty_Stack(s));
    return false;
}

int main()
{
    int maze[10][10]={{1,1,1,1,1,1,1,1,1,1},
    {1,0,0,1,0,0,0,1,0,1},{1,0,0,1,0,0,0,1,0,1},
    {1,0,0,0,0,1,1,0,0,1},{1,0,1,1,1,0,0,0,0,1},
    {1,0,0,0,1,0,0,0,0,1},{1,0,1,0,0,0,1,0,0,1},
    {1,0,1,1,1,0,1,1,0,1},{1,1,0,0,0,0,0,0,0,1},
    {1,1,1,1,1,1,1,1,1,1}};
    PosType start,end;
    start.x=start.y=1;
    end.x=end.y=8;
    MazePath(maze,start,end);
    return 0;
}

路径:1:(1,1)2:(1,2)3:(2,2)4:(3,2)5:(3,1)6:(4,1)7:(5,1)8:(5,2)9:(5,3)10:(6,3)11:(6,4)12:(6,5)13:(7,5)14:(8,5)15:(8,6)16:(8,7)17:(8,8)
====
迷宫为:(其中9为可通路径)
1 1 1 1 1 1 1 1 1 1
1 9 9 1 1 1 1 1 0 1
1 0 9 1 1 1 1 1 0 1
1 9 9 1 1 1 1 0 0 1
1 9 1 1 1 0 0 0 0 1
1 9 9 9 1 0 0 0 0 1
1 0 1 9 9 9 1 0 0 1
1 0 1 1 1 9 1 1 0 1
1 1 0 0 0 9 9 9 9 1
1 1 1 1 1 1 1 1 1 1


Process returned 0 (0x0)   execution time : 0.096 s
Press any key to continue.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值