利用栈实现实现迷宫问题

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define m 6
#define n 8
int maze[m+2][n+2]={{1,1,1,1,1,1,1,1,1,1},
                    {1,0,1,1,1,0,1,1,1,1},
                    {1,0,0,0,0,1,1,1,1,1},
                    {1,0,1,0,0,0,0,0,1,1},
                    {1,0,1,1,1,0,0,1,1,1},
                    {1,1,0,0,1,1,0,0,0,1},
                    {1,0,1,1,0,0,1,1,0,1},
                    {1,1,1,1,1,1,1,1,1,1}};

typedef struct{
    int x;
    int y;
}item;

item move[4]={{0,1},{1,0},{0,-1},{-1,0}};

typedef struct{
    int x,y,d;
}DataType;

typedef struct{
    DataType  data[1000];
    int top;
}SeqStack,*PSeqStack;

PSeqStack Init_SeqStack()
{
    PSeqStack p;
    p=(PSeqStack)malloc(sizeof(SeqStack));
    if(p)
        p->top=-1;
    return p;
}

int Empty_SeqStack(PSeqStack p)
{
    if(p->top==-1)
        return 1;
    else
        return 0;
}

int Push_SeqStack(PSeqStack p,DataType x)
{
    if(p->top==999)
        return 0;
    else
    {
        p->top++;
        p->data[p->top]=x;
        return 1;
    }
}

int Pop_SeqStack(PSeqStack p,DataType *x)
{
    if(Empty_SeqStack(p))
        return 0;
    else
    {
        *x=p->data[p->top];
        p->top--;
        return 1;
    }
}

void Destroy_SeqStack(PSeqStack *p)
{
    if(*p)
        free(*p);
    *p=NULL;
    return;
}

int mazepath(int maze[][n+2],item move[],int x0,int y0)
{
    PSeqStack S;
    DataType temp;
    int x,y,d,i,j;
    temp.x=x0;
    temp.y=y0;
    temp.d=-1;
    S=Init_SeqStack();
    if(!S)
    {
        printf("栈初始化失败!!!");
        return 0;
    }
    Push_SeqStack(S,temp);
    while(!Empty_SeqStack(S))
    {
        Pop_SeqStack(S,&temp);
        x=temp.x;
        y=temp.y;
        d=temp.d+1;
        while(d<4)
        {
            i=x+move[d].x;
            j=y+move[d].y;
            if(0==maze[i][j])
            {
                temp.x=x;
                temp.y=y;
                temp.d=d;
                Push_SeqStack(S,temp);
                x=i;
                y=j;
                maze[x][y]=-1;
                if(x==m&&y==n)
                {
                    while(!Empty_SeqStack(S))
                    {
                        Pop_SeqStack(S,&temp);
                        printf("(%d,%d)<-",temp.x,temp.y);
                    }
                    Destroy_SeqStack(&S);
                    return 1;
                }
                else
                d=0;
            }
            else
            d++;
        }
    }
    Destroy_SeqStack(&S);
    return 0;
}

int main()
{
    mazepath(maze,move,1,1);
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值