【堆栈应用二】迷宫算法,可直接动态运行

  1 #include<iostream>
  2 #include<fstream>
  3 #include<stack>
  4 #include<Windows.h>
  5 using namespace std;
  6 struct Position //定义坐标结构
  7 {
  8     int row, col;
  9 };
 10 class Maze
 11 {
 12 public:
 13     void ReadTxt();
 14     void DisplayMaze();
 15     bool FindPath();
 16     void WalkPath();
 17 
 18 private:
 19     int n;
 20     int step;
 21     int maze[100][100];
 22     stack<Position> path;//定义路线堆栈
 23 };
 24 
 25 void Maze::ReadTxt()//读入txt文件
 26 {
 27     
 28     ifstream inFile;
 29     inFile.open("maze.txt");
 30     inFile>>n;
 31     for(int i=0;i<n;i++)
 32         for(int j=0;j<n;j++)
 33             inFile>>maze[i][j];
 34     inFile.close();
 35 }
 36 
 37 void Maze::DisplayMaze()//打印原始迷宫
 38 {
 39     for(int i=0;i<n;i++)
 40     {
 41         for(int j=0;j<n;j++)
 42         {
 43             if((i==1&&j==0)||(i==n-2&&j==n-1))
 44                 cout<<"=>";
 45             else if(maze[i][j]==1)
 46                 cout<<"##";
 47             else cout<<"  ";
 48         }
 49         cout<<endl;
 50     }
 51 }
 52 bool Maze::FindPath()//找到路线并显示,无则给出“no path”信息
 53 {
 54     bool IfPath=true;
 55     Position offset[4];
 56     offset[0].row = 0; offset[0].col = 1; //
 57     offset[1].row = 1; offset[1].col = 0; //
 58     offset[2].row = 0; offset[2].col = -1; //
 59     offset[3].row = -1; offset[3].col = 0; //
 60 
 61     int a[100][100];
 62     for(int i=0;i<n;i++)
 63         for(int j=0;j<n;j++)
 64             a[i][j]=maze[i][j];
 65     Position here;
 66     here.row = 1;
 67     here.col = 0;
 68     a[1][0] = 1; // 避免返回起点
 69 
 70     int option = 0; // 下一动作的方向
 71     int LastOption = 3;
 72 
 73     while (here.row != (n-2) || here.col != (n-2)) //循环直到到达终点
 74     {
 75         int r,c;
 76         while (option <= LastOption) //找到下一动作的方向
 77         {
 78             r = here.row + offset[option].row;
 79             c = here.col + offset[option].col;
 80             if (a[r][c] == 0) break;
 81             option++; // 切换下一方向
 82         }
 83 
 84         if (option <= LastOption) //如果下一动作可行,即有一个方向可行
 85         {
 86             path.push(here);//将此刻的位置压入栈中
 87             here.row = r; 
 88             here.col = c;
 89             a[r][c] = 1;//将此刻的位置设为不可行
 90             option = 0;//将option归零
 91         }
 92         else //如果下一动作不可行,回溯
 93         {
 94             if(path.empty()) {IfPath= false;break;}//堆栈空,不可回溯,无解
 95             Position next=path.top();//弹出堆栈,设置option
 96             path.pop();
 97             if (next.row == here.row)
 98                 option = 2 + next.col - here.col;
 99             else option =3+ next.row - here.row;        
100             here = next;
101         }
102     }
103     if(IfPath==true)
104     {
105         int step=0;
106         Position a;
107         a.row=n-2;a.col=n-2;
108         path.push(a);
109         stack<Position> tmp;
110         while(!path.empty ())
111         {
112             tmp.push(path.top());
113             path.pop();
114         }
115         while(!tmp.empty())
116         {
117             cout<<'('<<tmp.top().row<<','<<tmp.top().col<<')'<<"->";
118             path.push(tmp.top());
119             tmp.pop();
120             step++;
121         }
122         cout<<endl<<"the number of the total steps: "<<step<<endl;
123     }
124     else  cout<<"no path"<<endl;
125     return IfPath;
126 }
127 void Maze::WalkPath()//动态显示
128 {
129     stack<Position> tmp;
130 
131     while(!path.empty ())
132     {
133         tmp.push(path.top());
134         path.pop();
135     }
136 
137     while(!tmp.empty())
138     {
139         system("cls");
140         for(int i=0;i<n;i++)
141     {
142         for(int j=0;j<n;j++)
143         {
144             if((i==1&&j==0)||(i==n-2&&j==n-1))
145                 cout<<"=>";
146             else if(i==tmp.top().row&&j==tmp.top().col)
147             {
148                 cout<<"";
149             }
150             else if(maze[i][j]==1)
151                 cout<<"##";
152             else cout<<"  ";
153         }
154         cout<<endl;
155     }
156         path.push(tmp.top());
157         tmp.pop();
158         Sleep(250); 
159     }
160 }
161 #include<iostream>
162 #include<Windows.h>
163 #include<conio.h>
164 using namespace std;
165 int main()
166 {
167     Maze maze1;
168     maze1.ReadTxt();
169     maze1.DisplayMaze();
170     bool t=maze1.FindPath();
171     
172     if(t==1)
173     {
174         char s;
175         cout<<endl<<"按任意键开始动态显示迷宫路径."<<endl;
176         getch();
177         maze1.WalkPath();
178     }
179     system("pause");
180     return 0;
181 }

 

posted @ 2016-03-18 14:51  隅子酱  阅读(557)  评论(0编辑  收藏  举报