#include"stdio.h" #include"stdlib.h" #define STACK_INIT_SIZE 100 #define STACKINCKEMENT 10 #define OK 1 #define ERROE 2 #define SIZE 10 typedef int Status; typedef struct { int row; int col; }PosType;//迷宫的元素 typedef struct { int ord; PosType seat; int di; }SElemType;//栈元素 typedef struct { SElemType *base; SElemType *top; int stacksize; }SqStack;//栈 typedef struct { int row; int col; int adr[SIZE][SIZE]; }MazeType;//迷宫 void InitMazeType(MazeType *M); void print(MazeType L); Status MazePath(PosType start,PosType end,MazeType *maze); void main() { MazeType L; PosType start,end; InitMazeType(&L); print(L); start.row=1; start.col=1; end.row=3; end.col=3; if(MazePath(start,end,&L)==OK) printf("走迷宫成功!"); else printf("走迷宫失败!"); } void InitMazeType(MazeType *M) { int i,j; printf("输入迷宫的行数M->row和列数M->col:"); scanf("%d %d",&M->row,&M->col); for(i=0;i<M->row;i++) { for(j=0;j<M->col;j++) { if(i==0||j==0||i==M->row-1||j==M->col-1) M->adr[i][j]=1; else { printf("若这个通道能通输入0否则输入1:"); scanf("%d",&M->adr[i][j]); } } } } void print(MazeType L) { int i,j; for(i=0;i<L.row;i++) { for(j=0;j<L.col;j++) { printf("%d",L.adr[i][j]); } printf("\n"); } } Status MazePath(PosType start,PosType end,MazeType *maze) { Status Pass(PosType curpos,MazeType maze); Status InitSqStack(SqStack *M); void FootPrint(PosType curpos,MazeType *maze); Status Push(SqStack *M,SElemType e); Status GetTop(SqStack M,SElemType *e); Status StackEmpty(SqStack M); Status Pop(SqStack *M,SElemType *e); void MarkPrint(PosType curpos,MazeType *maze); PosType NextPos(PosType curpos,int i);Status StackLengths(SqStack M); SqStack S; SElemType e,t,z; PosType curpos; int curstep; if(InitSqStack(&S)==OK) printf("栈初始化成功!"); else printf("栈初始化失败!"); curpos=start; curstep=1; do { if(Pass(curpos,*maze)==OK) { FootPrint(curpos,&(*maze)); e.seat.row=curpos.row; e.seat.col=curpos.col; e.di=1; e.ord=curstep; Push(&S,e); if(curpos.row==end.row&&curpos.col==end.col) { return OK; } else { curpos=NextPos(curpos,1); curstep++; } } else { GetTop(S,&t); if(t.di==4&&!StackEmpty(S)) { MarkPrint(t.seat,&(*maze)); Pop(&S,&z); } else { GetTop(S,&t); t.di++; Push(&S,t); curpos=t.seat; curpos=NextPos(curpos,t.di); } } }while(!StackEmpty(S)); //printf("%d",StackLengths(S)); return ERROE; } Status StackLengths(SqStack M)//返回元素个数 { return M.top-M.base; } Status Pass(PosType curpos,MazeType maze) { if(maze.adr[curpos.row][curpos.col]==0) return OK; return ERROE; } Status InitSqStack(SqStack *M) { M->base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType)); if(!M->base) return ERROE; M->base=M->top; M->stacksize=5;//STACK_INIT_SIZE; return OK; } void FootPrint(PosType curpos,MazeType *maze) { maze->adr[curpos.row][curpos.col]=2; }//走过的位置输入2 Status Push(SqStack *M,SElemType e)//插入元素,作为顶元素 { if(M->top-M->base==M->stacksize) { M->base=(SElemType*)realloc(M->base,(M->stacksize+STACKINCKEMENT)*sizeof(SElemType)); if(!M->base) return ERROE; M->top=M->base+M->stacksize; M->stacksize+=STACKINCKEMENT; } *((*M).top)=e; M->top++; return OK; } PosType NextPos(PosType curpos,int i) { if(i==1) { curpos.row++; return curpos; } else { if(i==2) { curpos.row--; return curpos; } else { if(i==3) { curpos.col++; return curpos; } else { curpos.col--; return curpos; } } } } Status GetTop(SqStack M,SElemType *e) { if(M.base==M.top) return ERROE; M.top--; *e=*(M.top); return OK; } Status StackEmpty(SqStack M) { return M.base==M.top?OK:ERROE; } void MarkPrint(PosType curpos,MazeType *maze) { maze->adr[curpos.row][curpos.col]=3; }//不能通过的记作3 Status Pop(SqStack *M,SElemType *e) { if(M->top==M->base) return ERROE; --M->top; *e=*(M->top); return OK; }