声明:图片及内容基于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. 004、Python xlsxwriter模块

    简单用法demo # !/usr/bin/python # coding:utf-8 # xlsxwriter的基本用法 import xlsxwriter # 1. 创建一个Excel文件 work ...

  2. 请问什么时候对象分配会不在 TLAB 内分配

    Java 对象分配流程 我们这里不考虑栈上分配,这些会在 JIT 的章节详细分析,我们这里考虑的是无法栈上分配需要共享的对象. 对于 HotSpot JVM 实现,所有的 GC 算法的实现都是一种对于 ...

  3. [Golang]-7 定时器和打点器

    目录 定时器 打点器 After()方法 我们常常需要在未来某个时刻运行 Go 代码,或者在某段时间间隔内重复运行. Go 的内置 定时器 和 打点器 特性让这些很容易实现. 定时器 type Tim ...

  4. Redis性能指标监控

    监控指标 •性能指标:Performance•内存指标: Memory•基本活动指标:Basic activity•持久性指标: Persistence•错误指标:Error 性能指标:Perform ...

  5. C语言中函数的调用方式

    第一眼看到这样一个题目的我,心想除了如下的直接调用还能怎么调用呢? 1 void fun(void) 2 { 3 ...... 4 //你的代码 5 ..... 6 } 7 int main(void ...

  6. Monorepo All In One

    Monorepo All In One monorepos 只是一种思想,或设计模式,架构风格 https://trunkbaseddevelopment.com/monorepos/ Lerna h ...

  7. Internationalization API & ECMA-402

    Internationalization API & ECMA-402 i18n https://caniuse.com/?search=Internationalization API In ...

  8. d3 & hexagon

    d3 & hexagon https://bl.ocks.org/mbostock/5249328 https://bl.ocks.org/vasturiano/e70e14483fe01eb ...

  9. TypeScript & global.d.ts

    TypeScript & global.d.ts https://www.typescriptlang.org/docs/handbook/declaration-files/template ...

  10. WiFi 6 & 5G

    WiFi 6 & 5G https://zhuanlan.zhihu.com/p/85509996 https://www.bilibili.com/read/cv3206576/ https ...