C++回溯法走迷宮


 

 

  1 #include <iostream>  
  2 #include <iomanip>  
  3 #include <cstdlib>  
  4 using namespace std;
  5 
  6 #define MaxSize 100  
  7 int maze[10][10] =   //定義一個迷宮,0表示通道,1表示牆  
  8 {
  9     { 1,1,1,1,1,1,1,1,1,1 },
 10     { 1,0,0,1,1,0,0,1,0,1 },
 11     { 1,0,0,1,0,0,0,1,0,1 },
 12     { 1,0,0,0,0,1,1,0,0,1 },
 13     { 1,0,1,1,1,0,0,0,0,1 },
 14     { 1,0,0,0,1,0,0,0,0,1 },
 15     { 1,0,1,0,0,0,1,0,0,1 },
 16     { 1,0,1,1,1,0,1,1,0,1 },
 17     { 1,1,0,0,0,0,0,0,0,1 },
 18     { 1,1,1,1,1,1,1,1,1,1 }
 19 };
 20 
 21 struct Try //定義一個棧,保存路徑  
 22 {
 23     int i;               //當前方塊的行號  
 24     int j;               //當前廣場的列號  
 25     int d;              //di是下一可走方位的方位號  
 26 } path[MaxSize];         //定義棧  
 27 
 28 int top = -1;            //初始化棧指針  
 29 
 30 void findPath(int xb, int yb, int xe, int ye);
 31 
 32 int main()
 33 {
 34     int x1, x2, y1, y2;
 35     cout << "請設定起點(x1,y1):" << endl;
 36     cout << "x1:"; 
 37     cin >> x1;
 38     cout << "y1:";
 39     cin >> y1;
 40     cout << "請設定終點(x2,y2):" << endl;
 41     cout << "x2:";
 42     cin >> x2;
 43     cout << "y2:";
 44     cin >> y2;
 45     findPath(x1, y1, x2, y2);    //從(x1,y1)進入,從(x2,y2)出
 46     system("pause");
 47     return 0;
 48 }
 49 
 50 void findPath(int xb, int yb, int xe, int ye)            //路徑為從(xb,yb)到(xe,ye)  
 51 {
 52     int i, j, d, find, k;
 53     top++;                                             //初始方塊進棧  
 54     path[top].i = xb;
 55     path[top].j = yb;
 56     path[top].d = -1;
 57     maze[xb][yb] = -1;
 58     while (top>-1)                                      //棧不為空時循環  
 59     {
 60         i = path[top].i;
 61         j = path[top].j;
 62         d = path[top].d;
 63         if (i == xe && j == ye)                             //找到了出口,輸出路徑  
 64         {
 65             cout << "迷宮路徑如下:\n";
 66             for (k = 0; k <= top; k++)
 67             {
 68                 cout << "\t(" << path[k].i << "," << path[k].j << ")";
 69                 if ((k + 1) % 5 == 0) 
 70                     cout << endl;            //每輸出五個方塊后換一行  
 71             }
 72             cout << endl;
 73             return;
 74         }
 75         find = 0;
 76         while (d<4 && find == 0)                          //找下一個可走的點  
 77         {
 78             d++;
 79             switch (d)
 80             {
 81             case 0:  //向上  
 82                 i = path[top].i - 1;
 83                 j = path[top].j;
 84                 break;
 85             case 1: //向右  
 86                 i = path[top].i;
 87                 j = path[top].j + 1;
 88                 break;
 89             case 2:  //向下  
 90                 i = path[top].i + 1;
 91                 j = path[top].j;
 92                 break;
 93             case 3:  //向左  
 94                 i = path[top].i;
 95                 j = path[top].j - 1;
 96                 break;
 97             }
 98             if (maze[i][j] == 0) find = 1;                      //找到通路  
 99         }
100         if (find == 1)                                        //找到了下一個可走方塊  
101         {
102             path[top].d = d;                               //修改原棧頂元素的d值  
103             top++;                                         //下一個可走方塊進棧  
104             path[top].i = i;
105             path[top].j = j;
106             path[top].d = -1;
107             maze[i][j] = -1;                                 //避免重復走到這個方塊  
108                                                              //cout << "\t(" << path[top].i << "," << path[top].j << ")"; //顯示經過的試探  
109         }
110         else  //沒有路可走,則退棧  
111         {
112             maze[path[top].i][path[top].j] = 0;                  //讓該位置變成其它路徑可走方塊  
113             top--;
114         }
115     }
116     cout << "沒有可走路徑!"<<endl;
117 }

 

作者:耑新新,發布於  博客園

轉載請注明出處,歡迎郵件交流:zhuanxinxin@aliyun.com


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM