数据结构迷宫问题
怎样用栈求迷宫的多个路径?
以下程序只是一个路径:
Status MazePath(MazeType &maze,PosType start, PosType end){
//求解迷宫maze中,从入口start到出口end的一条路径
//若存在,返回TRUE,否则返回FALSE
SqStack s;SElemType e;
InitStack(s);
PosType curpos = start;
int curstep = 1; //探索第一部
do{
if( Pass(maze,curpos) ){ //如果当前位置可以通过,即是未曾走到的通道块
FootPrint(maze,curpos); //留下足迹
e = CreateSElem(curstep,curpos,1); //创建元素
Push(s,e);
if( PosEquare(curpos,end) ) 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);curstep--; //留下不能通过的标记,并退回一步
}
if(e.di<4){
e.di++; Push(s,e); //换一个方向探索
curpos = NextPos(e.seat,e.di); //求下一个节点
}
}
}
}while(!StackEmpty(s));
return FALSE;
}