Status MazePath(MazeType maze[][N],Postype start,Postype end) { SqStack S; SElemType_Sq nodeInf; // 当前通道的信息 Postype curPos; // 当前位置 int curStep; // 当前通道的序号 InitList_Sq(&S); curPos = start; curStep = 1; do { if (Pass(curPos,maze)) { // 当前位置可通过? FootPrint(curPos,maze); //留下足迹 ShowMaze(maze); SetSElemType(&nodeInf,curStep,curPos,East); // 设置当前位置的属性 Push_sq(&S,nodeInf); // 加入栈 if (EqualPosType(curPos,end)) { // 如果到终点 printf("\n 寻路成功!!\n\n"); return TRUE; } curPos = NextPos(curPos,East); // 设置下一个为东 curStep++; // 探索下一块 } else { if (!StackEmpty_Sq(S)) {// 如果没有倒退到原点 Pop_Sq(&S,&nodeInf); while (nodeInf.di == North && !StackEmpty_Sq(S)) { MarkPrint(nodeInf.seat,maze); // 四个方向都遍历过,就回退一步 ShowMaze(maze); Pop_Sq(&S,&nodeInf); } if (nodeInf.di < North) { maze[nodeInf.seat.x][nodeInf.seat.y] = ++nodeInf.di; ShowMaze(maze); Push_sq(&S,nodeInf); curPos = NextPos(nodeInf.seat,nodeInf.di); } } } } while (!StackEmpty_Sq(S)); printf("\n 寻路失败!!\n\n"); return FALSE; }
Status MazePath(PosType start,PosType end) /* 算法3.3 */ { /* 若迷宫maze中存在从入口start到出口end的通道,则求得一条 */ /* 存放在栈中(从栈底到栈顶),并返回TRUE;否则返回FALSE */ SqStack S; PosType curpos; SElemType e; InitStack(&S); curpos=start; do { if(Pass(curpos)) { /* 当前位置可以通过,即是未曾走到过的通道块 */ FootPrint(curpos); /* 留下足迹 */ e.ord=curstep; e.seat.x=curpos.x; e.seat.y=curpos.y; e.di=0; Push(&S,e); /* 入栈当前位置及状态 */ curstep++; /* 足迹加1 */ if(curpos.x==end.x&&curpos.y==end.y) /* 到达终点(出口) */ return TRUE; curpos=NextPos(curpos,e.di); } else { /* 当前位置不能通过 */ if(!StackEmpty(S)) { Pop(&S,&e); /* 退栈到前一位置 */ curstep--; while(e.di==3&&!StackEmpty(S)) /* 前一位置处于最后一个方向(北) */ { MarkPrint(e.seat); /* 留下不能通过的标记(-1) */ Pop(&S,&e); /* 退回一步 */ curstep--; } if(e.di<3) /* 没到最后一个方向(北) */ { e.di++; /* 换下一个方向探索 */ Push(&S,e); curstep++; curpos=NextPos(e.seat,e.di); /* 设定当前位置是该新方向上的相邻块 */ } } } }while(!StackEmpty(S)); return FALSE; }
/* * @description:求解迷宫路径 */ Status MazePath(MazeType maze,SqStack *S,PosType start,PosType end) { PosType curpos; int curstep; SElemType elem; InitStack(S); curpos = start; curstep = 1; do { if(Pass(&maze,curpos)) { FootPrint(&maze,curpos); elem = SetElem(curpos,curstep,1); Push(S,elem); if(curpos.x == end.x && curpos.y == end.y ) return TRUE; curpos = NextPos(curpos,1); curstep++; } else { if(!StackEmpty(*S)) { Pop(S,&elem); //这种情况是:东南西已经 while(elem.di == 4 && !StackEmpty(*S)) { MarkPrint(&maze,elem.seat); //此路不通,做标记 Pop(S,&elem); } if(elem.di < 4) { //按东南西北的方向调整探测 elem.di++; Push(S,elem); curpos = NextPos(elem.seat,elem.di); } } } }while(!StackEmpty(*S)); return FALSE; }
Status MazePath(MazeType &maze,PostType start,PostType end){ //迷宫maze存在从入口start到end的通道则求得一条存放在栈中 Stack S; PostType curpos; int curstep;//当前序号,1.2.3.4分别表示东,南,西,北方向 SElemType e; InitStack(S); curpos=start; //设置"当前位置"为"入口位置" curstep=1; //探索第一步 do{ if(Pass(maze,curpos)){ //当前位置可以通过, FootPrint(maze,curpos);//留下足迹 e.ord=curstep; e.seat=curpos; e.di=1; Push(S,e); //加入路径 if(curpos.r==end.r&& curpos.c==end.c) return TRUE; //到达出口 curpos=NextPos(curpos,1); //下一位置是当前位置的东邻 curstep++; //探索下一步 } else{ //当前位置不通 if(!StackEmpty(S)){ Pop(S,e); while(e.di==4 && !StackEmpty(S)){ MarkPrint(maze,e.seat); Pop(S,e); //留下不能通过的标记,并退一步 }//while if(e.di < 4){ e.di++;//换下一个方向探索 Push(S,e); curpos=NextPos(e.seat,e.di);//设定当前位置是该新方向上的相邻 } } } }while(!StackEmpty(S)); return OK; }//MazePath