声明:图片及内容基于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)的更多相关文章

  1. 2021.08.16 P1363 幻象迷宫(dfs,我感受到了出题人浓浓的恶意)

    2021.08.16 P1363 幻象迷宫(dfs,我感受到了出题人浓浓的恶意) P1363 幻象迷宫 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 题意: 幻象迷宫可以认为是无限 ...

  2. hdu 1728:逃离迷宫(DFS,剪枝)

    逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  3. HDU 1269 迷宫城堡(DFS)

    迷宫城堡 Problem Description 为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000),每个通道都是单向的 ...

  4. HDU 1728 逃离迷宫(DFS)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1728 题目: 逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)  ...

  5. 洛谷P1605 迷宫 (DFS)

    题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫 中移动有上下 ...

  6. 洛谷P1605:迷宫(DFS)

    题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫中移动有上下左右 ...

  7. 迷宫问题 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, ...

  8. 洛谷P1605 迷宫【dfs】

    题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫 中移动有上下 ...

  9. SDUT 1269-走迷宫(DFS打印路径)

    走迷宫 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述 有一个m*n格的迷宫(表示有m行.n列),当中有可走的也有不可走的,假 ...

  10. poj3984迷宫问题(dfs+stack)

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35426   Accepted: 20088 Descriptio ...

随机推荐

  1. CF1474-A. Puzzle From the Future

    CF1474-A. Puzzle From the Future 题意: 有两个由\(0,1\)组成的.长度相等字符串\(a, b\),两个字符串按位相加得到一个新的字符串\(s\),对\(s\)取\ ...

  2. 流媒体传输协议之 RTP(下篇)

    本系列文章将整理各个流媒体传输协议,包括 RTP/RTCP,RTMP,希望通过深入梳理协议的设计细节,能够给流媒体领域的开发者带来一定的启发. 作者:逸殊 审核:泰一 接上篇:< 流媒体传输协议 ...

  3. k8s二进制部署 - dashboard安装

    配置资源清单rbac.yaml apiVersion: v1 kind: ServiceAccount metadata: labels: k8s-app: kubernetes-dashboard ...

  4. vue中获取元素并控制相应的dom

    1 在标签中使用ref="xxx" 2 在methods中调用this.$refs.xxx this.$refs.xxx.$el获取dom 注意1:大多数情况下为了复用方法,将xx ...

  5. 6. Connection has already been closed 数据库连接被关闭

    生产上Tomcat出现 Connection has already been closed.问题,但是在uat测试是好的! 遇见两次: 1.某个程序dao中执行逻辑异常复杂,有时候需要执行一分多钟, ...

  6. mybatis(六)插件机制及分页插件原理

    转载:https://www.cnblogs.com/wuzhenzhao/p/11120848.html MyBatis 通过提供插件机制,让我们可以根据自己的需要去增强MyBatis 的功能.需要 ...

  7. 翻译:《实用的Python编程》01_04_Strings

    目录 | 上一节 (1.3 数字) | 下一节 (1.5 列表) 1.4 字符串 本节介绍处理文本的方法. 表示字面量文本 在程序中字符串字面量使用引号来书写. # 单引号(Single quote) ...

  8. vue slot nested bug

    vue slot nested bug slot name bug Error <slot name="global-system-guide-slot"></s ...

  9. Google PageSpeed Insights : 网站性能优化检测工具

    1 1 https://developers.google.com/speed/pagespeed/insights/ PageSpeed Insights 使您的网页在所有设备上都能快速加载. 分析 ...

  10. Hive底层原理:explain执行计划详解

    不懂hive中的explain,说明hive还没入门,学会explain,能够给我们工作中使用hive带来极大的便利! 理论 本节将介绍 explain 的用法及参数介绍 HIVE提供了EXPLAIN ...