迷宫问题(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 ...
随机推荐
- Python——Django框架——django-simple-captcha(验证码)
一.引用 包 pip install django-simple-captcha 二.将captcha加入setting的 INSTALLED_APPS 三.运行python manager.py m ...
- JPG学习笔记1(附完整代码)
#topics h2 { background: rgba(43, 102, 149, 1); border-radius: 6px; box-shadow: 0 0 1px rgba(95, 90, ...
- map最最最基本用法
map<a,b>c中,a,b是变量类型 参数定义的map的名字 #include<stdio.h> #include<map> //头文件 map<int,c ...
- HDU 3681 Prison Break(状压DP + BFS)题解
题意:一张图,F是起点,Y是必须要到的点,D不能走,G可以充电.可以往四个方向走,每走一步花费一个电,走到G可以选择充满电或者不充,每个G只能充一次.问你走遍Y的最小初始点亮.number(G) + ...
- github gist 无法访问
转自这里 以管理员身份在hosts文件: Windows: C:\Windows\System32\drivers\etc Ubuntu: /etc/hosts 添加: 192.30.253.118 ...
- Spring Cloud Alibaba+Nacos搭建微服务架构
1. Spring Cloud Alibaba 简介 Spring Cloud Alibaba是阿里巴巴为分布式应用提供的一站式解决方案,能够更方便快捷地搭建分布式平台,nacos拥有着替换eu ...
- web components in action
web components in action web components css-doodle.js https://alligator.io/workflow/ https://d33wubr ...
- 小程序 web-view
小程序 web-view https://opendocs.alipay.com/mini/component/web-view https://opendocs.alipay.com/mini/ap ...
- react 遍历 object
@observable obj = { name: "ajanuw", age: 22, }; @computed get list() { return _.toPairs(th ...
- layui 时间插件laydate中动态设置改变min和max值
<div class="layui-inline"> <label class="layui-form-label">申请时间</ ...