#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++ 珊格迷宫问题的更多相关文章

  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. jenkins pipline

    def getHost(){ def remote = [:] remote.name = 'server02' remote.host = '39.19.90' remote.user = 'roo ...

  2. Spring循环依赖的三种方式以及解决办法

    一. 什么是循环依赖? 循环依赖其实就是循环引用,也就是两个或者两个以上的bean互相持有对方,最终形成闭环.比如A依赖于B,B依赖于C,C又依赖于A.如下图: 注意,这里不是函数的循环调用,是对象的 ...

  3. vue打包后.woff字体文件路径问题处理

    在执行 npm run build 命令打包后,如果出现 .woff 等字体文件找不到的情况 通过设置 vue-style-loader 打包前缀路径解决

  4. Linux安全:Linux如何防止木马

    (一)解答战略 去企业面试时是有多位竞争者的,因此要注意答题的维度和高度,一定要直接秒杀竞争者,搞定高薪offer. (二)解答战术 因为Linux下的木马常常是恶意者通过Web的上传目录的方式来上传 ...

  5. Android笔记(二十四) Android中的SeekBar(拖动条)

    拖动条和进度条非常相似,只是进度条采用颜色填充来表明进度完成的程度,而拖动条则通过滑块的位置来标识数值——而且拖动条允许用户拖动滑块来改变值,因此拖动条通常用于对系统的某种数值进行调节,比如调节音量等 ...

  6. Linux 命令之 ln

    ln 的作用是制作一个文件或者目录的快捷方式,让我们在使用的过程当中更加方便地使用. 下面我来简单介绍一下 ln 的基本用法. ln 的基本语法 生成一个软链 ln -s source_name li ...

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

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

  8. MySQL进阶11--DDL数据库定义语言--库创建/修改/删除--表的创建/修改/删除/复制

    /*进阶 11 DDL 数据库定义语言 库和表的管理 一:库的管理:创建/修改/删除 二:表的管理:创建/修改/删除 创建: CREATE DATABASE [IF NOT EXISTS] 库名; 修 ...

  9. 【基础搜索】poj-2676-Sudoku(数独)--求补全九宫格的一种合理方案

      数独 时限:2000 MS   内存限制:65536K 提交材料共计: 22682   接受: 10675   特别法官 描述 数独是一个非常简单的任务.一个9行9列的正方形表被分成9个较小的3x ...

  10. 没想到有一天我喜欢上java是因为微软,感谢啊

    一直不喜欢java就是没有好的 ide, eclipse myeclipse  idea  对于习惯visual studio的人  真的太好了 感谢微软 感谢visual studio code