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)的更多相关文章

  1. 洛谷——P1825 [USACO11OPEN]玉米田迷宫Corn Maze

    P1825 [USACO11OPEN]玉米田迷宫Corn Maze 题目描述 This past fall, Farmer John took the cows to visit a corn maz ...

  2. 洛谷 P1825 [USACO11OPEN]玉米田迷宫Corn Maze

    P1825 [USACO11OPEN]玉米田迷宫Corn Maze 题目描述 This past fall, Farmer John took the cows to visit a corn maz ...

  3. 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 ...

  4. 洛谷—— P1825 [USACO11OPEN]玉米田迷宫Corn Maze

    https://www.luogu.org/problem/show?pid=1825 题目描述 This past fall, Farmer John took the cows to visit ...

  5. [USACO11OPEN]玉米田迷宫Corn Maze

    题目描述 This past fall, Farmer John took the cows to visit a corn maze. But this wasn't just any corn m ...

  6. 【luogu P1825 [USACO11OPEN]玉米田迷宫Corn Maze】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1825 带有传送门的迷宫问题 #include <cstdio> #include <cst ...

  7. 洛谷 P1825 【[USACO11OPEN]玉米田迷宫Corn Maze】

    P1825 传送门 简单的题意 就是一个有传送门的迷宫问题(我一开始以为是只有1个传送门,然后我就凉了). 大体思路 先把传送门先存起来,然后跑一下\(BFS\). 然后,就做完了. 代码鸭 #inc ...

  8. Maze迷宫问题(求最优解)

    迷宫地形我们可以通过读文件的形式,通过已知入口逐个遍历坐标寻找通路. 文件如图: 每个坐标的位置用结构体来记录: struct Pos //位置坐标 { int _row; int _col; }; ...

  9. 深度优先搜索(DFS),逃离迷宫

    [原创] 今天来说说深度优先搜索,深度优先是图论中的内容,大意是从某一点出发,沿着一个方向搜索下去,并伴随着有回退的特点,通常用来判断某一解是否存在,不用来寻找最优解:这里来看一个非常有意思的题目: ...

随机推荐

  1. 0e开头MD5值小结

    s878926199a 0e545993274517709034328855841020 s155964671a 0e342768416822451524974117254469 s214587387 ...

  2. 极光推送_总结_01_Java实现极光推送

    一.代码实现 1.配置类—Env.java package com.ray.jpush.config; /**@desc : 极光推送接入配置 * * @author: shirayner * @da ...

  3. 关于Excel数据批量导入数据库的案例

    写这个案例主要是感觉这个功能挺实用,很多地方会用得到的,废话就不多说了,直接上对应的源码. 这个案例我运用的是Winform窗体程序实现数据的导入. 首先是数据库的登陆界面如下: 源码如下: usin ...

  4. Less的转义字符

    Less的转义字符 有时候,当需要引入无效的CSS语法或Less不能识别的字符,就需要使用转义字符.此时,就可以在字符串前面加一个 ~,并将需要转义的字符串放在 "" 中.格式为: ...

  5. 实际应用中遇到TimedRotatingFileHandler不滚动的问题

    需求: 程序每天晚上8点和10点定时运行,期望日志按日期记录 添加Handler部分代码如下: formatter = logging.Formatter("%(asctime)s %(fi ...

  6. 一步步搭建Retrofit+RxJava+MVP网络请求框架(二),个人认为这次封装比较强大了

    在前面已经初步封装了一个MVP的网络请求框架,那只是个雏形,还有很多功能不完善,现在进一步进行封装.添加了网络请求时的等待框,retrofit中添加了日志打印拦截器,添加了token拦截器,并且对Da ...

  7. HAUT 1261地狱飞龙 自适应辛普森 数值积分

    1261: 地狱飞龙 时间限制: 1 秒  内存限制: 64 MB 提交: 300  解决: 68 题目描述 最近clover迷上了皇室战争,他抽到了一种地狱飞龙,很开心.假设地域飞龙会对距离为d的敌 ...

  8. angularjs 利用$http 请求出现 400 Bad Request

    1. 出现400错误-代表错误的请求,说明我们的参数有问题 说明此时传入的参数存在问题,我们看下此时参数的格式是什么: 此时的参数是对象格式,查了一下,如果利用ajax格式传输数据的话,参数必须是js ...

  9. 数据结构与算法(C/C++版)【串】

    第四章<串.数组> (一)串   数据结构中提到的串,即字符串,由 n 个字符组成的一个整体( n >= 0 ).这 n 个字符可以由字母.数字或者其他字符组成.例如,S = &qu ...

  10. SpringBoot入门

    简介 从本质上来说,Spring Boot就是Spring,它做了那些没有它你也会去做的Spring Bean配置.它使用"习惯优于配置"(项目中存在大量的配置,此外还内置了一个习 ...