#defineOVERFLOW-2
#defineERROR0
#defineNULL0
#definetrue1
#defineTRUE1
#definefalse0
#defineFALSE0
#defineSTACK_INIT_SIZE100
#defineSTACKINCREMENT10
#include
#include
/*
初始化迷宫,1表示通道,0表示墙
*/
intmaze[8][8]={
1,1,0,1,1,1,0,1,
1,1,0,1,1,1,0,1,
1,1,1,1,0,0,1,1,
1,0,0,0,1,1,1,1,
1,1,1,0,1,1,1,1,
1,0,1,1,1,0,1,1,
1,0,0,0,1,0,0,1,
0,1,1,1,1,1,1,1
};
typedefstructMStackElem
{
intx;
inty;
intval;
}MStackElem;
typedefstruct{
MStackElem*base;
MStackElem*top;
intstackSize;
}MStack;
voidinitStack(MStack*s){
s->base=(MStackElem*)malloc(STACK_INIT_SIZE*sizeof(MStackElem));
if(!s->base){
printf("ininitStack()...FailedtoinitalizetheMStack,noenoughspace!exitnow.");
exit(OVERFLOW);
}
s->top=s->base;
s->stackSize=STACK_INIT_SIZE;
}
voidpush(MStack*s,MStackEleme){
if(s->top-s->base>=s->stackSize){
s->base=(MStackElem*)realloc(s->base,(STACK_INIT_SIZE+STACKINCREMENT)*sizeof(MStackElem));
if(!s->base){
printf("inpush()...FailedtorealloctheMStack,noenoughspace!exitnow.");
exit(OVERFLOW);
}
s->top=s->base+s->stackSize;
s->stackSize+=STACKINCREMENT;
}
*(s->top++)=e;
}
MStackElemgetTop(MStack*s){
if(s->top==s->base){
printf("ingetTop(),emptystack!exitnow.");
exit(ERROR);
}
else{
return*(s->top-1);
}
}
voidpop(MStack*s){
if(s->top==s->base){
printf("inpop(),emptystack!exitnow.");
exit(ERROR);
}
else{
--(s->top);
}
}
MStackrealPath,path;
intunPass(MStackpath,MStackElemcur){
intflag=1;
while(path.top!=path.base)
{
MStackEleme=*(path.top-1);
if(e.x==cur.x&;&;e.y==cur.y)
{
flag=0;
}
(path.top)--;
}
returnflag;
}
MStackElemgetEast(MStackElemcur){
if(cur.y!=7){
cur.y+=1;
cur.val=maze[cur.x][cur.y];
}
returncur;
}
MStackElemgetSouth(MStackElemcur){
if(cur.x!=7){
cur.x+=1;
cur.val=maze[cur.x][cur.y];
}
returncur;
}
MStackElemgetWest(MStackElemcur){
if(cur.y!=0){
cur.y-=1;
cur.val=maze[cur.x][cur.y];
}
returncur;
}
MStackElemgetNorth(MStackElemcur){
if(cur.x!=0){
cur.x-=1;
cur.val=maze[cur.x][cur.y];
}
returncur;
}
MStackElemgetNext(MStackElemcur){
MStackElemnext;
next.x=next.y=next.val=-1;
if(getEast(cur).val!=0&;&;unPass(path,getEast(cur))){
next=getEast(cur);
}
elseif(getSouth(cur).val!=0&;&;unPass(path,getSouth(cur))){
next=getSouth(cur);
}
elseif(getWest(cur).val!=0&;&;unPass(path,getWest(cur))){
next=getWest(cur);
}
elseif(getNorth(cur).val!=0&;&;unPass(path,getNorth(cur))){
next=getNorth(cur);
}
returnnext;
}
intgetMazePath(){
MStackElemstart,end,cur;
start.x=0;
start.y=0;
&nbs