/*注:本程序探索迷宫的优先顺序=> 1-下、2-右、3-上、4-左 <=总体趋势:下右,逆时针方向。因为出口就在右边下方*/ #include #include #include #define stack_init_size 200 #define stack_increment 10 #define OVERFLOW 0 #define OK 1 #define ERROE 0 #define TRUE 1 #define FALSE 0 typedef int Status; typedef struct{ int x; int y; }PosType; typedef struct { int ord; // 通道块在路径上的“序号” PosType seat; //通道块在迷宫中的“坐标位置” int di; //从此通道块走向下一通道块的“方向” }SElemType; typedef struct{ SElemType *base; SElemType *top; int stacksize; }SqStack; int mg[20][20]; /*随机生成迷宫的函数 /*为了能够让尽量能通过,将能通过的块和不能通过的块数量比大致为2:1*/ void Random(){ int i,j,k; srand(time(NULL)); mg[1][0]=mg[1][1]=mg[18][19]=0; //将入口、出口设置为“0”即可通过 for(j=0;j<20;j++) mg[0][j]=mg[19][j]=1; /*设置迷宫外围“不可走”,保证只有一个出口和入口*/ for(i=2;i<19;i++) mg[i][0]=mg[i-1][19]=1; /*设置迷宫外围“不可走”,保证只有一个出口和入口*/ for(i=1;i<19;i++) for(j=1;j<19;j++){ k=rand()%3; //随机生成0、1、2三个数 if(k) mg[i][j]=0; else{ if((i==1&&j==1)||(i==18&&j==18)) /*因为距入口或出口一步的路是必经之路,故设该通道块为“0”加大迷宫能通行的概率*/ mg[i][j]=0; else mg[i][j]=1; } } } //构造一个空栈 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){ if (mg[e.x][e.y]==0) //0时可以通过 return OK; // 如果当前位置是可以通过,返回1 return OVERFLOW; // 其它情况返回0 } //留下通过的足迹 Status FootPrint(PosType e){ mg[e.x][e.y]=7; return OK; } //压入栈 Status Push(SqStack &s,SElemType e){ if(s.top-s.base>=s.stacksize){ s.base=(SElemType *)realloc(s.base,(s.stacksize+stack_increment) *sizeof(SElemType)); if(!s.base)exit(OVERFLOW); s.top...