榆木脑壳练算法之迷宫寻路问题
2013-04-17 23:48
169 查看
// MazeProblem.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <process.h> int maze[7][7] = { {1, 1, 1, 1, 1, 1, 1}, {1, 0, 0, 0, 0, 0, 1}, {1, 1, 1, 0, 1, 1, 1}, {1, 0, 1, 1, 1, 1, 1}, {1, 1, 0, 0, 1, 1, 1}, {1, 0, 0, 0, 0, 0, 1}, {1, 1, 1, 1, 1, 1, 1} }; int success = 0; void findExit(int xPos, int yPos, const int mazeScope) { //2表示在xPos和yPos这一点已经走过的标记 maze[xPos][yPos] = 2; //正常参数下递归的出口 if(xPos == (mazeScope - 2) && yPos == (mazeScope - 2)) { if(maze[xPos][yPos] == 0) { success = 1; } } else { //走路的试探方向分别是先向右,再向下,再向左,再向上 if(maze[xPos + 1][yPos] == 0 && success != 1)findExit(xPos + 1, yPos, mazeScope); if(maze[xPos][yPos + 1] == 0 && success != 1)findExit(xPos, yPos + 1, mazeScope); if(maze[xPos - 1][yPos] == 0 && success != 1)findExit(xPos - 1, yPos, mazeScope); if(maze[xPos][yPos - 1] == 0 && success != 1)findExit(xPos, yPos - 1, mazeScope); } if(success == 1){ return; } else { //运行到这里的话,已经开始回溯,因为maze[xPos][yPos] = 2这一句,使得本来为0的通路 //不会再重新被递归到,此处也说明了该点不是一个出口路径上的点,需要恢复原样 maze[xPos][yPos] = 0; } } void printIsHaveExit() { if(success == 1) { printf("There is a exit at in the maze"); } else { printf("There is not a exit at in the maze"); } } /************************************************************************/ /* 假设有这样一个迷宫用0代表路,用1代表墙,做上角为入口,右下角为出口, **求解迷宫中是否有这样的一条通路 */ /************************************************************************/ int _tmain(int argc, _TCHAR* argv[]) { findExit(1, 1, 7); printIsHaveExit(); system("pause"); return 0; }
相关文章推荐
- dfs寻路算法---迷宫问题实现
- DFS算法之迷宫寻路问题
- 第六周--数据结构之自建算法库之迷宫问题(用队列)
- 回溯算法经典应用之—迷宫问题 (Java)
- 算法之迷宫问题
- 基础迷宫问题-------------(图的BFS 题目取自算法竞赛入门)
- 【数据结构】栈实现迷宫寻路问题
- 迷宫问题算法分析
- 算法分析——走迷宫问题
- 基于递归分割的迷宫生成算法与自动寻路
- AI-迷宫寻路算法-深度优先搜索和广度优先搜索
- 算法:堆栈与深度优先搜索(迷宫问题)
- 机试算法讲解: 第41题 广度优先搜索之迷宫问题
- [Qt] 迷宫随机生成和寻路算法 - Qt实现的迷宫小游戏
- 基于递归分割的迷宫生成算法与自动寻路
- 遗传算法解迷宫问题
- 算12/24点算法 布线问题/迷宫最短路径
- 走迷宫问题 算法竞赛入门经典
- C语言算法实现迷宫问题2
- 迷宫寻路问题——A*算法