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格 ...
随机推荐
- C#基础加强笔记
1面向对象 类:包含字段.属性.函数.构造函数 字段:存储数据 属性:保护字段 get set 函数:描述对象的行为 构造函数:初始化对象,给对象的每个属性赋值 面向对象的好处:让程序具有扩展性 类决 ...
- MacOS中创建Sublime Text3快捷方式返回Operation not permitted的原因及解决
在类Unix系统中我们可以很随心的添加一些程序在终端里快捷方法,比如将一些常用的工具放在/usr/bin下面 Sublime Text3是一个小巧精致而又功能强大的程序,而且本猫也安装了Swift语言 ...
- sql 四舍五入 保留两位小数
一.问题描述 数据库里的 float momey 类型,都会精确到多位小数.但有时候 我们不需要那么精确,例如,只精确到两位有效数字. 二.sqlserver解决方案: 1. 使用 Round() 函 ...
- Django 启动报错 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc7
pycharm 报错 cmd 报错 解决办法 首先 是计算机 编码问题 是 django 读取你的 用户host名 但是 windos 用户名 如果是中文 就会报这个错 要改成 英文
- kubeadm init初始化报错解决,亲测
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull' error ...
- cdh-hbase用户无法执行命令
- http接口测试工具-Advanced-REST-client
非常好用的http接口测试工具 相信作为一个java开发人员,大家或多或少的要写或者接触一些http接口.而当我们需要本地调试接口常常会因为没有一款好用的工具而烦恼.今天要给大家介绍一款非常好用.实用 ...
- windows cmd命令学习
tasklist|findstr "py"
- WPF 反编译后错误处理
1. 首先,手动创建一个WPF工程(WpfApplicationReflectorDemo) 2. 把生成的WpfApplicationReflectorDemo.exe 拖到ILSpy里 3.点击 ...
- 运维CMDB建设思路
在我们日常的运维工作中,面对着大量的基础设施和软件服务,该如何管理?这个管理的原则又是什么?粒度该如何控制?我们是否可以建立一个统一的标准模型来管理以上对象?管理过程中,如何降低人力成本?资源对象的生 ...