#define stack_init_size 200 #define overflow 0 #define ok 1 #define error 0 #include<stdio.h> #include<malloc.h> typedef int Status; typedef struct{ int x; int y; }PosType; int mg[10][10]; int zx[5],zy[5]; typedef struct { int ord; PosType seat; int di; }SElemType; typedef struct{ SElemType *base; SElemType *top; int stacksize; }SqStack; SqStack s; //構造一個空棧 Status InitStack(SqStack &s){ s.base =(SElemType *)malloc(stack_init_size * sizeof(SElemType)); if(!s.base) return overflow; s.top=s.base; s.stacksize=stack_init_size; return ok; } //當前塊能否經過 Status Pass(PosType e){ int i,j; i=e.x; j=e.y; if (mg[i][j]==0) //0時能夠經過 return ok; return overflow; } //留下足跡 Status FootPrint(PosType e){ int i,j; i=e.x; j=e.y; mg[i][j]=-1; return ok;} //壓入棧 Status Push(SqStack &s,SElemType e){ *s.top++=e; return ok;} //出棧 Status Pop(SqStack &s,SElemType &e){ if(s.top==s.base) return error; e=*--s.top; return ok; } //下一步 PosType NextPos(PosType &e,int di){ e.x=e.x+zx[di]; e.y=e.y+zy[di]; return e; } //是否空棧 Status StackEmpty(SqStack s){ if (s.top==s.base) return ok; return overflow; } //留下不能經過的足跡 Status MarkPrint(PosType e){ int i,j; i=e.x; j=e.y; mg[i][j]=1; return ok;} //迷宮函數 Status MazePath(int mg,PosType start,PosType end,SqStack &s){ PosType curpos; SElemType e; int curstep; curpos=start; curstep=1; do{ if(Pass(curpos)){ FootPrint(curpos); e.ord=curstep; e.seat.x=curpos.x; e.seat.y=curpos.y; e.di=1; Push(s,e); if((curpos.x==end.x)&&(curpos.y==end.y)) return ok; curpos=NextPos(curpos,1); curstep++;}//if else{ if(!StackEmpty(s)){ Pop(s,e); while(e.di==4 && !StackEmpty(s)){ MarkPrint(e.seat); Pop(s,e); }//while if(e.di<4){ e.di++; Push(s,e); curpos=NextPos(e.seat,e.di); }//if }//if }//else }while(!StackEmpty(s)); return error;}//MazePath //主函數 void main(){ PosType start,end; int i,j,x,y; SElemType e; for(i=1;i<=8;i++) for(j=1;j<=8;j++) scanf("%d",&mg[i][j]); for(i=0;i<=9;i++){ mg[i][0]=1;mg[i][9]=1;} for(j=0;j<=9;j++){ mg[0][j]=1;mg[9][j]=1;} InitStack(s); start.x=1; start.y=1; end.x=8; end.y=8; zx[1]=0; zy[1]=1; zx[2]=1; zy[2]=0; zx[3]=0; zy[3]=-1; zx[4]=-1; zy[4]=0; MazePath(**mg,start,end,s); while(!StackEmpty(s)) {Pop(s,e); x=e.seat.x; y=e.seat.y; printf(("%d,%d"),x,y);} }
//用鏈式棧來實現,棧頂插入刪除
#include 「stdio.h」 #include 「stdlib.h」 struct node { int row; int col; struct node *next; }; struct node *p; void push(int row,int col) { struct node *a; a=(struct node *)malloc(sizeof(struct node)); a->row=row; a->col=col; a->next=p; p=a; } void pop(void) { struct node *q; q=p; p=p->next; free(q); } int main(void) { int row=1; int col=1; int a[8][11]={{1,1,1,1,1,1,1,1,1,1,1}, {1,0,1,0,0,1,1,1,0,0,1}, {1,0,0,0,0,0,1,0,0,1,1}, {1,0,1,1,1,0,0,0,1,1,1}, {1,1,0,0,1,0,1,1,0,1,1}, {1,1,0,0,1,0,1,1,0,0,1}, {1,1,1,0,0,0,0,0,0,0,1}, {1,1,1,1,1,1,1,1,1,1,1}, }; /* struct node d; p=&d; */ p=(struct node *)malloc(sizeof(struct node)); p->row=1; p->col=1; p->next=NULL; /* push(row,col); a[row][col]=1; */ while(row!=6||col!=9) { if(a[row][col+1]==0) { col=col+1; push(row,col); a[row][col]=1; continue; } if(a[row-1][col]==0) { row=row-1; push(row,col); a[row][col]=1; continue; } if(a[row][col-1]==0) { col=col-1; push(row,col); a[row][col]=1; continue; } if(a[row+1][col]==0) { row=row+1; push(row,col); a[row][col]=1; continue; } pop(); if(p->next==NULL)break; row=p->row; col=p->col; }node
if(row==6&&col==9) {函數
while(p->next!=NULL) { printf(「%d %d\n」,p->row,p->col); pop(); } } else printf(「No Answer !!!」);code
return 0 ; }it