1. #demo1
  2. #include<iostream>
  3. #include<ctime>
  4. #include<cstdlib>
  5. #include<queue>
  6. #include<cstdio>
  7. using namespace std;
  8. //生成迷宫
  9. const int HEIGHT = ;
  10. const int WIDTH = ;
  11. bool isFound = false;
  12. int maze[HEIGHT][WIDTH];
  13. void initialMaze()
  14. {
  15.  
  16. maze[][] = ;//入口
  17. maze[HEIGHT - ][WIDTH - ] = ;//出口
  18. for (int i = ; i < HEIGHT; i++)//用随机数0,1填充迷宫
  19. {
  20. for (int j = ; j < WIDTH; j++)
  21. {
  22. if (i == && j == )
  23. continue;
  24. if (i == HEIGHT - && j == WIDTH - )
  25. continue;
  26. maze[i][j] = rand() % ;
  27. }
  28. }
  29.  
  30. //展示生成的迷宫
  31. for (int i = ; i < HEIGHT; i++)
  32. {
  33. for (int j = ; j < WIDTH; j++)
  34. {
  35. cout << maze[i][j];
  36. if (j != WIDTH - )
  37. {
  38. cout << " ";
  39. }
  40. else
  41. {
  42. cout << endl;
  43. }
  44. }
  45. }
  46. }
  47. //生成方向
  48. int directory[][] = { {,},{,},{,},{,-},{,-},{-,-},{-,},{-,} };
  49. //判断是否越界
  50. bool isLeap(int x, int y)
  51. {
  52. return x >= && x < WIDTH&&y >= && y < HEIGHT;
  53.  
  54. }
  55. //任意位置的结构体
  56. struct point {
  57. int x;
  58. int y;
  59. };
  60. //声明用于存储路径的结构体
  61. struct dir
  62. {
  63. int x;
  64. int y;
  65. int d;
  66. };
  67. //声明用于存储路径的队列
  68. queue<dir> directoryQueue;
  69. //迷宫循迹
  70. dir path[HEIGHT][WIDTH];//记录迷宫的路径
  71. int output[HEIGHT*WIDTH][];
  72. void mazeTravel(point start, point end, int maze[HEIGHT][WIDTH], int directory[][])
  73. {
  74. dir element;
  75. //dir tmp;
  76. int i;
  77. int j;
  78. int d;
  79. int a;
  80. int b;
  81. element.x = start.x;
  82. element.y = start.y;
  83. element.d = -;
  84. maze[start.x][start.y] = ;
  85. directoryQueue.push(element);
  86. while (!directoryQueue.empty())
  87. {
  88. element = directoryQueue.front();
  89. dir m = element;
  90. directoryQueue.pop();
  91. i = element.x;
  92. j = element.y;
  93. d = element.d + ;
  94.  
  95. while (d < )
  96. {
  97. a = i + directory[d][];
  98. b = j + directory[d][];
  99. if (a == end.x&&b == end.y&&maze[a][b] == )
  100. {
  101. //储存前一个点的信息至path
  102. dir temp = m;
  103. temp.d = d;
  104. path[a][b] = temp;
  105.  
  106. isFound = true;
  107. return;
  108. }
  109. if (isLeap(a, b)&&maze[a][b]==)
  110. {
  111. //储存前一个点的信息至path
  112. dir temp = m;
  113. temp.d = d;
  114. path[a][b] = temp;
  115.  
  116. maze[a][b] = ;
  117. element.x = a;
  118. element.y = b;
  119. element.d = -;
  120. directoryQueue.push(element);
  121. }
  122. d++;
  123. }
  124. }
  125. }
  126. void printPath(point start, point end)
  127. {
  128. if (!isFound)
  129. printf("The path is not found");
  130. else
  131. {
  132. int step = ;
  133. dir q;
  134. q.x = end.x;
  135. q.y = end.y;
  136. q.d = ;
  137. while (q.x != start.x || q.y != start.y)
  138. {
  139. output[step][] = q.x;
  140. output[step][] = q.y;
  141. output[step][] = q.d;
  142. int x = q.x;
  143. int y = q.y;
  144. q.x = path[q.x][q.y].x;
  145. q.y = path[x][q.y].y;
  146. q.d = path[x][y].d;
  147. step++;
  148. }
  149. output[step][] = q.x;
  150. output[step][] = q.y;
  151. output[step][] = q.d;
  152. printf("The path is as follows: \n");
  153. for (int i = step; i >= ; i--)
  154. {
  155. printf("(%d,%d)", output[i][], output[i][]);
  156. if (i != )
  157. printf("->");
  158. }
  159. printf("\n");
  160. }
  161. }
  162. int main()
  163. {
  164. srand(time());
  165. initialMaze();
  166. point a, b;
  167. a.x = ;
  168. a.y = ;
  169. b.x = HEIGHT - ;
  170. b.y = WIDTH - ;
  171. mazeTravel(a, b, maze, directory);
  172. printPath(a, b);
  173. return ;
  174. }

输出

  1.  
  2. The path is as follows:
  3. (,)->(,)->(,)->(,)->(,)->(,)->(,)->(,)->(,)->(,)->(,)
  4. Program ended with exit code:

demo2

  1. #demo2
  2. #include <iostream>
  3. #include <vector>
  4. #include <queue>
  5. using namespace std;
  6. using std::vector;
  7. struct point
  8. {
  9. int x;
  10. int y;
  11. int step;
  12. point(int _x, int _y, int _step) :x(_x), y(_y), step(_step) {}
  13. point(int _x, int _y) :x(_x), y(_y), step(){}
  14. point(){}
  15. bool operator==(const point& other)const
  16. {
  17. return x == other.x&&y == other.y;
  18. }
  19. };
  20. int minSteps_BFS(const vector<vector<int>>& path, vector<vector<point>>& mp, point src, point des, int step);
  21. int main()
  22. {
  23. vector<vector<int>> path = { { , , , , }, { , , , , }, { , , , , }, { , , , , }, { , , , , } };
  24. vector<vector<point>> mp(, vector<point>());
  25. point src(,);
  26. point des(,);
  27. int step = ;
  28. cout << minSteps_BFS(path, mp, src, des, step) << endl;
  29. cout << "具体路径如下:" << endl;
  30. //vector<point> res;
  31. while (!(mp[des.x][des.y] == src))
  32. {
  33. cout << des.x << " " << des.y << endl;
  34. des = mp[des.x][des.y];
  35. }
  36. cout << des.x << " " << des.y << endl;
  37. return ;
  38. }
  39.  
  40. int minSteps_BFS(const vector<vector<int>>& path, vector<vector<point>>& mp, point src, point des, int step)
  41. {
  42. const unsigned long n = path.size();
  43. const unsigned long m = path[].size();
  44. const int dx[] = { , , -, };
  45. const int dy[] = { , -, , };
  46. vector<vector<bool>> flag(n, vector<bool>(m, false));
  47. flag[src.x][src.y] = true;
  48. queue<point> que;
  49. que.push(src);
  50. while (!que.empty())
  51. {
  52. point p = que.front();
  53. for (int i = ; i < ; ++i)
  54. {
  55. if (p.x + dx[i] < || p.x + dx[i] >= n || p.y + dy[i] < || p.y + dy[i] >= m)
  56. continue;
  57. if (path[p.x + dx[i]][p.y + dy[i]] == && !flag[p.x + dx[i]][p.y + dy[i]])
  58. {
  59. flag[p.x + dx[i]][p.y + dy[i]] = true;
  60. que.push(point(p.x + dx[i], p.y + dy[i], p.step + ));
  61. mp[p.x + dx[i]][p.y + dy[i]]= p;
  62. if (point(p.x + dx[i], p.y + dy[i], p.step + ) == des)
  63. {
  64. return p.step + ;
  65. }
  66. }
  67. }
  68. que.pop();
  69. }
  70. return -;
  71. }

输出

  1. 具体路径如下:
  2.  
  3. Program ended with exit code:

参考:
https://www.cnblogs.com/xiugeng/p/9687354.html
https://blog.csdn.net/weixin_41106545/article/details/83211418

c++ 珊格迷宫问题的更多相关文章

  1. c++ 珊格画椭圆

    #ifndef _TEST_H #define _TEST_H #include <iostream> #include <math.h> using namespace st ...

  2. 洛谷P1141 01迷宫

    题目描述 有一个仅由数字0与1组成的n×n格迷宫.若你位于一格0上,那么你可以移动到相邻4格中的某一格1上,同样若你位于一格1上,那么你可以移动到相邻4格中的某一格0上. 你的任务是:对于给定的迷宫, ...

  3. ACM:图BFS,迷宫

    称号: 网络格迷宫n行m单位列格组成,每个单元格无论空间(使用1表示),无论是障碍(使用0为了表示).你的任务是找到一个动作序列最短的从开始到结束,其中UDLR同比分别增长.下一个.左.向右移动到下一 ...

  4. 01迷宫 洛谷 p1141

    题目描述 有一个仅由数字0与1组成的n×n格迷宫.若你位于一格0上,那么你可以移动到相邻4格中的某一格1上,同样若你位于一格1上,那么你可以移动到相邻4格中的某一格0上. 你的任务是:对于给定的迷宫, ...

  5. P1141 01迷宫

    https://www.luogu.org/problemnew/show/P1141 题目描述 有一个仅由数字0与1组成的n×n格迷宫.若你位于一格0上,那么你可以移动到相邻4格中的某一格1上,同样 ...

  6. P1141 01迷宫 dfs连通块

    题目描述 有一个仅由数字000与111组成的n×nn \times nn×n格迷宫.若你位于一格0上,那么你可以移动到相邻444格中的某一格111上,同样若你位于一格1上,那么你可以移动到相邻444格 ...

  7. P1141 01迷宫 DFS (用并查集优化)

    题目描述 有一个仅由数字00与11组成的n \times nn×n格迷宫.若你位于一格0上,那么你可以移动到相邻44格中的某一格11上,同样若你位于一格1上,那么你可以移动到相邻44格中的某一格00上 ...

  8. php生成迷宫和迷宫寻址算法实例

    较之前的终于有所改善.生成迷宫的算法和寻址算法其实是一样.只是一个用了遍历一个用了递归.参考了网上的Mike Gold的算法. <?php //zairwolf z@cot8.com heade ...

  9. 01迷宫 BFS

    题目描述 有一个仅由数字000与111组成的n×nn \times nn×n格迷宫.若你位于一格0上,那么你可以移动到相邻444格中的某一格111上,同样若你位于一格1上,那么你可以移动到相邻444格 ...

随机推荐

  1. 常用算法之排序(Java)

    一.常用算法(Java实现) 1.选择排序(初级算法) 原理:有N个数据则外循环就遍历N次并进行N次交换.内循环实现将外循环当前的索引i元素与索引大于i的所有元素进行比较找到最小元素索引,然后外循环进 ...

  2. Centos7阿里云安装OpenProject-亲测

    10/182019年10月18日13:50 参考 <https://ywnz.com/linuxyffq/4085.html> 说在前头:网上有各种教程,包括官方自己的教程,在阿里云服务器 ...

  3. https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce/repodata/repomd.xml:HTTPS Error 404 - Not Found

    1.按照菜鸟教程,安装docker,竟然报如题错误 2.然后发现,自己再添加软件源信息的时候,自作聪明的把centos换成了自己的主机名 3.那么需要重新来,先删除 cd  /etc/yum.repo ...

  4. 微信小程序 swiper 组件坑

    swiper 组件高度被限制为150px了,所以内容无法撑开. 解决办法 给这组件重新设置个高度,然后在把里面的图片设置为自动适应容器大小.图片模式设置为 宽度不变 自动适应高度 <swiper ...

  5. input 被checked时和label配合的妙用

    input 和label配合的妙用 1:作为文字隐藏与否的开关: 如下代码:对div里面所包含的文字较多,一开始只展示小部分,当用户点击按钮时,进行全部内容的展示(按钮是以向下隐藏箭头的图片) htm ...

  6. MySQL 启动、登录、退出和目录结构

    一.启动 MySQL 服务器启动方式有两种: (1)通过服务的方式自动启动 (2)手动启动的方式 1.windows 服务方式启动 操作步骤: 也可以在 cmd 窗口 输入 services.msc ...

  7. 爬虫之 selenium模块

    selenium模块   阅读目录 一 介绍 二 安装 三 基本使用 四 选择器 五 等待元素被加载 六 元素交互操作 七 其他 八 项目练习 一 介绍 selenium最初是一个自动化测试工具,而爬 ...

  8. Socket的一些疑惑整理

    关于listen的问题请看steven<tcp/ip详解1>18章18.11.4 呼入连接请求队列一节,说的很清楚

  9. Linux 本机/异机文件对比

    一:提取异步机器文件 #ssh 192.168.1.2 "cat /etc/glance/glance-api.conf | grep -v '#' |grep -v ^$" 二: ...

  10. 【Java】debug初级使用(Eclipse)

    1.设置.取消断点 双击要设置断点的代码行数字的前面 2.切换成Debug界面 就会发现画面变成了下图这样 以下是调试的界面解说(图源百度) 3.切换为原界面