poj 3984 迷宫问题(bfs)

迷宫问题

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8695 Accepted: 5125

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表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。

Input

一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。

Output

左上角到右下角的最短路径,格式如样例所示。

Sample Input

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

Sample Output

(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)

Source

题解:

每个节点记录父节点,递归打印路劲。

CODE:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<map>

#define N 100010
#define Mod 10000007
#define lson l,mid,idx<<1
#define rson mid+1,r,idx<<1|1
#define lc idx<<1
#define rc idx<<1|1
const double EPS = 1e-11;
const double PI = acos(-1.0);
typedef long long ll;

const int INF=1000010;

using namespace std;

int mp[10][10];
int x[4]= {-1,0,1,0};
int y[4]= {0,1,0,-1};

struct node
{
    int x,y;
    int nextx,nexty;
    int op;
} a[1000];

queue<node>que;

void print(int x)
{
    if(a[x].op!=-1)
    {
        print(a[x].op);
        printf("(%d, %d)\n",a[x].x,a[x].y);
    }
}

void bfs()
{
    int first=0,num=0;
    node s,t;
    a[first].x=0,a[first].y=0;
    a[first].op=-1;
    que.push(a[first++]);
    while(que.size())
    {
        t=que.front();
        que.pop();
        if(t.x==4&&t.y==4)
        {
            print(num);
            break;
        }
        for(int i=0; i<4; i++)
        {
            a[first].x=t.x+x[i];
            a[first].y=t.y+y[i];
            if(a[first].x>=0&&a[first].x<=4&&a[first].y>=0&&a[first].y<=4&&mp[a[first].x][a[first].y]==0)
            {
                a[first].nextx=t.x;
                a[first].nexty=t.y;
                a[first].op=num;
                mp[a[first].x][a[first].y]=1;
                que.push(a[first++]);
            }
        }
        num++;
    }
}

int main()
{
    freopen("test.in","r",stdin);
    while(cin>>mp[0][0]>>mp[0][1]>>mp[0][2]>>mp[0][3]>>mp[0][4])
    {
        for(int i=1; i<5; i++)
            for(int j=0; j<5; j++)
                cin>>mp[i][j];
        printf("(0, 0)\n");
        bfs();
    }
    return 0;
}
时间: 2024-07-29 04:10:32

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

题目链接:迷宫问题 天啦撸.最近怎么了.小bug缠身,大bug 不断.然这是我大腿第一次给我dbug.虽然最后的结果是.我............bfs入队列的是now.............. 然后.保存路径的一种用的string .一种用的数组.大同小异.根据就是我bfs 先搜到的绝壁就是步数最少的. 附代码: pre 数组 1 /* 2 很简单的广搜.如果不是+路径输出的话.. 3 保存路径. 4 */ 5 6 #include <stdio.h> 7 #include <str

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