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 }

 

posted on 2016-12-18 16:03  Arthurian  阅读(2324)  评论(3编辑  收藏  举报