BFS(最短路+路径打印) POJ 3984 迷宫问题

题目传送门

 1 /*
 2     BFS:额,这题的数据范围太小了。但是重点是最短路的求法和输出路径的写法。
 3         dir数组记录是当前点的上一个点是从哪个方向过来的,搜索+,那么回溯-
 4 */
 5 /************************************************
 6 Author        :Running_Time
 7 Created Time  :2015-8-4 9:02:06
 8 File Name     :POJ_3984.cpp
 9 ************************************************/
10
11 #include <cstdio>
12 #include <algorithm>
13 #include <iostream>
14 #include <sstream>
15 #include <cstring>
16 #include <cmath>
17 #include <string>
18 #include <vector>
19 #include <queue>
20 #include <deque>
21 #include <stack>
22 #include <list>
23 #include <map>
24 #include <set>
25 #include <bitset>
26 #include <cstdlib>
27 #include <ctime>
28 using namespace std;
29
30 #define lson l, mid, rt << 1
31 #define rson mid + 1, r, rt << 1 | 1
32 typedef long long ll;
33 const int MAXN = 10;
34 const int INF = 0x3f3f3f3f;
35 const int MOD = 1e9 + 7;
36 int a[MAXN][MAXN];
37 bool vis[MAXN][MAXN];
38 int dir[MAXN][MAXN];
39 int step[MAXN][MAXN];
40 int dx[4] = {-1, 1, 0, 0};
41 int dy[4] = {0, 0, -1, 1};
42 int n = 4, m = 4;
43
44 bool judge(int x, int y)    {
45     if (x < 0 || x > n || y < 0 || y > m || a[x][y] == 1)   return false;
46     return true;
47 }
48
49 void print_path(void)   {
50     int x = n, y = m;   vector<pair<int, int> > ans;
51     while (dir[x][y] != -1) {
52         ans.push_back (make_pair (x, y));
53         int px = x, py = y;
54         x -= dx[dir[px][py]];    y -= dy[dir[px][py]];
55     }
56     int sz = (int) ans.size ();
57     printf ("(0, 0)\n");
58     for (int i=sz-1; i>=0; --i) {
59         printf ("(%d, %d)\n", ans[i].first, ans[i].second);
60     }
61 }
62
63 void BFS(void)  {
64     memset (vis, false, sizeof (vis));
65     memset (step, INF, sizeof (step));
66     memset (dir, -1, sizeof (dir));
67     queue<pair<int, int> > Q; Q.push (make_pair (0, 0));    vis[0][0] = true;
68     step[0][0] = 0;
69     while (!Q.empty ()) {
70         int x = Q.front ().first, y = Q.front ().second;    Q.pop ();
71         for (int i=0; i<4; ++i) {
72             int tx = x + dx[i], ty = y + dy[i];
73             if (!judge (tx, ty))    continue;
74             if (vis[tx][ty] && step[tx][ty] <= step[x][y] + 1)  continue;
75             if (tx == n && ty == m) {
76                 dir[tx][ty] = i;    print_path ();  return ;
77             }
78             dir[tx][ty] = i;    step[tx][ty] = step[x][y] + 1;
79             Q.push (make_pair (tx, ty));    vis[tx][ty] = true;
80         }
81     }
82 }
83
84 int main(void)    {     //POJ 3984 迷宫问题
85     for (int i=0; i<5; ++i)    {
86         for (int j=0; j<5; ++j)    {
87             scanf ("%d", &a[i][j]);
88         }
89     }
90     BFS ();
91
92     return 0;
93 }
时间: 2024-08-02 11:02:21

BFS(最短路+路径打印) POJ 3984 迷宫问题的相关文章

poj 3984:迷宫问题(广搜,入门题)

迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7635   Accepted: 4474 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, }; 它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要

POJ 3984 迷宫问题 搜索题解

本题可以使用BFS和DFS解题,也可以构建图,然后利用Dijsktra解题. 不过因为数据很少,就没必要使用Dijsktra了. BFS和DFS效率都是一样的,因为都需要搜索所有可能的路径,并记录最短路径和当前路径. 推荐使用DFS,感觉会方便很多,BFS会麻烦很多,因为需要记录并比较路径. #include <stdio.h> #include <string.h> #include <limits.h> const int MAX_N = 5; const int

POJ 3984 迷宫问题(简单bfs+路径打印)

传送门: http://poj.org/problem?id=3984 迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33105   Accepted: 18884 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, }; 它表示一个

(简单) POJ 3984 迷宫问题,BFS。

Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, }; 它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线. 水题,BFS然后记录路径就好. 代码如下: #include<iostream> #include<cstring> #

poj 3984 迷宫问题 (BFS+记录路径)

题目连接:http://poj.org/problem?id=3984 题解:简单的BFS+记录路径,具体题解看代码注释. #include <iostream> #include <queue> #include <cstdio> using namespace std; struct point { int x; int y; }; queue<point>q; int map[5][5]; int vis[5][5];//标记走过的路 int g[4]

POJ 3984:迷宫问题(BFS+路径记录)

迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7560   Accepted: 4426 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, }; 它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要

POJ 3984 迷宫问题【BFS/路径记录】

迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31428 Accepted: 18000 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, }; 它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编

POJ 3984 迷宫问题 BFS+路径保存

题目链接: 思路:STL果然不是万能的..写了一个下午... 改用普通数组写一会便过了..真坑.. 主要就是建立一个保存前驱的数组 代码: #include <iostream> #include <cstdio> #include <queue> using namespace std; int a[6][6]; int go[4][2] = {1,0,0,1,-1,0,0,-1}; struct node { int x; int y; }q[100]; int p

POJ 3984 迷宫问题 bfs 难度:0

http://poj.org/problem?id=3984 典型的迷宫问题,记录最快到达某个点的是哪个点即可 #include <cstdio> #include <cstring> #include <queue> using namespace std; const int maxn=10; const int inf=0x3fffffff; struct pnt { int x,y; pnt(){x=y=0;} pnt(int tx,int ty){x=tx,y