BFS | 3984 | 迷宫问题
2013-05-13 20:56
676 查看
学习BFS,找到raphealguo的博客介绍BFS,完成了这题。
POJ 3984 迷宫问题
POJ 3984 迷宫问题
#include <cstdio> #include <iostream> #include <queue> using namespace std; int map[5][5], visit[25], path[25]; // int dir[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; // 左下上右搜索 bool is_leagal(int x, int y) { if (x < 0 || x > 4 || y < 0 || y > 4) return false; if (map[x][y] == 1) return false; return true; } void print(int final) { if (path[final] != -1) print(path[final]); printf("(%d, %d)\n", final / 5, final % 5); } void dfs() { queue<int> Q; int now, next; int x, y, temp_x, temp_y; // 第一个节点入列 visit[0] = true; path[0] = -1; Q.push(0); while (!Q.empty()) { // 取出第一个节点 now = Q.front(); Q.pop(); x = now / 5; y = now % 5; for (int i = 0; i < 5; ++i) { temp_x = x + dir[i][0]; temp_y = y + dir[i][1]; next = temp_x * 5 + temp_y; if (is_leagal(temp_x, temp_y) && !visit[next]) { path[next] = now; if (next == 24) return; visit[next] = true; Q.push(next); // 把周围合法节点入列 } } } } int main() { for (int i = 0; i < 5; ++i) for (int j = 0; j < 5; ++j) scanf("%d", &map[i][j]); dfs(); print(24); }可以最快的获得路径,因为BFS类似一层一层的扒皮,所以可以最快的找到一条路径。
相关文章推荐
- POJ-3984-迷宫问题【经典BFS】
- POJ 3984 迷宫问题(BFS)
- BFS(最短路+路径打印) POJ 3984 迷宫问题
- POJ-3984迷宫问题(典型BFS找最短路径)
- POJ-3984-迷宫问题-BFS(广搜)-手写队列
- POJ 3984-迷宫问题(BFS+回溯路径)
- POJ-3984-迷宫问题(BFS打印路径)
- OJ 3984 迷宫问题(BFS:迷宫最短路径且输出路径)
- POJ - 3984 迷宫问题 (BFS)
- POJ 3984 迷宫问题(BFS)
- poj 3984 迷宫问题(BFS+输出路径)
- 3984 迷宫问题 BFS入门题+记录路径
- poj 3984 迷宫问题(bfs)
- POJ 3984 迷宫问题(BFS)
- POJ 3984 迷宫问题 (BFS)
- POJ 3984 迷宫问题(bfs)
- POJ 3984 迷宫问题(bfs)
- POJ - 3984 迷宫问题 (BFS+前缀处理输出路径)
- POJ 3984 迷宫问题(BFS or DFS)
- POJ 3984 迷宫问题(BFS)