复杂迷宫问题的递归实现以及最短路径
2017-03-12 19:25
513 查看
复杂迷宫就是有多条通路的迷宫,而且要求解最短路的话就要用递归集合栈,用递归探测,寻找所有通路,用栈保存所走过的路径,具体方法为:
(1)从当前点为起点探测由它开始能前进的点(这里的探测方向为上,有,左,下);
(2)找到能前进的点之后,首先将这个点的坐标压入栈(path)中保存起来,并将这个点的值赋为前一个点的值加1;递归调用这个函数,继续探测;
(3)当前结点不能前进之后,将path中的点弹出栈,探测它的另一个可以前进的方向;
(4)当前结点为出口时,比较栈path和栈shortestpath的size,将值较小的栈赋值给shortestpath;
具体代码如下:
运行结果:
这里我们可以清楚地看到所走过的路径。
而且我们可以看到,栈shortestpath中保存着最短路径的坐标。
(1)从当前点为起点探测由它开始能前进的点(这里的探测方向为上,有,左,下);
(2)找到能前进的点之后,首先将这个点的坐标压入栈(path)中保存起来,并将这个点的值赋为前一个点的值加1;递归调用这个函数,继续探测;
(3)当前结点不能前进之后,将path中的点弹出栈,探测它的另一个可以前进的方向;
(4)当前结点为出口时,比较栈path和栈shortestpath的size,将值较小的栈赋值给shortestpath;
具体代码如下:
#include <iostream> #include <stack> using namespace std; const int N = 10; int MazeMap = { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, { 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 }, { 1, 1, 0, 1, 1, 1, 1, 1, 1, 1 }, { 1, 1, 0, 1, 1, 0, 0, 0, 1, 1 }, { 1, 1, 0, 1, 1, 0, 1, 0, 1, 1 }, { 1, 1, 0, 0, 0, 0, 0, 0, 1, 1 }, { 1, 1, 1, 0, 1, 1, 1, 0, 1, 1 }, { 1, 1, 1, 0, 1, 1, 1, 0, 1, 1 }, { 1, 1, 1, 0, 1, 1, 1, 0, 1, 1 }, }; void PrintMap(int maze[] , size_t N) { for (size_t i = 0;i < N;++i) { for (size_t j = 0;j < N;++j) { cout << maze[i][j]<<" "; } cout << endl; } } struct Pos { size_t i; size_t j; }; bool IsGoingR(int maze[] , size_t n, Pos cur, Pos next) { if (next.i < n && next.i >= 0 && next.j < n && next.j >= 0) { if (maze[next.i][next.j] == 0 || maze[cur.i][cur.j] < maze[next.i][next.j]) { return true; } } return false; } bool GetTheWayR(int maze[] , size_t n, Pos pos, stack<Pos>& path,stack<Pos>& shortestpath) { path.push(pos); if (pos.i == n - 1) { if (path.size() < shortestpath.size()||shortestpath.empty()) { shortestpath = path; } } Pos next; //上 next = pos; next.i = pos.i - 1; if (IsGoingR(maze, N, pos, next)) { maze[next.i][next.j] = maze[pos.i][pos.j] + 1; if (GetTheWayR(maze, N, next, path, shortestpath)) { return true; } } //右 next = pos; next.j = pos.j + 1; if (IsGoingR(maze, N, pos, next)) { maze[next.i][next.j] = maze[pos.i][pos.j] + 1; if (GetTheWayR(maze, N, next, path, shortestpath)) { return true; } } //左 next = pos; next.j = pos.j - 1; if (IsGoingR(maze, N, pos, next)) { maze[next.i][next.j] = maze[pos.i][pos.j] + 1; if (GetTheWayR(maze, N, next, path, shortestpath)) { return true; } } //下 next = pos; next.i = pos.i + 1; if (IsGoingR(maze, N, pos, next)) { maze[next.i][next.j] = maze[pos.i][pos.j] + 1; if (GetTheWayR(maze, N, next, path, shortestpath)) { return true; } } path.pop(); return 0; } void Test() { PrintMap(MazeMap, N); stack<Pos> path; stack<Pos> shortestpath; Pos entry = { 2,0 }; GetTheWayR(MazeMap, N,entry,path,shortestpath); cout << endl; PrintMap(MazeMap, N); }
运行结果:
这里我们可以清楚地看到所走过的路径。
而且我们可以看到,栈shortestpath中保存着最短路径的坐标。
相关文章推荐
- 实现迷宫问题的所有路径及最短路径程序
- 3.10 用栈实现解决迷宫问题(输出一条路径(非最短路径))
- 用并查集(find-union)实现迷宫算法以及最短路径求解
- 关于栈与递归求解迷宫与迷宫最短路径问题
- 迷宫基于栈的实现--最短路径的非递归实现
- hdu3790最短路径问题 (用优先队列实现的||dijkstar)
- 街区最短路径问题Java实现
- 最短路径问题 java实现 源代码
- 迷宫最短路径的C++实现(队列:广度优先)
- Dijstra算法实现求解单源最短路径问题
- 最短路径实现代码-->Dijkstra求解最短路径问题
- 老鼠过迷宫问题——递归实现
- 最小生成树与最短路径的区别以及实现方法
- Bellman—Ford算法实现(解决存在负边情况下单源最短路径问题)
- 每对顶点间的最短路径算法时间复杂度改进C++实现
- HUD3790最短路径问题(SPFA实现)
- 队列实现迷宫问题(精装版,可返回路径)
- 最小生成树与最短路径的区别以及实现方法
- AStar算法求解迷宫最短路径问题
- hdu3790最短路径问题 (用优先队列实现的)