c++ 珊格迷宫问题
- #demo1
- #include<iostream>
- #include<ctime>
- #include<cstdlib>
- #include<queue>
- #include<cstdio>
- using namespace std;
- //生成迷宫
- const int HEIGHT = ;
- const int WIDTH = ;
- bool isFound = false;
- int maze[HEIGHT][WIDTH];
- void initialMaze()
- {
- maze[][] = ;//入口
- maze[HEIGHT - ][WIDTH - ] = ;//出口
- for (int i = ; i < HEIGHT; i++)//用随机数0,1填充迷宫
- {
- for (int j = ; j < WIDTH; j++)
- {
- if (i == && j == )
- continue;
- if (i == HEIGHT - && j == WIDTH - )
- continue;
- maze[i][j] = rand() % ;
- }
- }
- //展示生成的迷宫
- for (int i = ; i < HEIGHT; i++)
- {
- for (int j = ; j < WIDTH; j++)
- {
- cout << maze[i][j];
- if (j != WIDTH - )
- {
- cout << " ";
- }
- else
- {
- cout << endl;
- }
- }
- }
- }
- //生成方向
- int directory[][] = { {,},{,},{,},{,-},{,-},{-,-},{-,},{-,} };
- //判断是否越界
- bool isLeap(int x, int y)
- {
- return x >= && x < WIDTH&&y >= && y < HEIGHT;
- }
- //任意位置的结构体
- struct point {
- int x;
- int y;
- };
- //声明用于存储路径的结构体
- struct dir
- {
- int x;
- int y;
- int d;
- };
- //声明用于存储路径的队列
- queue<dir> directoryQueue;
- //迷宫循迹
- dir path[HEIGHT][WIDTH];//记录迷宫的路径
- int output[HEIGHT*WIDTH][];
- void mazeTravel(point start, point end, int maze[HEIGHT][WIDTH], int directory[][])
- {
- dir element;
- //dir tmp;
- int i;
- int j;
- int d;
- int a;
- int b;
- element.x = start.x;
- element.y = start.y;
- element.d = -;
- maze[start.x][start.y] = ;
- directoryQueue.push(element);
- while (!directoryQueue.empty())
- {
- element = directoryQueue.front();
- dir m = element;
- directoryQueue.pop();
- i = element.x;
- j = element.y;
- d = element.d + ;
- while (d < )
- {
- a = i + directory[d][];
- b = j + directory[d][];
- if (a == end.x&&b == end.y&&maze[a][b] == )
- {
- //储存前一个点的信息至path
- dir temp = m;
- temp.d = d;
- path[a][b] = temp;
- isFound = true;
- return;
- }
- if (isLeap(a, b)&&maze[a][b]==)
- {
- //储存前一个点的信息至path
- dir temp = m;
- temp.d = d;
- path[a][b] = temp;
- maze[a][b] = ;
- element.x = a;
- element.y = b;
- element.d = -;
- directoryQueue.push(element);
- }
- d++;
- }
- }
- }
- void printPath(point start, point end)
- {
- if (!isFound)
- printf("The path is not found");
- else
- {
- int step = ;
- dir q;
- q.x = end.x;
- q.y = end.y;
- q.d = ;
- while (q.x != start.x || q.y != start.y)
- {
- output[step][] = q.x;
- output[step][] = q.y;
- output[step][] = q.d;
- int x = q.x;
- int y = q.y;
- q.x = path[q.x][q.y].x;
- q.y = path[x][q.y].y;
- q.d = path[x][y].d;
- step++;
- }
- output[step][] = q.x;
- output[step][] = q.y;
- output[step][] = q.d;
- printf("The path is as follows: \n");
- for (int i = step; i >= ; i--)
- {
- printf("(%d,%d)", output[i][], output[i][]);
- if (i != )
- printf("->");
- }
- printf("\n");
- }
- }
- int main()
- {
- srand(time());
- initialMaze();
- point a, b;
- a.x = ;
- a.y = ;
- b.x = HEIGHT - ;
- b.y = WIDTH - ;
- mazeTravel(a, b, maze, directory);
- printPath(a, b);
- return ;
- }
输出
- The path is as follows:
- (,)->(,)->(,)->(,)->(,)->(,)->(,)->(,)->(,)->(,)->(,)
- Program ended with exit code:
demo2
- #demo2
- #include <iostream>
- #include <vector>
- #include <queue>
- using namespace std;
- using std::vector;
- struct point
- {
- int x;
- int y;
- int step;
- point(int _x, int _y, int _step) :x(_x), y(_y), step(_step) {}
- point(int _x, int _y) :x(_x), y(_y), step(){}
- point(){}
- bool operator==(const point& other)const
- {
- return x == other.x&&y == other.y;
- }
- };
- int minSteps_BFS(const vector<vector<int>>& path, vector<vector<point>>& mp, point src, point des, int step);
- int main()
- {
- vector<vector<int>> path = { { , , , , }, { , , , , }, { , , , , }, { , , , , }, { , , , , } };
- vector<vector<point>> mp(, vector<point>());
- point src(,);
- point des(,);
- int step = ;
- cout << minSteps_BFS(path, mp, src, des, step) << endl;
- cout << "具体路径如下:" << endl;
- //vector<point> res;
- while (!(mp[des.x][des.y] == src))
- {
- cout << des.x << " " << des.y << endl;
- des = mp[des.x][des.y];
- }
- cout << des.x << " " << des.y << endl;
- return ;
- }
- int minSteps_BFS(const vector<vector<int>>& path, vector<vector<point>>& mp, point src, point des, int step)
- {
- const unsigned long n = path.size();
- const unsigned long m = path[].size();
- const int dx[] = { , , -, };
- const int dy[] = { , -, , };
- vector<vector<bool>> flag(n, vector<bool>(m, false));
- flag[src.x][src.y] = true;
- queue<point> que;
- que.push(src);
- while (!que.empty())
- {
- point p = que.front();
- for (int i = ; i < ; ++i)
- {
- if (p.x + dx[i] < || p.x + dx[i] >= n || p.y + dy[i] < || p.y + dy[i] >= m)
- continue;
- if (path[p.x + dx[i]][p.y + dy[i]] == && !flag[p.x + dx[i]][p.y + dy[i]])
- {
- flag[p.x + dx[i]][p.y + dy[i]] = true;
- que.push(point(p.x + dx[i], p.y + dy[i], p.step + ));
- mp[p.x + dx[i]][p.y + dy[i]]= p;
- if (point(p.x + dx[i], p.y + dy[i], p.step + ) == des)
- {
- return p.step + ;
- }
- }
- }
- que.pop();
- }
- return -;
- }
输出
- 具体路径如下:
- Program ended with exit code:
参考:
https://www.cnblogs.com/xiugeng/p/9687354.html
https://blog.csdn.net/weixin_41106545/article/details/83211418
c++ 珊格迷宫问题的更多相关文章
- c++ 珊格画椭圆
#ifndef _TEST_H #define _TEST_H #include <iostream> #include <math.h> using namespace st ...
- 洛谷P1141 01迷宫
题目描述 有一个仅由数字0与1组成的n×n格迷宫.若你位于一格0上,那么你可以移动到相邻4格中的某一格1上,同样若你位于一格1上,那么你可以移动到相邻4格中的某一格0上. 你的任务是:对于给定的迷宫, ...
- ACM:图BFS,迷宫
称号: 网络格迷宫n行m单位列格组成,每个单元格无论空间(使用1表示),无论是障碍(使用0为了表示).你的任务是找到一个动作序列最短的从开始到结束,其中UDLR同比分别增长.下一个.左.向右移动到下一 ...
- 01迷宫 洛谷 p1141
题目描述 有一个仅由数字0与1组成的n×n格迷宫.若你位于一格0上,那么你可以移动到相邻4格中的某一格1上,同样若你位于一格1上,那么你可以移动到相邻4格中的某一格0上. 你的任务是:对于给定的迷宫, ...
- P1141 01迷宫
https://www.luogu.org/problemnew/show/P1141 题目描述 有一个仅由数字0与1组成的n×n格迷宫.若你位于一格0上,那么你可以移动到相邻4格中的某一格1上,同样 ...
- P1141 01迷宫 dfs连通块
题目描述 有一个仅由数字000与111组成的n×nn \times nn×n格迷宫.若你位于一格0上,那么你可以移动到相邻444格中的某一格111上,同样若你位于一格1上,那么你可以移动到相邻444格 ...
- P1141 01迷宫 DFS (用并查集优化)
题目描述 有一个仅由数字00与11组成的n \times nn×n格迷宫.若你位于一格0上,那么你可以移动到相邻44格中的某一格11上,同样若你位于一格1上,那么你可以移动到相邻44格中的某一格00上 ...
- php生成迷宫和迷宫寻址算法实例
较之前的终于有所改善.生成迷宫的算法和寻址算法其实是一样.只是一个用了遍历一个用了递归.参考了网上的Mike Gold的算法. <?php //zairwolf z@cot8.com heade ...
- 01迷宫 BFS
题目描述 有一个仅由数字000与111组成的n×nn \times nn×n格迷宫.若你位于一格0上,那么你可以移动到相邻444格中的某一格111上,同样若你位于一格1上,那么你可以移动到相邻444格 ...
随机推荐
- 常用算法之排序(Java)
一.常用算法(Java实现) 1.选择排序(初级算法) 原理:有N个数据则外循环就遍历N次并进行N次交换.内循环实现将外循环当前的索引i元素与索引大于i的所有元素进行比较找到最小元素索引,然后外循环进 ...
- Centos7阿里云安装OpenProject-亲测
10/182019年10月18日13:50 参考 <https://ywnz.com/linuxyffq/4085.html> 说在前头:网上有各种教程,包括官方自己的教程,在阿里云服务器 ...
- 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 ...
- 微信小程序 swiper 组件坑
swiper 组件高度被限制为150px了,所以内容无法撑开. 解决办法 给这组件重新设置个高度,然后在把里面的图片设置为自动适应容器大小.图片模式设置为 宽度不变 自动适应高度 <swiper ...
- input 被checked时和label配合的妙用
input 和label配合的妙用 1:作为文字隐藏与否的开关: 如下代码:对div里面所包含的文字较多,一开始只展示小部分,当用户点击按钮时,进行全部内容的展示(按钮是以向下隐藏箭头的图片) htm ...
- MySQL 启动、登录、退出和目录结构
一.启动 MySQL 服务器启动方式有两种: (1)通过服务的方式自动启动 (2)手动启动的方式 1.windows 服务方式启动 操作步骤: 也可以在 cmd 窗口 输入 services.msc ...
- 爬虫之 selenium模块
selenium模块 阅读目录 一 介绍 二 安装 三 基本使用 四 选择器 五 等待元素被加载 六 元素交互操作 七 其他 八 项目练习 一 介绍 selenium最初是一个自动化测试工具,而爬 ...
- Socket的一些疑惑整理
关于listen的问题请看steven<tcp/ip详解1>18章18.11.4 呼入连接请求队列一节,说的很清楚
- Linux 本机/异机文件对比
一:提取异步机器文件 #ssh 192.168.1.2 "cat /etc/glance/glance-api.conf | grep -v '#' |grep -v ^$" 二: ...
- 【Java】debug初级使用(Eclipse)
1.设置.取消断点 双击要设置断点的代码行数字的前面 2.切换成Debug界面 就会发现画面变成了下图这样 以下是调试的界面解说(图源百度) 3.切换为原界面