`

迷宫求解算法

阅读更多

  1. 原文出处: http://blog.csdn.net/gzzheyi/article/details/6686351 
  2. Status MazePath(MazeType (*Maze)[MAX],PosType start,PosType end)  
  3. {  
  4.     SElemType   e ;  
  5.     MazeStack S ;  
  6.     PosType curpos ;  
  7.   
  8.     int curstep ;  
  9.   
  10.     InitStack(&S) ;     //初始化栈  
  11.   
  12.     curpos = start ;  // start.x= start.y = 0  
  13.   
  14.     curstep = 1 ;  
  15.   
  16.     do  
  17.     {  
  18.         if(Pass(curpos,Maze))  
  19.         {  
  20.             FootPrint(curpos,Maze) ;  
  21.             e.di = 1 ;  
  22.             e.seat = curpos ;  
  23.             e.ord = curstep ;  
  24.   
  25.             Push(&S,e) ;  
  26.   
  27.             if(curpos.x == end.x && curpos.y == end.y )  
  28.             {  
  29.                 PrintMaze(Maze) ;  
  30.                 return TRUE ;  
  31.             }  //if  
  32.   
  33.             curpos = NextPos(&curpos,1) ;  
  34.             curstep++ ;  
  35.               
  36.         } //if  
  37.   
  38.         else  
  39.         {  
  40.             if(!StackEmpty(S))  
  41.             {  
  42.                 Pop(&S,&e) ;  
  43.   
  44.                 while(e.di == 4 && !StackEmpty(S))  
  45.                 {  
  46.                     MarkPrint(e.seat,Maze) ;  
  47.                     Pop(&S,&e) ;  
  48.                 }  //while  
  49.   
  50.                 if(e.di <4)  
  51.                 {  
  52.                     e.di++ ;  
  53.                     Push(&S,e) ;  
  54.                     curpos = NextPos(&(e.seat) ,e.di) ;  
  55.                 } //if  
  56.             } //if  
  57.         }  //else  
  58.     }while(!StackEmpty(S)) ;   //while  
  59.   
  60.   
  61.     PrintMaze(Maze) ;  
  62.   
  63.     return FALSE ;  
  64.   
  65. // MazePath 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics