迷宫还是搞不懂,比如。。。。。。。。
#include"stdio.h"#include"malloc.h"#include"stdlib.h"typedef struct{int x,y;int ord;int der;}ElemType;
typedef struct{ElemType *base;ElemType *top;int stacksize;}Sqstack;
void InitStack(Sqstack &S);void Push(Sqstack &S, ElemType e);int Pop(Sqstack &S,ElemType &e);void NextPos(int *x, int *y, int der);
void InitStack(Sqstack &S){S.base=(ElemType*)malloc(sizeof(ElemType)*INIT_SIZE);if(!S.base) exit(1);S.top=S.base;S.stacksize=INIT_SIZE;}
void Push(Sqstack &S, ElemType e){if(S.top-S.base==S.stacksize){S.base=(ElemType*)malloc(sizeof(ElemType)*(INIT_SIZE+INCREMENT));S.top=S.base+S.stacksize;S.stacksize+=INCREMENT;}*(S.top++)=e;}
int Pop(Sqstack &S,ElemType &e){if(S.top==S.base) return 0;e=*(--S.top);return 1;}
void NextPos(int *x, int *y, int der){switch(der){case 1:++(*x);break;case 2:++(*y);break;case 3:--(*x);break;case 4:--(*y);break;default:break;
}}
int main(){Sqstack S;int x,y,step;ElemType e,*P;char maze[10][10]={{'#',' ','#','#','#','#','#','#','#','#'},{'#',' ',' ','#',' ',' ',' ','#',' ','#'},{'#',' ',' ','#',' ',' ',' ','#',' ','#'},{'#',' ',' ',' ',' ','#','#',' ',' ','#'},{'#',' ','#','#','#',' ',' ',' ',' ','#'},{'#',' ',' ',' ','#',' ',' ',' ',' ','#'},{'#',' ','#',' ',' ',' ','#',' ',' ','#'},{'#',' ','#','#','#',' ','#','#',' ','#'},{'#','#',' ',' ',' ',' ',' ',' ',' ','#'},{'#','#','#','#','#','#','#','#',' ','#'},
};
printf("The needed found maze are as follows:\n\n");for(x=0;x<10;x++){for(y=0;y<10;y++)printf("%c",maze[x][y]);printf("\n");}InitStack(S);P=S.base;x=0; y=1;step=1;do{if(maze[x][y]==' '){maze[x][y]='1';e.x=x;e.y=y;e.ord=step;e.der=1;
Push(S,e);if(x==9&&y==8) break;NextPos(&x,&y,1);step++;}else{if(S.base!=S.top){Pop(S,e);while(e.der==4){maze[e.x][e.y]='@';Pop(S, e);}}
if(e.der<4){e.der++;Push(S, e);x=e.x;y=e.y;NextPos(&x, &y, e.der);}}} while(S.top!=S.base);
printf("\n找到路径: 用1表示走的路,@表走的死路,#表墙: \n\n");for(x=0;x<10;x++){ for(y=0;y<10;y++)printf("%c",maze[x][y]);printf("\n");}printf("坐标表示如下:\n\n");
while(S.base!=S.top){if(P==S.base){printf("(%d,%d)", (*S.base).x, (*S.base).y);S.base++;}else{printf(" -> (%d,%d)", (*S.base).x, (*S.base).y);S.base++;}}printf("\n");
free(P);return 0;
上面划横线的地方不是很懂么1,为什么e.di==4时能判断那是死路?
2,P指针起什么作用?