C语言用栈实现自动走迷宫程序,只用栈实现迷宫(C语言描述)

得分:50

ca56232b3bbedf9a539d07f37fffb99a.gif

3144d8b7615c79d9f638db40d5689d26.gif

a218af6549b45ee526caf607ebff1358.gif

0f8df0e29816ae721419de940fb833d1.gif

#include

#include

#define STACK_INIT_SIZE 10000

#define STACKINCREMENT 100

#define M 10

#define N 10

int flag=0;

typedef struct//坐标位置

{

int x;

int y;

}PosType;

typedef struct//栈中元素结构体

{

int ord;//序号

PosType seat;//坐标位置

int di;//通道方向(1东2南3西4北)

}SElemType;

typedef struct//栈

{

SElemType *base;

SElemType *top;

int stacksize;

}SqStack;

栈操作

void initialStack(SqStack *s)//栈的建立

{

s->base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));

if(!s->base) exit(0);

s->top=s->base;

s->stacksize=STACK_INIT_SIZE;

}

void Push(SqStack *s,SElemType e)//插入栈顶为e的元素

{

if(s->top-s->base>=s->stacksize)

{

s->base=(SElemType *)realloc(s->base,(STACK_INIT_SIZE+STACKINCREMENT)*sizeof(SElemType));

if(!s->base) exit(0);

s->top=s->base+s->stacksize;

s->stacksize+=STACKINCREMENT;

}

(*(s->top)).ord=e.ord;

(*(s->top)).seat.x=e.seat.x;

(*(s->top)).seat.y=e.seat.y;

(*(s->top)).di=e.di;

s->top++;

}

void Pop(SqStack *s,SElemType *e)//删除栈顶元素 用e返回

{

if(s->top==s->base) exit(0);

(s->top)--;

e->ord=(*(s->top)).ord;

e->seat.x=(*(s->top)).seat.x;

e->seat.y=(*(s->top)).seat.y;

e->di=(*(s->top)).di;

}

int StackEmpty(SqStack *s)//检查栈是否为空

{

if(s->top==s->base) return(1);

else return(0);

}

void ClearStack(SqStack *s)//将s栈清空

{

s->top=s->base;

s->stacksize=0;

}

//初始化迷宫

void initialarray(int (*p)[N])

{

int i,j;

int maze[M][N]={1,1,1,1,1,1,1,1,1,1,

1,0,0,1,0,0,0,1,0,1,

1,0,0,0,0,1,0,1,0,1,

1,0,0,1,1,0,0,0,0,1,

1,1,1,1,1,0,0,1,0,1,

1,0,0,0,1,0,0,0,0,1,

1,0,1,0,0,1,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};/***/

printf("打印迷宫:\n");

for(i=0;i

{

for(j=0;j

{

printf("%d",maze[i][j]);

*(*(p+i)+j)=maze[i][j];/***/利用二维数组

}

printf("\n");

}

}

///

void NextPos(PosType *p,int d)//确定下一个位置的坐标

{

switch(d)

{

case 1:p->y++;break;

case 2:p->x++;break;

case 3:p->y--;break;

case 4:p->x--;break;

}    /(1东2南3西4北)改变坐标

}

void MazePath(int (*maze)[N])//核心函数//---int (*maze)[N]

{

int curstep;

int mark[M][N]={0};

SqStack s;

PosType curpos;

SElemType e;//栈中元素结构体 1.序号;2.坐标位置;3.通道方向

initialStack(&s);

curpos.x=1;

curpos.y=1;

curstep=1;

do

{

if((!maze[curpos.x][curpos.y])&&(!mark[curpos.x][curpos.y]))//*******

{

mark[curpos.x][curpos.y]=1;

e.ord=curstep;//**e=(curstep,curpos,1)

e.seat.x=curpos.x;

e.seat.y=curpos.y;

e.di=1;//***

Push(&s,e);

if((curpos.x==M-2)&&(curpos.y==N-2))

{

flag=1;

printf("走出迷宫的一条坐标路径为:\n[终点]");

while(!StackEmpty(&s))

{

Pop(&s,&e);

printf("

}

printf("

printf("\n");

ClearStack(&s);

}

NextPos(&curpos,1);//curpos=NextPos(curpos,1)

curstep++;

}

else

{

if(!StackEmpty(&s))

Pop(&s,&e);

while(e.di==4&&!StackEmpty(&s))

{

mark[e.seat.x][e.seat.y]=1;

Pop(&s,&e);

}

if(e.di<4)

{

e.di++;

Push(&s,e);

curpos.x=e.seat.x;//**curpos=NextPos(e.seat,e,di)

curpos.y=e.seat.y;

NextPos(&curpos,e.di);///**

}

}

}while(!StackEmpty(&s));

}

void main()

{

int array[M][N];

initialarray(array);

MazePath(array);

if(flag==0)

printf("该迷宫不能走出去!\n");

}

6f83fddf9cb9ff6843fffd45e1807199.gif

Discuz!

好好学习  天天向上

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值