POJ-3984 迷宫问题(BFS找最短路径并保存)

问题:

定义一个二维数组: 

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

输入:

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

输出:

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

//#include <bits/stdc++.h>
#include <stdio.h>
#include <cmath>
#include <queue>
#define inf 0x3f3f3f3f
#define FRE() freopen("in.txt", "r", stdin)
#define FRO() freopen("out.txt", "w", stdout)

using namespace std;
typedef long long ll;
const int maxn = 6;
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int mp[maxn][maxn];
struct Node
{
    int x,y;
    Node(int _x,int _y):x(_x),y(_y){}
    Node(){}
};
Node path[maxn][maxn];
queue<Node> que;

bool isin(int x,int y)
{
    if(x>=0 && x<5 && y>=0 && y<5)
        return true;
    return false;
}

void BFS()
{
    Node now = Node(0,0);
    que.push(now);
    mp[0][0]=1;

    while(!que.empty())
    {
        Node u = que.front();
        que.pop();
        if(u.x==4 && u.y==4) return;
        for(int i=0; i<4; i++)
        {
            int tx = u.x+dx[i];
            int ty = u.y+dy[i];
            if(isin(tx,ty) && mp[tx][ty]==0)
            {
                que.push(Node(tx,ty));
                mp[tx][ty] = 1;
                path[tx][ty] = u;
            }
        }

    }

}

void showPath(Node u)
{
    if(u.x==0 && u.y==0) printf("(%d, %d)\n",u.x,u.y);
    else
    {
        showPath(path[u.x][u.y]);
        printf("(%d, %d)\n",u.x,u.y);
    }
}

int main()
{
    for(int i=0; i<5; i++)
    {
        for(int j=0; j<5; j++)
        {
            scanf("%d",&mp[i][j]);
        }
    }

    BFS();
    showPath(Node(4,4));
    return 0;
}
/*
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)
*/

原文地址:https://www.cnblogs.com/sykline/p/11439339.html

时间: 2024-08-22 10:53:13

POJ-3984 迷宫问题(BFS找最短路径并保存)的相关文章

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 难度: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 + Stack)

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

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+路径保存

题目链接: 思路: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