POJ 3984 迷宫问题 bfs

题目链接:迷宫问题

天啦撸。最近怎么了。小bug缠身,大bug 不断。然这是我大腿第一次给我dbug。虽然最后的结果是。我............bfs入队列的是now..............

然后。保存路径的一种用的string 。一种用的数组。大同小异。根据就是我bfs 先搜到的绝壁就是步数最少的、

附代码:

pre 数组

 1 /*
 2  很简单的广搜。如果不是+路径输出的话。。
 3  保存路径。
 4  */
 5
 6 #include <stdio.h>
 7 #include <string.h>
 8 #include <iostream>
 9 #include <queue>
10 #include <string>
11 using namespace std;
12
13 struct Node {
14     int x, y;
15 }now, temp, ans;
16
17 int mp[10][10];
18 int vis[10][10];
19 Node que[210];
20 Node pre[210];
21
22 int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
23
24 bool check(Node a) {
25     if (a.x >= 0 && a.x < 5 && a.y >= 0 && a.y < 5 && !vis[a.x][a.y] && mp[a.x][a.y] == 0)
26         return true;
27     return false;
28 }
29
30 int head = 0, tail = 0;
31
32 void bfs() {
33     while(head < tail) {
34         now = que[head++];
35         if (now.x == 4 && now.y == 4) {
36             return;
37         }
38         for (int i=0; i<4; ++i) {
39             temp.x = now.x + dir[i][0];
40             temp.y = now.y + dir[i][1];
41
42             if (check(temp)) {
43                 que[tail++] = temp;
44                 vis[temp.x][temp.y] = 1;
45                 int id = temp.x * 5 + temp.y;
46                 pre[id].x = now.x, pre[id].y = now.y;
47             }
48         }
49     }
50 }
51
52 void outPre(int num) {
53     if (pre[num].x == -1 && pre[num].y == -1)
54         return;
55     else outPre(5*pre[num].x+pre[num].y);
56     cout << "(" << pre[num].x << ", " << pre[num].y << ")\n";
57 }
58
59 int main() {
60     head = 0, tail = 0;
61     memset(vis, 0, sizeof(vis));
62
63     for (int i=0; i<5; ++i) {
64         for (int j=0; j<5; ++j) {
65             cin >> mp[i][j];
66         }
67     }
68     now.x = 0, now.y = 0;
69     vis[0][0] = 1;
70     que[tail++] = now;
71     pre[0].x = -1, pre[0].y = -1;
72     bfs();
73
74     outPre(24);
75     cout << "(4, 4)\n";
76     return 0;
77 }

string

 1 /*
 2  很简单的广搜。如果不是+路径输出的话。想了一下 好像是dfs?好像不是啊。
 3
 4  */
 5
 6 #include <stdio.h>
 7 #include <string.h>
 8 #include <iostream>
 9 #include <queue>
10 #include <string>
11 using namespace std;
12
13 struct Node {
14     int x, y;
15     string ansStep;
16 }now, temp, ans;
17
18 int mp[10][10];
19 int vis[10][10];
20 Node que[210];
21
22 int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
23 bool check(Node a) {
24     if (a.x >= 0 && a.x < 5 && a.y >= 0 && a.y < 5 && !vis[a.x][a.y] && mp[a.x][a.y] == 0)
25         return true;
26     return false;
27 }
28
29 int head = 0, tail = 0;
30
31 void bfs() {
32     while(head < tail) {
33         now = que[head++];
34         if (now.x == 4 && now.y == 4) {
35             now.ansStep += ‘\0‘;
36             ans.ansStep = now.ansStep;
37             return;
38         }
39         for (int i=0; i<4; ++i) {
40             temp.x = now.x + dir[i][0];
41             temp.y = now.y + dir[i][1];
42
43             if (check(temp)) {
44                 vis[temp.x][temp.y] = 1;
45                 temp.ansStep = now.ansStep;
46                 temp.ansStep += temp.x + ‘0‘;
47                 temp.ansStep += temp.y + ‘0‘;
48                 que[tail++] = temp;
49             }
50         }
51     }
52 }
53
54 int main() {
55     head = 0, tail = 0;
56     memset(vis, 0, sizeof(vis));
57
58     for (int i=0; i<5; ++i) {
59         for (int j=0; j<5; ++j) {
60             cin >> mp[i][j];
61         }
62     }
63     now.x = 0, now.y = 0;
64     now.ansStep += "00";
65
66     vis[0][0] = 1;
67     que[tail++] = now;
68     bfs();
69
70     string ansstr = ans.ansStep;
71     int len = ansstr.length();
72     for (int i=0; i<len && i+2<len; i+=2) {
73         cout << "(" << ansstr[i] << ", " << ansstr[i+1] << ")" << endl;
74     }
75     return 0;
76 }

时间: 2024-08-04 23:52:05

POJ 3984 迷宫问题 bfs的相关文章

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

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解法

#include<stdio.h> #include<string.h> #include<algorithm> #include<stack> using namespace std; int a[5][5],b[5][5]; int di[4][2]={0,1,0,-1,1,0,-1,0}; void bfs(int x,int y) { int tx=x,ty=y,i; if(a[x][y]==0) { a[x][y]=1; for(i=0;i<

POJ 3984 迷宫问题 (BFS + Stack)

链接 : Here! 思路 : BFS一下, 然后记录下每个孩子的父亲用于找到一条路径, 因为寻找这条路径只能从后向前找, 这符合栈的特点, 因此在输出路径的时候先把目标节点压入栈中, 然后不断的向前寻找, 最后直接pop出栈中所有的元素即可. 注意 : 不要把局部变量压入栈中, 这样就直接段错误了? ?? /************************************************************************* > File Name: 3984-迷宫

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

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 ********************

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, }; 它表示一个