经典迷宫问题
2015-07-26 19:24
323 查看
经典的迷宫问题,分别用了BFS与DFS解决。
不过这里的DFS只能找到一条通的路径,并不能确保最短,而BFS找出来的是最短路径。
#include<iostream> #include<queue> using namespace std; /* 6 8 0 0 1 1 0 1 1 1 1 0 1 0 1 0 1 0 0 0 0 0 0 1 1 1 0 1 1 1 0 0 1 1 1 0 0 1 1 0 0 0 0 1 1 0 0 1 1 0 */ struct point{ int x; int y; }; int **maze, width, height; point **pre; int next2[4][2] = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } }; //输入迷宫 void input(){ cout << "请输入迷宫的高和宽" << endl; cin >> height >> width; //10,10 maze = new int *[height + 2]; pre = new point *[height + 2]; for (int i = 0; i <= height + 1; i++) { maze[i] = new int[width + 2]; pre[i] = new point[width + 2]; } for (int i = 0; i <= height + 1; i++) maze[i][0] = maze[i][width + 1] = 1; for (int j = 0; j <= width + 1; j++) maze[0][j] = maze[height + 1][j] = 1; for (int i = 1; i < height + 1; i++){ for (int j = 1; j < width + 1; j++) cin >> maze[i][j]; } for (int i = 0; i <= height + 1; i++){ for (int j = 0; j <= width + 1; j++){ cout << maze[i][j]; pre[i][j].x = pre[i][j].y = 0; } cout << endl; } } bool BFS(point start, point end){ if (start.x == end.x && start.y == end.y){ return true; } queue<point> queue; point now; queue.push(start); maze[start.x][start.y] = -1; while (!queue.empty()) { now = queue.front(); queue.pop(); for (int i = 0; i < 4; i++){ point move; move.x = now.x + next2[i][0]; move.y = now.y + next2[i][1]; if (move.x == end.x&&move.y == end.y) { pre[end.x][end.y].x = now.x; pre[end.x][end.y].y = now.y; return true; } if (maze[move.x][move.y] == 0){ queue.push(move); maze[move.x][move.y] = 1; pre[move.x][move.y].x = now.x; pre[move.x][move.y].y = now.y; } } } return false; } bool DFS(point start, point end){ if (start.x == end.x && start.y == end.y){ return true; } point now; point *queue = new point[100]; int count = 0; queue[count++] = start; maze[start.x][start.y] = -1; while (count) { now = queue[--count]; for (int i = 0; i < 4; i++){ point move; move.x = now.x + next2[i][0]; move.y = now.y + next2[i][1]; if (move.x == end.x&&move.y == end.y) { pre[end.x][end.y].x = now.x; pre[end.x][end.y].y = now.y; return true; } if (maze[move.x][move.y] == 0){ queue[count++] = move; maze[move.x][move.y] = 1; pre[move.x][move.y].x = now.x; pre[move.x][move.y].y = now.y; } } } return false; } //显示迷宫的路径 void showMaze(){ point now = { height, width }; point endPoint = { 0, 0 }; while (now.x != endPoint.x && now.y != endPoint.y) { cout << "(" << now.x << "," << now.y << ") "; now = pre[now.x][now.y]; } } int main(){ //输入迷宫 input(); point start; start.x = 1; start.y = 1; point end; end.x = height; end.y = width; //if (BDF(start, end)) if (DFS(start, end)) { for (int i = 0; i <= height + 1; i++){ for (int j = 0; j <= width + 1; j++){ cout << pre[i][j].x << "," << pre[i][j].y << " "; } cout << endl; } } showMaze(); }
不过这里的DFS只能找到一条通的路径,并不能确保最短,而BFS找出来的是最短路径。
相关文章推荐
- Win2003利用dfs(分布式文件系统)在负载均衡下的文件同步配置方案
- win2003分布式文件系统(dfs)配置方法[图文详解]
- win2003分布式文件系统及其部署 图文教程
- Hadoop2.6+jdk8的安装部署(1)——使用jar包安装部署【详细】
- Hadoop FS Shell
- DFS使用方法总结
- FastDFS注意事项
- 无忧技术带您预览DFS(分布式文件系统)管理控制台
- C 语言实现迷宫 DFS算法
- 一幅图弄清DFT与DTFT,DFS的关系
- HDFS---Namenode
- HDFS ---- Services startup
- POJ1523 SPF dfs
- poj1731 Orders dfs
- Surrounded Regions
- Word Ladder, Gray Code
- Binary Tree Zigzag Level Order Traversal,Restore IP Addresses,Word Search,Simplify Path
- Hdu2444二分图
- ZOJ Problem Set - 1711 解题报告
- DFS求岛的个数LeetCode 200. Number of Islands