#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. gulp删除目标文件中所有的console.log()语句——gulp-strip-debug

    1.安装npm包 npm install --save-dev gulp-strip-debug 2.使用 const gulp = require('gulp'); const stripDebug ...

  2. 在论坛中出现的比较难的sql问题:46(日期条件出现的奇怪问题)

    原文:在论坛中出现的比较难的sql问题:46(日期条件出现的奇怪问题) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉得有 ...

  3. 文件流FileStream的读写

    1.FileStream文件流的概念: FileStream 类对文件系统上的文件进行读取.写入.打开和关闭操作,并对其他与文件相关的操作系统句柄进行操作,如管道.标准输入和标准输出.读写操作可以指定 ...

  4. @app.route源码流程分析

    @app.route(), 是调用了flask.app.py文件里面的Flask类的route方法,route方法所做的事情和add_url_rule类似,是用来为一个URL注册一个视图函数,但是我们 ...

  5. Android简单闹钟设置

    利用AlarmManager实现闹钟设置 //设置本地闹钟,actiongString:闹钟标识 setLocAlarm(int week, String actionString) { Calend ...

  6. 巧用XML格式数据传入存储过程转成表数据格式

    1.首先将后台数据转成对应的XML数据格式 /// <summary> /// 集合转XML数据格式 /// </summary> /// <param name=&qu ...

  7. 前端框架开始学习Vue(三)

    初步安装.与搭建    https://www.cnblogs.com/yanxulan/p/8978732.html ----如何搭建一个vue项目 安装 nodejs,,, npm i == np ...

  8. C#-DBHelper

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  9. Flink系列之流式

    本文仅是自己看书.学习过程中的个人总结,刚接触流式,视野面比较窄,不喜勿喷,欢迎评论交流. 1.为什么是流式? 为什么是流式而不是流式系统这样的词语?流式系统在我的印象中是相对批处理系统而言的,用来处 ...

  10. C++——多态性 与 虚函数

    多态性 多态性是面向对象程序设计的关键技术之一.若程序设计语言不支持多态性,不能称为面向对象的语言.利用多态性技术,可以调用同一个函数名的函数,实现完全不同的功能. 多态性(polymorphism) ...