当前位置:   article > 正文

迷宫问题(栈)_迷宫问题栈

迷宫问题栈

迷宫问题(栈与队列)

[问题描述]

利用栈操作实现迷宫问题求解

[基本要求]

(1)从文件中读取数据,生成模拟迷宫地图,不少于20行20列。

(2)给出任意入口和出口,显示输出迷宫路线。

文件内容:

解题思路:

这道题和八皇后问题比较类似。八皇后问题是在满足一定条件下,在一张图中寻找皇后放置的合适位置。这里则是根据出口和入口,在一张迷宫图中寻找路线,路线上的各点需满足:不重复不为墙壁

我们首先从文件中读取数据,生成迷宫地图。之后找出所有的迷宫口,并选定入口和出口。从入口出发,每次都对上下左右的四个位置进行探测,若该点满足条件(不重复且不为墙壁),则加入保存路线的栈route中(这里我们用visit[][]数组标注某一位置是否可行)。若未到达指定出口,且某一位置无法继续前进时,则将栈顶位置弹栈,处理前一位置,若仍无法继续前进,继续弹栈,直到可以前进为止。

按照此方法找到的路线即为从入口到出口的路线,路线上的各点保存在栈route中,最后打印输出包含路线的迷宫图即可。

程序代码:
  1. # include <iostream>
  2. # include <fstream>
  3. # include <string.h>
  4. # include <stack>
  5. # define maxn 30
  6. using namespace std;
  7. //地图类型的结构体
  8. typedef struct Point {
  9. int col; //行
  10. int row; //列
  11. }Point;
  12. //创建迷宫
  13. void Create();
  14. //打印迷宫
  15. void Print();
  16. //生成出入口
  17. void FindGate();
  18. //判断位置是否可行
  19. bool Judge(int x, int y);
  20. //走迷宫
  21. void GoMaze();
  22. int m = 0, n = 0; //迷宫的行、列
  23. int maze[maxn][maxn]; //迷宫地图
  24. int gate[maxn][2]; //保存迷宫出入口
  25. int vis[maxn][maxn]; //记录走过的位置
  26. //在迷宫的前进方式
  27. int way[4][2] = { {0,1},{-1,0},{1,0},{0,-1} };
  28. stack<Point>route; //保存路线的栈
  29. int path[maxn][maxn] = { {0} }; //路线
  30. int main()
  31. {
  32. Create(); //创建迷宫
  33. Print(); //打印迷宫图
  34. int select;
  35. while (1) {
  36. cout << "\n开始游戏!" << endl;
  37. FindGate(); //生成出入口
  38. GoMaze(); //走迷宫
  39. cout << endl;
  40. Print(); //打印迷宫图(包含路线)
  41. for (int i = 1; i <= m; i++) {
  42. for (int j = 1; j <= n; j++) {
  43. path[i][j] = 0;
  44. vis[i][j] = 0;
  45. }
  46. }
  47. cout << "\n是否继续游戏? (1:是 2:否)" << endl;
  48. cout << "请输入:";
  49. cin >> select;
  50. if (select != 1) {
  51. break;
  52. }
  53. }
  54. cout << "\n游戏结束!" << endl;
  55. return 0;
  56. }
  57. //创建迷宫
  58. void Create()
  59. {
  60. fstream file;
  61. file.open("迷宫样例.txt", ios::in);
  62. if (file.fail()) {
  63. cout << "文件打开失败" << endl;
  64. exit(0);
  65. }
  66. char str[100];
  67. char c;
  68. int i = 0;
  69. file.getline(str, 100);
  70. while (str[i] != ' ' && i < strlen(str)) {
  71. m *= 10;
  72. m = m + str[i] - 48;
  73. i++;
  74. }
  75. i++;
  76. while (str[i] != ' ' && i < strlen(str)) {
  77. n *= 10;
  78. n = n + str[i] - 48;
  79. i++;
  80. }
  81. int k = 1, j;
  82. file.getline(str, 100);
  83. while (!file.eof()) {
  84. j = 1;
  85. for (int i = 0; i < strlen(str); i = i + 2)
  86. {
  87. maze[k][j] = str[i] - 48;
  88. j++;
  89. }
  90. k++;
  91. file.getline(str, 100);
  92. }
  93. j = 1;
  94. for (int i = 0; i < strlen(str); i = i + 2) {
  95. maze[k][j] = str[i] - 48;
  96. j++;
  97. }
  98. }
  99. //打印迷宫及路线
  100. void Print()
  101. {
  102. for (int i = 1; i <= m; i++) {
  103. for (int j = 1; j <= n; j++) {
  104. if (maze[i][j]) {
  105. cout << "D";
  106. }
  107. else if (path[i][j]) {
  108. cout << "*";
  109. }
  110. else {
  111. cout << " ";
  112. }
  113. }
  114. cout << endl;
  115. }
  116. }
  117. //生成出入口
  118. void FindGate()
  119. {
  120. int k = 1; //出入口序号
  121. for (int i = 1; i <= m; i++) {
  122. if (maze[i][1] == 0) {
  123. cout << "(" << k << ")";
  124. cout << i << " " << 1 << endl;
  125. gate[k][0] = i;
  126. gate[k][1] = 1;
  127. k++;
  128. }
  129. if (maze[i][n] == 0) {
  130. cout << "(" << k << ")";
  131. cout << i << " " << n << endl;
  132. gate[k][0] = i;
  133. gate[k][1] = n;
  134. k++;
  135. }
  136. }
  137. for (int i = 2; i < n; i++) {
  138. if (maze[1][i] == 0) {
  139. cout << "(" << k << ")";
  140. cout << 1 << " " << i << endl;
  141. gate[k][0] = 1;
  142. gate[k][1] = i;
  143. k++;
  144. }
  145. if (maze[m][i] == 0) {
  146. cout << "(" << k << ")";
  147. cout << m << " " << i << endl;
  148. gate[k][0] = m;
  149. gate[k][1] = i;
  150. k++;
  151. }
  152. }
  153. }
  154. //判断位置是否可行
  155. bool Judge(int x, int y)
  156. {
  157. if (x > m || x < 1 || y > n || y < 1 || maze[x][y] || vis[x][y]) {
  158. //超过边界或该点为墙壁或上一次走过,则不通
  159. return false;
  160. }
  161. else {
  162. return true;
  163. }
  164. }
  165. //走迷宫
  166. void GoMaze()
  167. {
  168. int x, y, x1, y1, x2, y2;
  169. cout << "请输入入口序号:";
  170. cin >> x;
  171. x1 = gate[x][0];
  172. y1 = gate[x][1];
  173. cout << "请输入出口序号:";
  174. cin >> y;
  175. x2 = gate[y][0];
  176. y2 = gate[y][1];
  177. Point current; //现在所处的位置
  178. current.col = x1;
  179. current.row = y1;
  180. path[current.col][current.row] = 1;
  181. vis[current.col][current.row] = 1;
  182. route.push(current); //将入口放入路线
  183. //到达终点前会一直寻找路线
  184. while (current.col != x2 || current.row != y2) {
  185. int t = 0; //判断是否可以继续前进
  186. while (!t) {
  187. for (int i = 0; i < 4; i++) {
  188. current.col = current.col + way[i][0];
  189. current.row = current.row + way[i][1];
  190. if (Judge(current.col, current.row)) {
  191. //可以继续前进
  192. t = 1;
  193. route.push(current); //将该位置放入路线
  194. path[current.col][current.row] = 1;
  195. vis[current.col][current.row] = 1;
  196. break;
  197. }
  198. else {
  199. //不通
  200. current.col = current.col - way[i][0];
  201. current.row = current.row - way[i][1];
  202. continue;
  203. }
  204. }
  205. if (!t) {
  206. //若不能继续前进,则弹栈
  207. if (!route.empty()) {
  208. route.pop();
  209. }
  210. path[current.col][current.row] = 0;
  211. //更新当前位置
  212. if (!route.empty()) {
  213. current.col = route.top().col;
  214. current.row = route.top().row;
  215. }
  216. }
  217. }
  218. }
  219. }

运行结果:

  1. DDD DDDDDD DDDD
  2. DDDDDD DD D DDDD
  3. DD DDD DD DDDDDD DDDD
  4. DD DDD DDD D DDDDDD DDDD
  5. DD DDDDDDD D
  6. DD DDDD D DDDDDDDDDD
  7. DDDD DDD DDD DDDDDDDDDD
  8. DDDD D DDD DDDDDDDDDD
  9. DDDD DDD D D DDDDD
  10. DDDD DDD D D DDDDDD DDDDD
  11. DDDD D D DDD DDDDD
  12. DDDDDDDDDD DDDD DDD DDDDD
  13. DDD D DDDD DDD DDDDD
  14. DDD DDDD D DDD
  15. DDD DDDDDDDDD DDDDD DDDDD
  16. DDD DDDD DDDDD DDDDD
  17. DDD DDDD DDDD DDDDD DDDDD
  18. DDD DDDD DDDD DDDDD DDDDD
  19. DDD DDDD DDDDD
  20. DDD DDDD DD DDD DDDDDDDDD
  21. DDD DDD DDDDDDDDD
  22. DDD DDDDDD DDDD DD
  23. DDD DDDDDD DDDDDD DDDD DD
  24. DDD DDDDDD DDDDDD DDDD DD
  25. DDDDDD DDDD DDDD
  26. 开始游戏!
  27. (1)1 1
  28. (2)5 25
  29. (3)14 25
  30. (4)25 1
  31. (5)25 25
  32. (6)1 2
  33. (7)25 2
  34. (8)1 3
  35. (9)25 3
  36. (10)1 4
  37. (11)25 4
  38. (12)1 5
  39. (13)1 6
  40. (14)1 7
  41. (15)1 11
  42. (16)25 11
  43. (17)1 12
  44. (18)1 13
  45. (19)1 14
  46. (20)25 16
  47. (21)25 17
  48. (22)25 18
  49. (23)1 21
  50. (24)25 23
  51. (25)25 24
  52. 请输入入口序号:1
  53. 请输入出口序号:5
  54. *******DDD DDDDDD DDDD
  55. DDDDDD* DD D DDDD
  56. DD*****DDD DD DDDDDD DDDD
  57. DD*DDD DDD D DDDDDD DDDD
  58. DD*DDDDDDD D
  59. DD*** DDDD D DDDDDDDDDD
  60. DDDD*DDD***DDD DDDDDDDDDD
  61. DDDD*****D*DDD DDDDDDDDDD
  62. DDDD DDD D*D DDDDD
  63. DDDD DDD D*D DDDDDD DDDDD
  64. DDDD D*D DDD DDDDD
  65. DDDDDDDDDD*DDDD DDD DDDDD
  66. DDD******D*DDDD DDD DDDDD
  67. DDD*DDDD*** D DDD
  68. DDD*DDDDDDDDD DDDDD DDDDD
  69. DDD******DDDD DDDDD DDDDD
  70. DDD DDDD*DDDD DDDDD DDDDD
  71. DDD DDDD*DDDD DDDDD DDDDD
  72. DDD DDDD******** DDDDD
  73. DDD DDDD DD DDD*DDDDDDDDD
  74. DDD DDD*DDDDDDDDD
  75. DDD DDDDDD DDDD********DD
  76. DDD DDDDDD DDDDDD DDDD*DD
  77. DDD DDDDDD DDDDDD DDDD*DD
  78. DDDDDD DDDD DDDD***
  79. 是否继续游戏? (1:是 2:否)
  80. 请输入:1
  81. 开始游戏!
  82. (1)1 1
  83. (2)5 25
  84. (3)14 25
  85. (4)25 1
  86. (5)25 25
  87. (6)1 2
  88. (7)25 2
  89. (8)1 3
  90. (9)25 3
  91. (10)1 4
  92. (11)25 4
  93. (12)1 5
  94. (13)1 6
  95. (14)1 7
  96. (15)1 11
  97. (16)25 11
  98. (17)1 12
  99. (18)1 13
  100. (19)1 14
  101. (20)25 16
  102. (21)25 17
  103. (22)25 18
  104. (23)1 21
  105. (24)25 23
  106. (25)25 24
  107. 请输入入口序号:2
  108. 请输入出口序号:4
  109. DDD DDDDDD DDDD
  110. DDDDDD DD D DDDD
  111. DD DDD DD DDDDDD DDDD
  112. DD DDD DDD D DDDDDD DDDD
  113. DD DDDDDDD D***********
  114. DD DDDD D*DDDDDDDDDD
  115. DDDD DDD DDD*DDDDDDDDDD
  116. DDDD D DDD*DDDDDDDDDD
  117. DDDD DDD D D ******DDDDD
  118. DDDD DDD D D DDDDDD*DDDDD
  119. DDDD D D DDD*DDDDD
  120. DDDDDDDDDD DDDD DDD*DDDDD
  121. DDD D DDDD DDD*DDDDD
  122. DDD DDDD D DDD*
  123. DDD DDDDDDDDD DDDDD*DDDDD
  124. DDD******DDDD DDDDD*DDDDD
  125. DDD*DDDD*DDDD DDDDD*DDDDD
  126. DDD*DDDD*DDDD DDDDD*DDDDD
  127. DDD*DDDD* *********DDDDD
  128. DDD*DDDD*DD*DDD DDDDDDDDD
  129. DDD* ****DDD DDDDDDDDD
  130. DDD*DDDDDD DDDD DD
  131. DDD*DDDDDD DDDDDD DDDD DD
  132. DDD*DDDDDD DDDDDD DDDD DD
  133. ****DDDDDD DDDD DDDD
  134. 是否继续游戏? (1:是 2:否)
  135. 请输入:2
  136. 游戏结束!

以上便是我对这道题的看法,很高兴与大家分享。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/703592
推荐阅读
相关标签
  

闽ICP备14008679号