迷宫问题 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),逃离迷宫
[原创] 今天来说说深度优先搜索,深度优先是图论中的内容,大意是从某一点出发,沿着一个方向搜索下去,并伴随着有回退的特点,通常用来判断某一解是否存在,不用来寻找最优解:这里来看一个非常有意思的题目: ...
随机推荐
- riot.js教程【六】循环、HTML元素标签
前文回顾 riot.js教程[五]标签嵌套.命名元素.事件.标签条件 riot.js教程[四]Mixins.HTML内嵌表达式 riot.js教程[三]访问DOM元素.使用jquery.mount输入 ...
- 了解Python列表的一些方法
首先定义一个名字列表,然后使用print() BIF在屏幕上显示这个列表. 接下来,使用len() BIF得出列表中有多少个数据项,然后再访问并显示第2个数据项的值: 创建了列表之后,可以使用列表方法 ...
- 关于svg
动画:css3动画,canvas(js动画),svg(html动画). svg基本元素 version: 表示 <svg> 的版本,目前只有 1.0,1.1 两种 xmlns:http:/ ...
- redis配置文件之复制
主从复制使用slaveof将Redis实例作为另一个Redis服务器的副本. 1) Redis复制是异步的,master可以配置成如果它连接的slave没有达到给定的数量,就停止接受写入.2) 如果断 ...
- codeblocks+mbedtls库配置
网上都没有找到window下mbedtls的相关配置,或许是太简单了.希望可以帮助那些像我这样的小白一枚. 下载 github的下载:https://github.com/ARMmbed/mbedtl ...
- Git命令汇总(基础篇)
自己用Git有一段时间了,随着项目越来越多,功能分支也随之增加,从简单的基础命令到随心所欲,需要自己不断地去尝试总结,下面来分享一下我的Git使用总结. 本章基础篇主要讲解一些Git代码提交流程和Gi ...
- ZBX_NOTSUPPORTED: Item does not allow parameters.
搞mongo监控的时候,zabbix报错:ZBX_NOTSUPPORTED: Item does not allow parameters. 想了半天,不知道原因,最后经过大神指点,原来是zabb ...
- HiveSchemaTool-Parsing failed. Reason- Unrecognized option- -dbType mysql
版本: Hive2.1 在linux上部署Hive的时候,初始化元数据的时候,出现HiveSchemaTool:Parsing failed. Reason: Unrecognized option: ...
- java学习笔记之System类
System类常用总结 System类概述 java.lang.System类,系统属性信息工具类 常用静态方法: 1. public static long currentTimeMillis() ...
- 从开源项目看 Python 单元测试
我觉得以前在我开发程序的时候,除了文档,可能单元测试是另外一个让我希望别人都写,但是自己又一点都不想写的东西.但是,随着开发程序的增多,以及自己对 Bug 的修改的增多,我发现,UT 在很大程度上是对 ...