求大神帮忙改下纯C语言实现的迷宫程序解决方案

求大神帮忙改下纯C语言实现的迷宫程序

//头文件SqStack.h
#ifndef SQSTACK_H_
#define SQSTACK_H_

#ifndef constant
#define constant

#define ElemType int
#define OVERFLOW 0
#define OK 1
#define ERROR 0

typedef int Status;

#include <stdlib.h>

#endif /* constant */

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

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

#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10

typedef struct {
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;

Status InitStack_Sq(SqStack *S);
Status CreateStack_Sq(SqStack *S,SElemType elem[],int n);
Status DestroyStack_Sq(SqStack *S);
Status ClearStack_Sq(SqStack *S);
int EmptyStack_Sq(SqStack *S);
int LengthStack_Sq(SqStack *S);
Status GetTop_Sq(SqStack *S,SElemType *e);
Status Push_Sq(SqStack *S,SElemType e);
Status Pop_Sq(SqStack *S,SElemType *e);

#endif /* SQSTACK_H_ */


//栈实现,应该没有问题,测试过SqStack.c

#include "SqStack.h"
int mutex;
Status InitStack_Sq(SqStack *S){
if(mutex==1) {free(S->base);mutex=0;S->base=S->top=NULL;}
S->base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!S->base) exit(OVERFLOW);
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
mutex=1;
return OK;
}

Status CreateStack_Sq(SqStack *S,SElemType elem[],int n){
int i;
if(mutex==1) {free(S->base);mutex=0;S->base=S->top=NULL;}
S->base=(SElemType *)malloc((n/STACK_INIT_SIZE+1)*STACK_INIT_SIZE*sizeof(SElemType));
if(!S->base) exit(OVERFLOW);
S->top=S->base;
S->stacksize=STACK_INIT_SIZE*(n/STACK_INIT_SIZE+1);
mutex=1;
for(i=0;i<n;i++){
*S->top++=elem[i];
}
return OK;
}

Status DestroyStack_Sq(SqStack *S){
if(mutex==1) {free(S->base);mutex=0;S->base=S->top=NULL;}
return OK;
}
Status ClearStack_Sq(SqStack *S){
S->top=S->base;
return OK;
}
int EmptyStack_Sq(SqStack *S){
return S->base==S->top;
}
int LengthStack_Sq(SqStack *S){
return S->top-S->base;
}
Status GetTop_Sq(SqStack *S,SElemType *e){
if(S->base==S->top)return ERROR;
*e=*(S->top-1);

return OK;
}

Status Push_Sq(SqStack *S,SElemType e){
if(S->top-S->base>=S->stacksize){
S->base=(SElemType *)realloc(S->base,(STACK_INIT_SIZE+STACKINCREMENT)*sizeof(SElemType));
if(!S->base) exit(OVERFLOW);
S->top=S->base+S->stacksize;
S->stacksize=S->stacksize+STACKINCREMENT;
}
*S->top++=e;
return OK;
}

Status Pop_Sq(SqStack *S,SElemType *e){
if(S->top!=S->base)
*e=*--(S->top);
else
return ERROR;
return OK;
}

//迷宫程序MazePath.c 不要求把迷宫路径,只要把路径压入栈中即可

#define M 8
#define N 8
#include "SqStack.h"
#include <stdio.h>

PosType NextPos(PosType *pos,int di){
switch(di){
case 0:pos->x--;break;
case 1:pos->y++;break;
case 2:pos->x++;break;
case 3:pos->y--;break;
}
return *pos;
}

Status MazePath(SqStack *S,int maze[][10],PosType start,PosType end){
PosType curpos=start;
int curstep=1;
SElemType e;
InitStack_Sq(&S);

do{

if(maze[start.x][start.y]==0){
e.ord=curstep;e.seat=curpos;e.di=1;
Push_Sq(&S,e);
if(curpos.x==end.x && curpos.y==end.y ) return OK;
curpos=NextPos(&curpos,1);
curstep++;
}
else{

if(!EmptyStack_Sq(&S)){
Pop_Sq(&S,&e);
while(e.di==4 && !EmptyStack_Sq(&S)){
maze[e.seat.x][e.seat.y]=1;
Pop_Sq(&S,&e);



}

if(e.di<4){
e.di++;
Push_Sq(&S,e);

curpos=NextPos(&e.seat,e.di);



}

}


}




}while(!EmptyStack_Sq(&S));





return ERROR;
}

int main(){
SqStack S;
int find=0;
int mg[M+2][N+2]={
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 1, 0, 0, 0, 1, 0, 1},
{1, 0, 1, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 0, 0, 0, 1, 1, 0, 0, 1},
{1, 0, 1,  1, 1, 0, 1, 1, 0, 1},
{1, 0, 0, 0, 1, 0, 0, 0, 1, 1},
{1, 0, 1, 0, 0, 0, 1, 0, 0, 1},
{1, 0, 1, 1, 1, 0, 1, 1, 0, 1},