迷宫问题-栈的运用

#include"stdio.h"
#include"stdlib.h"
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define N 6
typedef struct pos
{
    int x;
    int y;
}pos;
pos origin={5,2};
pos terminus={4,5};
int maze[6][6]={
        { 0, 0, 0, 0, 0, 0 },
        { 0, 0, 1, 0, 0, 0 },
        { 0, 0, 1, 0, 0, 0 },
        { 0, 0, 1, 1, 1, 0 },
        { 0, 0, 1, 0, 1, 1 },
        { 0, 0, 1, 0, 0, 0 }
    };
typedef int SElemType,Status;
typedef struct
{
    pos *base;
    pos *top;
    int stacksize;
}SqStack;
Status InitStack(SqStack *S)
{
    S->base=(pos*)malloc(STACK_INIT_SIZE*sizeof(pos));
    if(!S->base)return 0;
    S->top=S->base;
    S->stacksize=STACK_INIT_SIZE;
    return 1;
}
Status Push(SqStack *S,pos coord)
{
    if(S->top-S->base>=S->stacksize)
    {
    S->base=(pos *)realloc(S->base,(S->stacksize+STACKINCREMENT) *sizeof(pos));
    if(!S->base)return 0;
    S->top=S->base+S->stacksize;
    S->stacksize+=STACKINCREMENT;
    }
    *S->top++=coord;
    return 1;
}
Status Pop(SqStack *S)
{
	if(S->base==S->top)return 0;
	--S->top;
	return 1;
}
Status print(SqStack S)
{
     printf("找到出口!\n\n");
    printf("栈顶\n");
    for (;S.base!=S.top;S.top--)
    {
         printf("[%d %d]\n", (S.top-1)->x,(S.top-1)->y);
    }
    printf("栈底\n\n");
    //printf("路径长度为%u\n", S.top);
}//上面是头文件,运行时应添加为头文件
#include"stdio.h"
#include"Stack.h"
#include"stdlib.h"
Status Judge(pos coord,int maze[6][6])
{
    if(coord.x>=0&&coord.x<=N-1&&coord.y>=0&&coord.y<=N-1&&maze[coord.x][coord.y]!=0)
        return 1;
    else return 0;
}
Status Sign(pos coord,int maze[6][6])
{
    maze[coord.x][coord.y]=0;
    return 1;
}
Status Check(pos coord)
{
    if(coord.x==terminus.x&&coord.y==terminus.y)
        return 1;
    else return 0;
}
Status Centre(pos coord,SqStack S,int maze[6][6])
{
    if(Judge(coord,maze))
    {
        Sign(coord,maze);
        Push(&S,coord);
    }
    else return 0;
    if(Check(coord))
    {
        print(S);
        return 0;
    }
    pos east=coord;
    east.y=coord.y+1;
    Centre(east,S,maze);
    pos south=coord;
    south.x=coord.x+1;
    Centre(south,S,maze);
    pos west=coord;
    west.y=coord.y-1;
    Centre(west,S,maze);
    pos north=coord;
    north.x=coord.x-1;
    Centre(north,S,maze);
    Pop(&S);
    return 0;
}
int main()
{
    SqStack S;
	printf("%d\n",InitStack(&S));
	if(!Centre(origin,S,maze))
        printf("not exit!\n");
}*///迷宫

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值