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

Q:

  

  给定一个N*N的迷宫中,(0,0)为起始点,(N-1,N-1)为目的地,求可通往目的地的多个解

 1 #include <iostream>
 2 #include <vector>
 3 #include <cstring>
 4 using namespace std;
 5
 6 typedef struct
 7 {
 8     int x;
 9     int y;
10 }pos;
11
12 #define N 4  //4*4 maze
13 #define ENTER_X 0
14 #define ENTER_Y 0
15 #define EXIT_X N-1
16 #define EXIT_Y N-1
17
18 int Maze[N][N];
19 int paths;  //sum of paths
20 vector<pos> Path;  //svae paths
21 vector<pos> BestPath; //save min distant path
22
23 void InitMaze();
24 bool MazeTrack(int x,int y);
25
26 int main()
27 {
28     InitMaze();
29     int i = 0 , j = 0;
30     for(i=0;i<N;i++)
31     {    for(j=0;j<N;j++)
32             cout << Maze[i][j];
33         cout << endl;
34     }
35     MazeTrack(ENTER_X,ENTER_Y);
36     return 0;
37 }
38
39 void InitMaze()
40 {
41     int    a[N][N] = {
42         {0,0,0,0},
43         {1,0,1,0},
44         {1,0,1,0},
45         {1,0,1,0}
46         };
47     memcpy(Maze,a,sizeof(a));
48     paths = 0;
49 }
50
51 bool MazeTrack(int x,int y)
52 {
53     bool IsPath = false;
54     pos p;
55     p.x = x;
56     p.y = y;
57     //set range of maze‘s x/y
58     if(x<N && x>=0 && y<N && y>=0 && Maze[x][y]==0)
59     {
60         //make
61         Path.push_back(p);
62         Maze[x][y] = 1;  //if value is 1,you can‘t go there
63
64         cout << "now,position is("<<x<<","<<y<<")" << endl;
65         //terminal
66         if(x==EXIT_X && y==EXIT_Y)
67         {
68             cout << "find a path" << endl;
69             paths++;
70             IsPath = true;
71             vector<pos>::iterator it;
72             for(it=Path.begin() ; it!=Path.end() ; it++)
73                 cout << "("<< it->x <<","<< it->y <<")" << " ";
74             cout << endl;
75             return IsPath;
76         }
77         //search  (find forward solutions)
78         IsPath = MazeTrack(x-1,y) || MazeTrack(x,y-1) || MazeTrack(x+1,y) || MazeTrack(x,y+1);
79         //backtrack
80         if(!IsPath)
81         {
82             Path.pop_back();
83             Maze[x][y] = 0;
84         }
85         cout << Path.size() << endl;
86     }
87     //fuction exit
88     return IsPath;
89 } 

输出的解:

0000
1010
1010
1010
now,position is(0,0)
now,position is(0,1)
now,position is(1,1)
now,position is(2,1)
now,position is(3,1)
4
3
2
now,position is(0,2)
now,position is(0,3)
now,position is(1,3)
now,position is(2,3)
now,position is(3,3)
find a path
(0,0) (0,1) (0,2) (0,3) (1,3) (2,3) (3,3)
7
7
7
7
7
7

solution

第一次用回溯法,参考了下面的用回溯法解迷宫问题的模板:

 1 using namespace std;
 2 #define N 100
 3 #define M 100
 4
 5 typedef struct
 6 {
 7        int x;
 8        int y;
 9 }Point;
10
11 vector<Point>solutionPath ;  //存放有解的坐标路径
12
13 //主函数 x,y默认为起始结点,如(0,0), 得到从起始结点到目的结点的路径。
14 bool hasSolutionFunction( template<typename T>* Matrix , int x, int y)
15 {
16
17     bool *visited = new bool[M*N];
18     memset (visited,0,M*N); //visited 存放每个结点是否被遍历,true为已经遍历过,false为否
19
20     res =  hasSolutionCore(Matrix , x ,y)
21     cout<<solutionPath<<endl;
22     return res
23
24 }
25
26 //深度遍历求解路径的函数
27 bool hasSolutionCore(template<typename T>*  Matrix , int x, int y)
28 {
29
30     hasSolution = false;
31     Point p = Point(x,y);
32
33
34
35     if (x,y) is terminal  //x,y 已经是终止条件,当求解到这个结点是叶结点或目的地
36         {
37         solutionPath.push_back(p);
38         return true;
39         }
40
41     if ((x,y) && visited[x][y]==flase )// x,y是合法结点(具体条件可扩展),且未被访问过
42     {
43         visited[x][y] = true;
44         solutionPath.push_back(p);
45
46         hasSolution = hasSolutionCore(Matrix,x-1, y) ||hasSolutionCore(Matrix,x,y-1)||... //如果不是叶结点,则该路径是否有解取决于其他方向的往后求解。
47
48         // x,y结点以下的所有子路径都无解,则回溯
49         if (!hasSolution)
50         {
51         visited[x][y] = false;
52         solutionPath.pop_back();
53         }
54
55     }
56     return hasSolution;
57
58 }
时间: 2024-08-29 13:43:18

迷宫问题 Maze 4X4 (sloved by backtrack)的相关文章

数据结构与算法4: 经典问题之迷宫问题(Maze path)

数据结构与算法4: 经典问题之迷宫问题(Maze path) 写在前面 本节实践了教材[1][2]的两种经典迷宫算法.注意体会栈的运用.如果有改进意见请帮助我. 1.迷宫问题直观感受 下面给出迷宫问题的一个直观感受图,引入图只是为了帮助直观理解,这里不涉及OpenGL等其他绘图内容. 下图中棕色代表通道阻塞,白色代表可用通道,红色代表起始位置,绿色代表当前位置,黄色代表出口. (采用C++ 和OpenGL 绘制,目前是2D版本,3D版本有空时再补上) 迷宫1: 迷宫2: 2.迷宫算法 2.1 迷

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

P1825 [USACO11OPEN]玉米田迷宫Corn Maze 题目描述 This past fall, Farmer John took the cows to visit a corn maze. But this wasn't just any corn maze: it featured several gravity-powered teleporter slides, which cause cows to teleport instantly from one point in

[USACO11OPEN]玉米田迷宫Corn Maze

题目描述 This past fall, Farmer John took the cows to visit a corn maze. But this wasn't just any corn maze: it featured several gravity-powered teleporter slides, which cause cows to teleport instantly from one point in the maze to another. The slides w

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

题目链接:https://www.luogu.org/problemnew/show/P1825 带有传送门的迷宫问题 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int maxn = 2001; char map[maxn][maxn]; int fx[4] = {0, 1, 0, -1};

[uva11OPEN]玉米田迷宫Corn Maze(广搜bfs)

第一次做MLE了…第二次WA了5个点,其实就是一个判断错了…QAQ总的来说…是个水题/板子题(逃 #include<bits/stdc++.h> using namespace std; #define For(i,l,r) for(register int i=l; i<r; i++) int n,m; int d[4][2] = { 0, 1, 0, -1, 1, 0, -1, 0 }; bool bj,vis[301][301]; char ch[301][301]; struct

Maze迷宫问题

迷宫地形我们可以通过读文件的形式,通过已知入口逐个遍历坐标寻找通路. 文件如图: 每个坐标的位置用Pos来记录: struct Pos    //位置坐标 {    int  _row;    int _col; }; 定义行列范围 #define M 10   //行 #define N 10   //列 初始化迷宫数组:将通过读文件的方式获取的字符转成整型数据,保存在M行N列的数组中. void InitMaze(int* maze) { struct WavHeadhWAV; FILE* 

Maze迷宫问题(求最优解)

迷宫地形我们可以通过读文件的形式,通过已知入口逐个遍历坐标寻找通路. 文件如图: 每个坐标的位置用结构体来记录: struct Pos //位置坐标 { int _row; int _col; }; 定义行列范围: #define M 10 //行 #define N 10 //列 初始化迷宫数组:将通过读文件的方式获取的字符转成整型数据,保存在M行N列的数组中. void InitMaze(int* maze) { struct WavHeadhWAV; FILE* fout = fopen(

迷宫的实现

问题描述:实验心理学中的一个典型的问题,心理学家吧一只老鼠从一个无顶的大盒子的入口处赶进迷宫.迷宫设置很多隔壁,对前进方向形成了许多障碍,心理学家在迷宫的唯一出口处放置了一块奶酪,吸引老鼠仔迷宫中寻找通路以到达出口. 求解思想:回溯法是一种不断试探且及时纠正错误的搜索方法,下面的求解过程采用回溯法.从入口出发,按某一方向向前探索,若能走通(未走过的),即某处可以到达,则到达一个新点,否则试探下一个方向:若所有的方向均没有通路,则沿原路返回前一点,换下一个方向继续试探,直到所有可能的通路都搜索到,

数据结构之迷宫问题求解(二)迷宫的最短路径

上篇文章我们讨论了,迷宫问题的普通求解问题,这篇文章我们继续深入,求迷宫的最短路径. 要想求迷宫的最短路径,一个很简单的方法就是再设置一个Min栈,用来放最短路径,每找到一个出口,就将path栈与Min栈进行比较,如果path栈更小,则赋值给Min. 而在上篇文章中,我们将走过的路径做了标记,每走一个坐标,就把那个坐标置为3,直至找到出口. 因此如果用这种标记方式,显然是会出现问题的. 所以我们需要换种标记方式! 最终....我决定,使出口的值为2,每走一步使当前位置标记变为是上一位置标记再加1