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. [贪心][高精]P1080 国王游戏(整合)

    题目描述 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 n 位大臣排成一排,国王站在队伍的最 ...

  2. MyBatis物理分页的代码实现

    一.分页 MyBatis有两种分页方法:内存分页,也就是假分页,本质是查出所有的数据然后根据游标的方式,截取需要的记录,如果数据量大,执行效率低,可能造成内存溢出.物理分页,就是数据库本身提供了分页方 ...

  3. Play-With-Docker在chrome上的插件

    一键使用PWD 在chrome扩展中,找到"Play With Docker"插件,并安装在chrome浏览器中 进入hub.docker.com网站,搜索熟悉的docker镜像. ...

  4. Java基础回顾(3)

    数组:用一种数据类型的集合 ★数组元素下标从0开始. 数组的复制.扩容: ①.System.arraycopy(源数组, 源数组的初始下标,                     目标数组, 目标数 ...

  5. 学习笔记:UITabBarController使用详解

    一.手动创建UITabBarController 最常见的创建UITabBarController的地方就是在application delegate中的 applicationDidFinishLa ...

  6. VSCode 同步设置及扩展插件 实现设备上设置统一

    准备工作:电脑上需安装VSCode,拥有一个github账户.实现同步的功能主要依赖于VSCode插件 "Settings Sync" Setting Sync 可同步包含的所有扩 ...

  7. 垃圾陷阱洛谷dp

    题目描述 卡门――农夫约翰极其珍视的一条Holsteins奶牛――已经落了到“垃圾井”中.“垃圾井”是农夫们扔垃圾的地方,它的深度为D(2<=D<=100)英尺. 卡门想把垃圾堆起来,等到 ...

  8. Maven SpringMVC整合Mybatis

    关于Spring的核心理念和Mybatis的优点网上已经有很多文档做了说明.这篇博客,只记录springmvc整合mybatis时常见的知识点,以及注意事项,它只有最精简的几个模块,以帮助初学者迅速搭 ...

  9. mustache.js 使用

    对于mustache模板,我是属于即用即查的方法,以下记录仅是我常用的方式.方便以后使用时不用再去项目中去找,因为真的不好找.(此处 -->serious 脸) 当需要渲染一些数据列表的时候,使 ...

  10. Linux安装Nginx以及简单理解

    1.Nginx简单介绍 ①.Nginx:一个高性能的HTTP和反向代理服务器,高并发处理很不错. ②.反向代理:在计算机世界里,由于单个服务器的处理客户端(用户)请求能力有一个极限,当用户的接入请求蜂 ...