迷宫问题 POJ 3984

http://poj.org/problem?id=3984

定义一个二维数组: 

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)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <queue>
#include <algorithm>
using namespace std;
#define maxn 521
#define INF 0xfffffff
int maps[maxn][maxn];
int dir[4][2]= {{0,1},{1,0},{-1,0},{0,-1}};

struct node
{
    int x, y;
}a[maxn][maxn];

void DFS(int x, int y)
{
    if(x == 0 && y == 0)
    {
        printf("(0, 0)\n");
        return ;
    }

    DFS(a[x][y].x, a[x][y].y);

    printf("(%d, %d)\n",x, y);
}
void BFS()
{
    int i;
    queue<node>Q;
    node begins, now, next;

    begins.x = 0;
    begins.y = 0;

    Q.push(begins);

    while(Q.size())
    {
        now = Q.front();
        Q.pop();

        if(now.x == 4 && now.y == 4)
            return ;

        for(i=0; i<4; i++)
        {
            next.x = now.x + dir[i][0];
            next.y = now.y + dir[i][1];

            if(next.x>=0 && next.x<5 && next.y>=0 && next.y<5 && !maps[next.x][next.y])
            {
                maps[next.x][next.y]=1;
                a[next.x][next.y] = now;
                Q.push(next);
            }
        }
    }

}

int main()
{
    int i,j;

    for(i=0; i<5 ; i++)
    for(j=0; j<5; j++)
    scanf("%d", &maps[i][j]);

    BFS();
    DFS(4, 4);

    return 0;
}


 
时间: 2024-08-07 10:11:19

迷宫问题 POJ 3984的相关文章

简单BFS +打印路径 迷宫问题 POJ - 3984

#include<cstdio> #include<iostream> #include<cstring> #include<string> #include<stack> #include<queue> using namespace std; int nex[4][2]= { {0,1},{1,0},{0,-1},{-1,0} }; typedef struct { int x,y; } Point; int a[10][10],

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 难度: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+路径打印)

传送门: 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+记录路径)

题目连接: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 迷宫问题

迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16724   Accepted: 9981 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 逃离迷宫

迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10001   Accepted: 5939 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表示可以走的路,只能横着走或竖着走,不能斜着走,