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格 ...
随机推荐
- MongoDB安装及环境配置
一.什么是MongoDB MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统. 在高负载的情况下,添加更多的节点,可以保证服务器性能. MongoDB 旨在为WEB应用提供 ...
- 简单web性能测试工具——ab命令(ApacheBench)
ab命令(ApacheBench) ----------转载内容 ApacheBench(即ab)通常用来做网站性能压力测试,是性能调优过程中必不可少的一环,ab命令会创建很多的并发访问线程,模拟多个 ...
- 用D3js的区域生成器实现简单波浪图
最近做控件遇到含有波浪图的图表,一开始用Echarts虽然很快完成了,但Echarts的波浪图与其他图表的响应式不同步,于是学习了D3js,D3js写起来确实复杂一些,但能够实现的效果也更丰富,做的时 ...
- nodejs之express的中间件
express中间件分成三种 内置中间件 static 自定义中间件 第三方中间件 (body-parser) (拦截器) 全局自定义中间件 在请求接口时 有几个接口都要验证传来的内容是否存在或者是否 ...
- eval函数和isNaN函数
(一)eval函数定义:eval() 函数可计算某个字符串,并执行其中的的 JavaScript 代码. (二)语法:eval(string)string必需. (三)返回值:通过计算 string ...
- javascript原型原型链 学习随笔
理解原型和原型链.需从构造函数.__proto__属性(IE11以下这个属性是undefined,请使用chrome调试).prototype属性入手. JS内置的好多函数,这些函数又被叫做构造函数. ...
- 易优cms后台RCE以及任意文件上传漏洞
前言 EyouCms是基于TP5.0框架为核心开发的免费+开源的企业内容管理系统,专注企业建站用户需求提供海量各行业模板,降低中小企业网站建设.网络营销成本,致力于打造用户舒适的建站体验.易优cms ...
- 部署vue项目到阿里云服务器(Ubuntu16.04 64位)
上传文件 1.通过Xftp将vue项目文件上传至云服务器:由于node_modules这个依赖包体积较大,上传较慢,上传时跳过,在云服务器上重新进行npm install安装依赖包即可: 2.也可通过 ...
- Vue指令之`v-bind`的三种用法及v-on事件指令
v-bind:是 Vue中,提供的用于绑定属性的指令 1. 直接使用指令`v-bind` 2. 使用简化指令`:` 3. 在绑定的时候,拼接绑定内容:`:title="btnTitle + ...
- javascript_05-操作符
一元运算符 a++和++a //5 2 3 var a =1; var b = ++a + ++a; console.log(b) //4 1 3 var a =1; var b = a++ + ++ ...