迷宫问题(DFS)
声明:图片及内容基于https://www.bilibili.com/video/BV1oE41177wk?t=3245
问题及分析

8*8的迷宫,最外周是墙,0表示可以走,1表示不可以走
设置迷宫
const int M = 8; //迷宫长(不包含墙)
const int N = 8; //迷宫宽(不包含墙)
设置方位坐标
class Box{ //栈中元素
public:
int x, y;
int di; //方向及Direction数组的下标
Box(int x, int y, int di):x(x),y(y),di(di) {}
Box() {}
};
设置方位偏移量
class Direction{ //x,y方向增量
public:
int incX, incY;
Direction(int x, int y) :incX(x), incY(y) {}
};
寻找路径
bool findPath(int maze[M + 2][N + 2], Direction direct[], stack<Box> &s) {
Box temp(1,1,-1); //temp用来记录当前所在位置
int x, y, di; //迷宫格子当前处理单元的纵横坐标和方向
int line, col; //迷宫数组下一个单元的行坐标和列坐标
maze[1][1] = -1; //走过的格子设为-1
s.push(temp);
while (!s.empty()) {
temp = s.top(); //每次获取栈的头元素
s.pop(); //弹出头元素
x = temp.x; y = temp.y; di = temp.di+1; //上次循环到没法走要换个方向,更新当前位置,下,x,y不变,di+1,换个方向
while (di < 4) {
line = x + direct[di].incX;
col = y + direct[di].incY;
if (maze[line][col] == 0) { //格子为0说明这个格子可以走
Box temp2(x, y, di);
temp = temp2;
s.push(temp); //成功走到下一个格子就压入栈
x = line; y = col; maze[line][col] = -1; //更新当前所在位置
if (x == M && y == N) return true;
else di = 0; //从最新的位置开始尝试四个方向
}
else di++; //maze[line][col]== -1 没法走,换一个方向
}
}
return false; //没有路
}
完整代码
#include<iostream>
#include<stack>
using namespace std;
const int M = 8; //迷宫长(不包含墙)
const int N = 8; //迷宫宽(不包含墙) class Direction{ //x,y方向增量
public:
int incX, incY;
Direction(int x, int y) :incX(x), incY(y) {}
}; class Box{ //栈中元素
public:
int x, y;
int di; //方向及Direction数组的下标
Box(int x, int y, int di):x(x),y(y),di(di) {}
Box() {}
}; bool findPath(int maze[M + 2][N + 2], Direction direct[], stack<Box> &s) {
Box temp(1,1,-1); //temp用来记录当前所在位置
int x, y, di; //迷宫格子当前处理单元的纵横坐标和方向
int line, col; //迷宫数组下一个单元的行坐标和列坐标
maze[1][1] = -1; //走过的格子设为-1
s.push(temp);
while (!s.empty()) {
temp = s.top(); //每次获取栈的头元素
s.pop(); //弹出头元素
x = temp.x; y = temp.y; di = temp.di+1;
while (di < 4) {
line = x + direct[di].incX;
col = y + direct[di].incY;
if (maze[line][col] == 0) { //格子为0说明这个格子可以走
Box temp2(x, y, di);
temp = temp2;
s.push(temp); //成功走到下一个格子就压入栈
x = line; y = col; maze[line][col] = -1;
if (x == M && y == N) return true;
else di = 0; //从最新的位置开始尝试四个方向
}
else di++; //换一个方向
}
}
return false; //没有路
} int main() {
stack<Box> s;
Direction direct[4] = { {0,1},{1,0},{0,-1},{-1,0} };
int maze[M + 2][N + 2];
for (int i = 0; i < M + 2; i++) {
for (int j = 0; j < N + 2; j++) {
cin >> maze[i][j];
}
}
int maze2[M+2][N+2]; //用maze2拷贝maze用于最后打印
for (int i = 0; i < M + 2; i++) {
for (int j = 0; j < N + 2; j++) {
maze2[i][j]= maze[i][j];
}
}
if (findPath(maze, direct, s)) {
while (!s.empty()) {
cout << "(" << s.top().x << "," << s.top().y << ")" << endl;
maze2[s.top().x][s.top().y] = s.top().di + 2; //类似哈希散列,做特殊标记。墙是0~1,箭头是2~5
s.pop(); //为了实现可视化
}
for (int i = 0; i < M + 2; i++) { //路径可视化
for (int j = 0; j < N + 2; j++) {
switch (maze2[i][j]) {
case 0:cout << "0 "; break;
case 1:cout << "1 "; break;
case 2:cout << "→"; break;
case 3:cout << "↓"; break;
case 4:cout << "←"; break;
case 5:cout << "↑"; break;
default:break;
}
//cout << maze2[i][j]<<" ";
}
cout << endl;
}
}
else cout << "No path"<<endl;
return 0;
}
输入:
1 1 1 1 1 1 1 1 1 1
1 0 0 1 0 0 0 1 0 1
1 0 0 1 0 0 0 1 0 1
1 0 0 0 0 1 1 0 0 1
1 0 1 1 1 0 0 0 0 1
1 0 0 0 1 0 0 0 0 1
1 0 1 0 0 0 1 0 0 1
1 0 1 1 1 0 1 1 0 1
1 1 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1
输出:
(8,7)
(8,6)
(8,5)
(7,5)
(6,5)
(6,4)
(6,3)
(5,3)
(5,2)
(5,1)
(4,1)
(3,1)
(3,2)
(2,2)
(1,2)
(1,1)
1 1 1 1 1 1 1 1 1 1
1 →↓1 0 0 0 1 0 1
1 0 ↓1 0 0 0 1 0 1
1 ↓←0 0 1 1 0 0 1
1 ↓1 1 1 0 0 0 0 1
1 →→↓1 0 0 0 0 1
1 0 1 →→↓1 0 0 1
1 0 1 1 1 ↓1 1 0 1
1 1 0 0 0 →→→0 1
1 1 1 1 1 1 1 1 1 1
迷宫问题(DFS)的更多相关文章
- 2021.08.16 P1363 幻象迷宫(dfs,我感受到了出题人浓浓的恶意)
2021.08.16 P1363 幻象迷宫(dfs,我感受到了出题人浓浓的恶意) P1363 幻象迷宫 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 题意: 幻象迷宫可以认为是无限 ...
- hdu 1728:逃离迷宫(DFS,剪枝)
逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- HDU 1269 迷宫城堡(DFS)
迷宫城堡 Problem Description 为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000),每个通道都是单向的 ...
- HDU 1728 逃离迷宫(DFS)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1728 题目: 逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) ...
- 洛谷P1605 迷宫 (DFS)
题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫 中移动有上下 ...
- 洛谷P1605:迷宫(DFS)
题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫中移动有上下左右 ...
- 迷宫问题 dfs bfs 搜索
定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, ...
- 洛谷P1605 迷宫【dfs】
题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫 中移动有上下 ...
- SDUT 1269-走迷宫(DFS打印路径)
走迷宫 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描写叙述 有一个m*n格的迷宫(表示有m行.n列),当中有可走的也有不可走的,假 ...
- poj3984迷宫问题(dfs+stack)
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35426 Accepted: 20088 Descriptio ...
随机推荐
- Gome 高性能撮合引擎微服务
Gome 高性能撮合引擎微服务 使用 Golang 做计算,gRPC 做服务,ProtoBuf 做数据交换,RabbitMQ 做队列,Redis 做缓存实现的高性能撮合引擎微服务 依赖 具体依赖信息可 ...
- docker理论题-02
1.什么是namespace? 答:名称空间,作用隔离容器 2.namespace隔离有那些? 答:ipc:共享内存.消息队列 mnt:挂载点 net:网络栈 uts:域,主机名 user:用户,组 ...
- ReactDOM API All In One
ReactDOM API All In One React DOM API render() hydrate() unmountComponentAtNode() findDOMNode() crea ...
- Redis in Action : Redis 实战学习笔记
1 1 1 Redis in Action : Redis 实战学习笔记 1 http://redis.io/ https://github.com/antirez/redis https://ww ...
- Angular 2 for 2017 web full stack development
1 1 1 Angular 2 for 2017 web full stack development 1 1 https://angular2.xgqfrms.xyz/ https://ng2-he ...
- color recognition by image
color recognition by image 通过图像进行颜色识别 https://imagecolorpicker.com/ unknown color origin pic grey bl ...
- C++ 中的智能指针-基础
简介 在现代 C++ 编程中,标准库包含了智能指针(Smart pointers). 智能指针用来确保程序不会出现内存和资源的泄漏,并且是"异常安全"(exception-safe ...
- mybatisPlus中的模糊查询问题
不能查询中文 记得在数据库的配置中写明编码格式characterEncoding=utf-8
- EasyExcel学习
导入依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</a ...
- sql注入和union all关联查询的学习总结
1.后台从页面取值进行sql查询时最好不要直接拼,如下代码: String sql = "SELECT wo.* " + " from push_command pu & ...