用栈实现迷宫求解

严书上迷宫问题求解,讲了栈的用途,可以存放过程数值。自己实现很有难度(好像图的深度遍历也行,有什么联系呢?)。

核心代码如下:

 1 Status MazePath(PosType start, PosType end) 
 2 { 
 3   
 4   PosType curpos=start; 
 5   SqStack S; 
 6   SElemType e; 
 7   InitStack(S); 
 8   do
 9   { if(Pass(curpos)) 
10     { FootPrint(curpos); 
11       e.ord=curstep; 
12       e.seat=curpos; 
13       e.di=0; 
14       Push(S, e); 
15       curstep++; 
16       if(curpos.x==end.x && curpos.y==end.y) 
17         return TRUE;
18       NextPos(curpos, e.di); 
19     }
20     else 
21     { if(!StackEmpty(S)) 
22       { Pop(S, e); 
23         curstep--; 
24         while(e.di==3 && !StackEmpty(S)) 
25         { MarkPrint(e.seat); 
26           Pop(S, e); 
27           curstep--; 
28         }
29         if(e.di<3) 
30         { e.di++; 
31           Push(S, e); 
32           curstep++; 
33           curpos=e.seat; 
34           NextPos(curpos,e.di);
35         }
36       }
37     }
38   }while(!StackEmpty(S));
39   return FALSE;
40 }

posted @ 2019-03-21 11:00  蒋弢  阅读(245)  评论(1编辑  收藏  举报