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格 ...
随机推荐
- 【SQL Server高可用性】数据库复制:SQL Server 2008R2中通过数据库复制,把A表的数据复制到B表
原文:[SQL Server高可用性]数据库复制:SQL Server 2008R2中通过数据库复制,把A表的数据复制到B表 经常在论坛中看到有人问数据同步的技术,如果只是同步少量的表,那么可以考虑使 ...
- 【转载】 C#使用Union方法求两个List集合的并集数据
在C#语言的编程开发中,有时候需要对List集合数据进行运算,如对两个List集合进行交集运算或者并集运算,其中针对2个List集合的并集运算,可以使用Union方法来快速实现,Union方法的调用格 ...
- java 里执行javascript代码
import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; ScriptEngineManager sem = ...
- Linux/Aix日常报错整理
[root@localhost ~]# umount /mnt umount.nfs: /mnt: device is busy umount.nfs: /mnt: device is busy 问题 ...
- 再见 Docker,是时候拥抱下一代容器工具了
本文首发于:微信公众号「运维之美」,公众号 ID:Hi-Linux. 「运维之美」是一个有情怀.有态度,专注于 Linux 运维相关技术文章分享的公众号.公众号致力于为广大运维工作者分享各类技术文章和 ...
- YUSS Round 1
YUSS Round 1 A. 国庆快乐 签到题. #include<bits/stdc++.h> using namespace std; int main() { printf(&qu ...
- MaxTenuringThreshold与阈值的动态调整理论详解
今天会学习“MaxTenuringThreshold”这样一个新的JVM参数,编写的示例还是会基于上一次的代码,新建个类,如下: 接下来给它设置JVM的参数,具体如下: 而接下来会新增三个参数: 这个 ...
- 数据库中聚合索引(MySQL和SQL Server区别)
一.聚集索引和非聚集索引 聚集索引:类似字典的拼音目录.表中的数据按照聚集索引的规则来存储的.就像新华字典.整本字典是按照A-Z的顺序来排列.这也是一个表只能有一个聚集索引的原因.因为这个特点,具体索 ...
- Linq 中 Join 的用法
Linq中连接主要有组连接.内连接.左外连接.交叉连接四种.各个用法如下. 注:本文内容主要来自<Linq实战>,本例中用到的对象请见文章底部. 1. 组连接 组连接是与分组查询是一样的. ...
- JSOI2009 密码 和 JSOI2007 文本生成器 和 ZOJ3545 Rescue the Rabbit
密码 众所周知,密码在信息领域起到了不可估量的作用.对于普通的登陆口令,唯一的破解 方法就是暴力破解一逐个尝试所有可能的字母组合,但这是一项很耗时又容易被发现的工 作.所以,为了获取对方的登陆口令,在 ...