数据结构——用栈解决简单迷宫问题

#include<stdio.h>
#include<stdlib.h>
using namespace std;
typedef int ElemType;
#define MaxSize 50


/*int mg[n1][n2]
#define n1 50//定义行范围
#define n2 50//定义列范围
void array(int g,int h)    //以二维数组形式定义迷宫内容
{
          int a,b;
          for(a=0;a<g;a++)
          {
               for(b=0;b<h;b++)
               {
                     scanf("%d",&mg[a][b]);   //输入迷宫对应的数组数据
             }
         }
}*/
int mg[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}
};


typedef struct
{
    int i;//当前方块的行号
    int j;//当前方块的列号
    int di;//di是下一相邻可走方位的方位号
}Box;      //方块类型


typedef struct
{
    Box data[MaxSize];
    int top;
}StType;//顺序栈类型


void InitStack(StType *&st)
{
    st=(StType *)malloc(sizeof(StType));
    st->top=-1;
}//初始化栈


bool Push(StType *&st,Box e)
{
    if(st->top==MaxSize-1)//判断是否会溢出
        return false;
    st->top++;
    st->data[st->top]=e;
    return true;
}//入栈


bool StackEmpty(StType *st)
{
    return (st->top==-1);
}//判断栈是否为空


bool GetTop(StType *st,Box &e)
{
    if(st->top==-1)
        return false;
    e=st->data[st->top];
    return true;
}//取栈顶元素


bool Pop(StType *st,Box &e)
{
    if(st->top==-1)
        return false;
    e=st->data[st->top];
    st->top--;
    return true;
}//出栈


void DestroyStack(StType *st)
{
    free(st);
}//销毁栈




bool mgpath(int xi,int yi,int xe,int ye)
{
    Box path[MaxSize];
    Box e;
    int i,j,di,i1,j1,k;
    bool find;
    StType *st;
    InitStack(st);
    e.i=xi;e.j=yi;e.di=-1;//设置e为入口
    Push(st,e);//e进栈
    mg[xi][yi]=-1;//将入口的迷宫值设为-1,避免重复
    while(!StackEmpty(st))//栈不为空循环
    {
        GetTop(st,e);
        i=e.i;j=e.j;di=e.di;//取栈顶方块e
        if(i==xe&&j==ye)
        {
            printf("一条迷宫路径如下:\n");
            k=0;
            while(!StackEmpty(st))
            {
                Pop(st,e);
                path[k++]=e;
            }
            while(k>=1)
            {
                k--;
                printf("\t(%d,%d)",path[k].i,path[k].j);
                if((k+2)%5==0)
                    printf("\n");


            }
            printf("\n");
            DestroyStack(st);
            return true;
        }
        find=false;
        while(di<4&&!find)
        {
            di++;
            switch(di)
            {
                case 0:i1=i-1;j1=j;break;
                case 1:i1=i;j1=j+1;break;
                case 2:i1=i+1;j1=j;break;
                case 3:i1=i;j1=j-1;break;
            }
            if(mg[i1][j1]==0) find=true;
        }
        if(find)
        {
            st->data[st->top].di=di;
            e.i=i1;e.j=j1;e.di=-1;
            Push(st,e);
            mg[i1][j1]==-1;
        }
        else{
            Pop(st,e);
            mg[e.i][e.j]=0;
        }
    }
    DestroyStack(st);
    return false;
}
int main()
{
    if(!mgpath(1,1,8,8))
        printf("该迷宫没有解!");
    return 1;

}

运行结果:


  • 7
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GlassySky0816

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值