迷宫问题 Maze 4X4 (sloved by backtrack)
Description

给定一个N*N的迷宫中,(0,0)为起始点,(N-1,N-1)为目的地,求可通往目的地的多个解
思路
这道题其实就是简单的DFS,一路探索到底,没路就回溯到交叉口。
#include <iostream>
#include <vector>
#include <cstring>
using namespace std; typedef struct
{
int x;
int y;
}pos; #define N 4 //4*4 maze
#define ENTER_X 0
#define ENTER_Y 0
#define EXIT_X N-1
#define EXIT_Y N-1 int Maze[N][N];
int paths; //sum of paths
vector<pos> Path; //svae paths
vector<pos> BestPath; //save min distant path void InitMaze();
bool MazeTrack(int x,int y); int main()
{
InitMaze();
int i = , j = ;
for(i=;i<N;i++)
{ for(j=;j<N;j++)
cout << Maze[i][j];
cout << endl;
}
MazeTrack(ENTER_X,ENTER_Y);
return ;
} void InitMaze()
{
int a[N][N] = {
{,,,},
{,,,},
{,,,},
{,,,}
};
memcpy(Maze,a,sizeof(a));
paths = ;
} bool MazeTrack(int x,int y)
{
bool IsPath = false;
pos p;
p.x = x;
p.y = y;
//set range of maze's x/y
if(x<N && x>= && y<N && y>= && Maze[x][y]==)
{
//make
Path.push_back(p);
Maze[x][y] = ; //if value is 1,you can't go there cout << "now,position is("<<x<<","<<y<<")" << endl;
//terminal
if(x==EXIT_X && y==EXIT_Y)
{
cout << "find a path" << endl;
paths++;
IsPath = true;
vector<pos>::iterator it;
for(it=Path.begin() ; it!=Path.end() ; it++)
cout << "("<< it->x <<","<< it->y <<")" << " ";
cout << endl;
return IsPath;
}
//search (find forward solutions)
IsPath = MazeTrack(x-,y) || MazeTrack(x,y-) || MazeTrack(x+,y) || MazeTrack(x,y+);
//backtrack
if(!IsPath)
{
Path.pop_back();
Maze[x][y] = ;
}
cout << Path.size() << endl;
}
//fuction exit
return IsPath;
}
输出的解:
now,position is(,)
now,position is(,)
now,position is(,)
now,position is(,)
now,position is(,) now,position is(,)
now,position is(,)
now,position is(,)
now,position is(,)
now,position is(,)
find a path
(,) (,) (,) (,) (,) (,) (,)
solution
我是第一次用回溯法,参考了下面的用回溯法解迷宫问题的模板:
using namespace std;
#define N 100
#define M 100 typedef struct
{
int x;
int y;
}Point; vector<Point>solutionPath ; //存放有解的坐标路径 //主函数 x,y默认为起始结点,如(0,0), 得到从起始结点到目的结点的路径。
bool hasSolutionFunction( template<typename T>* Matrix , int x, int y)
{ bool *visited = new bool[M*N];
memset (visited,,M*N); //visited 存放每个结点是否被遍历,true为已经遍历过,false为否 res = hasSolutionCore(Matrix , x ,y)
cout<<solutionPath<<endl;
return res } //深度遍历求解路径的函数
bool hasSolutionCore(template<typename T>* Matrix , int x, int y)
{ hasSolution = false;
Point p = Point(x,y); if (x,y) is terminal //x,y 已经是终止条件,当求解到这个结点是叶结点或目的地
{
solutionPath.push_back(p);
return true;
} if ((x,y) && visited[x][y]==flase )// x,y是合法结点(具体条件可扩展),且未被访问过
{
visited[x][y] = true;
solutionPath.push_back(p); hasSolution = hasSolutionCore(Matrix,x-, y) ||hasSolutionCore(Matrix,x,y-)||... //如果不是叶结点,则该路径是否有解取决于其他方向的往后求解。 // x,y结点以下的所有子路径都无解,则回溯
if (!hasSolution)
{
visited[x][y] = false;
solutionPath.pop_back();
} }
return hasSolution; }
迷宫问题 Maze 4X4 (sloved by backtrack)的更多相关文章
- 洛谷——P1825 [USACO11OPEN]玉米田迷宫Corn Maze
P1825 [USACO11OPEN]玉米田迷宫Corn Maze 题目描述 This past fall, Farmer John took the cows to visit a corn maz ...
- 洛谷 P1825 [USACO11OPEN]玉米田迷宫Corn Maze
P1825 [USACO11OPEN]玉米田迷宫Corn Maze 题目描述 This past fall, Farmer John took the cows to visit a corn maz ...
- P1825 [USACO11OPEN]玉米田迷宫Corn Maze
题目描述 This past fall, Farmer John took the cows to visit a corn maze. But this wasn't just any corn m ...
- 洛谷—— P1825 [USACO11OPEN]玉米田迷宫Corn Maze
https://www.luogu.org/problem/show?pid=1825 题目描述 This past fall, Farmer John took the cows to visit ...
- [USACO11OPEN]玉米田迷宫Corn Maze
题目描述 This past fall, Farmer John took the cows to visit a corn maze. But this wasn't just any corn m ...
- 【luogu P1825 [USACO11OPEN]玉米田迷宫Corn Maze】 题解
题目链接:https://www.luogu.org/problemnew/show/P1825 带有传送门的迷宫问题 #include <cstdio> #include <cst ...
- 洛谷 P1825 【[USACO11OPEN]玉米田迷宫Corn Maze】
P1825 传送门 简单的题意 就是一个有传送门的迷宫问题(我一开始以为是只有1个传送门,然后我就凉了). 大体思路 先把传送门先存起来,然后跑一下\(BFS\). 然后,就做完了. 代码鸭 #inc ...
- Maze迷宫问题(求最优解)
迷宫地形我们可以通过读文件的形式,通过已知入口逐个遍历坐标寻找通路. 文件如图: 每个坐标的位置用结构体来记录: struct Pos //位置坐标 { int _row; int _col; }; ...
- 深度优先搜索(DFS),逃离迷宫
[原创] 今天来说说深度优先搜索,深度优先是图论中的内容,大意是从某一点出发,沿着一个方向搜索下去,并伴随着有回退的特点,通常用来判断某一解是否存在,不用来寻找最优解:这里来看一个非常有意思的题目: ...
随机推荐
- mac 安装protobuf,并编译
因公司接口协议是PB文件,需要将 PB 编译成JAVA文件,且MAC 电脑,故整理并分享MAC安装 google 下的protobuf 文件 MAC 安装protobuf 流程 1.下载 http ...
- 33.Linux-实现U盘自动挂载(详解)
1.当我们每次插入u盘后,都会自动创键U盘的设备节点/dev/sda%d 这是因为里面调用了device_create()实现的, busybox的mdev机制就会根据主次设备号等信息,在/dev下创 ...
- Vuex非常适合新手的教程,保教不会!
本讲解基于Vue-cli(脚手架)搭建的项目. Vuex 数据状态管理工具,整个流程类似依赖注入,相当于预先定义,倒推.(个人理解) 1. 安装vuex 命令行输入 npm install vuex ...
- 自动化运维工具——ansible详解(二)
Ansible playbook 简介 playbook 是 ansible 用于配置,部署,和管理被控节点的剧本. 通过 playbook 的详细描述,执行其中的一系列 tasks ,可以让远端主机 ...
- JavaWeb框架_Struts2_(七)----->文件的上传和下载
这个章节是Struts2框架应用最广泛的三个版块(上传下载.国际化.校验输入)之一,所以这一版块的学习还蛮重要的. 1. 章节目录 Struts2文件上传 单文件上传 拦截器实现文件过滤 文件上传常量 ...
- NYOJ 2357: 插塔憋憋乐 贪心
2357: 插塔憋憋乐 时间限制: 1 Sec 内存限制: 128 MB 提交: 82 解决: 18 [提交][状态][讨论版] 题目描述 众所不知,LLM是一位红警3大佬,打的非常厉害,但是曾经 ...
- ibv_get_device_guid()函数
uint64_t ibv_get_device_guid(struct ibv_device *device); 描述 函数返回RDMA 设备的 GUID(The Global Unique IDen ...
- constructor 属性,判断是否为数组
<!--你可以使用 constructor 属性来查看是对象是否为数组 (包含字符串 "Array"):--><p>constructor属性返回变量或对象 ...
- Hibernate框架入门
导读 本文主要介绍hibernate的入门,主要包括以下内容:hibernate介绍.hibernate环境搭建.hibernate简单测试.测试涉及的api详解. 一.hibernate介绍 JDB ...
- NGUI_概述
序言:这是张三疯第一次开始NGUI插件的学习,刚开始学习,肯定有很多漏洞,后期会及时的补上的. 希望大家可以见谅,希望大佬多多指教. 一.什么是NGUI: NGUI是严格遵循KISS原则并用C#编写的 ...