POJ2251——Dungeon Master(三维BFS)

和迷宫问题区别不大,相比于POJ1321的棋盘问题,这里的BFS是三维的,即从4个方向变为6个方向。

用上队列的进出操作较为轻松。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;

char map[35][35][35];
int vis[35][35][35];
int to[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
int k,n,m,sx,sy,sz,ex,ey,ez;
struct node
{
    int x,y,z,step;
};
int check(int x,int y,int z)
{
    if(x<0||y<0||z<0||x>=k||y>=n||z>=m||map[x][y][z]==‘#‘||vis[x][y][z])
        return 1;
    return 0;
}
int bfs()
{
    int i;
    node a,next;
    queue<node> Q;
    a.x = sx,a.y= sy,a.z = sz;
    a.step = 0;
    vis[sx][sy][sz]=1;
    Q.push(a);
    while(!Q.empty())
    {
        a=Q.front();
        Q.pop();
        if(a.x==ex&&a.y==ey&&a.z==ez)
            return a.step;
        for(i=0;i<6;i++)
        {
            next=a;
            next.x=a.x+to[i][0];
            next.y=a.y+to[i][1];
            next.z=a.z+to[i][2];
            if(check(next.x,next.y,next.z))
                continue;
            vis[next.x][next.y][next.z]=1;
            next.step=a.step+1;
            Q.push(next);
        }
    }
    return 0;
}
int main()
{
    int i,j,r;
    while(scanf("%d%d%d",&k,&n,&m),n+m+k)
    {
        for(i=0;i<k;i++)
        {
            for(j=0;j<n;j++)
            {
                scanf("%s",map[i][j]);
                for(r=0;r<m;r++)
                {
                    if(map[i][j][r]==‘S‘)
                    {
                        sx=i,sy=j,sz=r;
                    }
                    else if(map[i][j][r]==‘E‘)
                    {
                        ex=i,ey=j,ez=r;
                    }
                }
            }
        }
        memset(vis,0,sizeof(vis));
        int ans;
        ans=bfs();
        if(ans)
            printf("Escaped in %d minute(s).\n",ans);
        else
            printf("Trapped!\n");
    }
    return 0;
}
时间: 2024-08-06 16:06:07

POJ2251——Dungeon Master(三维BFS)的相关文章

POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层的地图,相同RC坐标处是相连通的.(.可走,#为墙) 解题思路:从起点开始分别往6个方向进行BFS(即入队),并记录步数,直至队为空.若一直找不到,则困住. /* POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路) */ #include <cstdio> #i

POJ 2251:Dungeon Master(三维BFS)

Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16178 Accepted: 6268 Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled wit

poj 2251 Dungeon Master(三维BFS)(中等)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20598   Accepted: 7971 Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled

ZOJ 1940 Dungeon Master 三维BFS

Dungeon Master Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with r

POJ:Dungeon Master(三维bfs模板题)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16748   Accepted: 6522 Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled

poj2251:Dungeon Master

最初没有注意到结果是要求最小的步数,那么就成了最基本的迷宫找到一条出路的问题并记下找到出路时,所花的步数,那么很容易得到代码如下: 1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 using namespace std; 5 6 #define MAX 1000000 7 int l,r,c,coun; 8 char m[30][30][30]; 9 bool flag[30][30]

POJ 2251 Dungeon Master(三维6方向BFS)

B - Dungeon Master Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2251 Appoint description:  System Crawler  (2015-03-28) Description You are trapped in a 3D dungeon and need to find the quicke

UVA 532- Dungeon Master(三维bfs)

Dungeon Master Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be fille

POJ 2251:Dungeon Master【bfs】

Dungeon Master Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submission(s) : 9   Accepted Submission(s) : 7 Problem Description You are trapped in a 3D dungeon and need to find the quickest way out! The dunge

uva 532 Dungeon Master(BFS)

uva 532 Dungeon Master You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. Y