当前位置 : 主页 > 编程语言 > c语言 >

POJ 2251 Dungeon Master(三维BFS)

来源:互联网 收集:自由互联 发布时间:2023-09-07
Dungeon Master Time Limit:1000MSMemory Limit:65536KB64bit IO Format:%I64d %I64u Submit Status Practice POJ 2251 System Crawler (2014-09-08) Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co


Dungeon Master


Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u


Submit  Status  Practice  POJ 2251


System Crawler  (2014-09-08)



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 rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides. 

Is an escape possible? If yes, how long will it take? 



Input



The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size). 
L is the number of levels making up the dungeon. 
R and C are the number of rows and columns making up the plan of each level. 
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.



Output



Each maze generates one line of output. If it is possible to reach the exit, print a line of the form 

Escaped in x minute(s).



where x is replaced by the shortest time it takes to escape. 


If it is not possible to escape, print the line 


Trapped!



Sample Input



3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0



Sample Output



Escaped in 11 minute(s). Trapped!






题目挂出来有一段时间了,就是看不懂这个题要干什么,就一直看,终于发现这其实就是一个三维的BFS而已,又一个跌落神坛的题目......



题目大意:  给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径

移动方向可以是上,下,左,右,前,后,六个方向

每移动一次就耗费一分钟,要求输出最快的走出时间。 不同L层的地图,相同RC坐标处是连通的



AC代码



#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>

using namespace std;

struct node
{
    int x,y,z;
    int count;
} q[25000001];

char map[33][33][33];
int v[33][33][33];
int n,m,k;
int jx[] = {0,0,0,0,1,-1};
int jy[] = {0,0,-1,1,0,0};
int jz[] = {1,-1,0,0,0,0};

void BFS(int x,int y,int z,int xx,int yy,int zz)
{
    int s = 0,e = 0;
    int i;
    struct node t,f;
    memset(v,0,sizeof(v));
    t.x = x;
    t.y = y;
    t.z = z;
    t.count = 0;
    v[x][y][z] = 1;
    q[e++] = t;
    while(s < e)
    {
        t = q[s++];

        if(map[t.x][t.y][t.z] == 'E')
        {
            printf("Escaped in %d minute(s).\n",t.count);
            return ;
        }
        for(i=0;i<6;i++)
        {
            f.x = t.x + jx[i];
            f.y = t.y + jy[i];
            f.z = t.z + jz[i];
            if(f.x>=0 && f.x<n && f.y>=0 && f.y<m && f.z>=0 && f.z<k && (map[f.x][f.y][f.z] == '.'||map[f.x][f.y][f.z] == 'E' )&& v[f.x][f.y][f.z] == 0)
            {
                f.count = t.count + 1;
                q[e++] = f;
                v[f.x][f.y][f.z] = 1;
            }
        }
    }
    printf("Trapped!\n");
}

int main()
{
    int i,j,r;
    while(scanf("%d%d%d",&n,&m,&k)!=EOF)
    {
        if(n == 0 && m == 0 && k == 0)
        {
            break;
        }
        int flag = 0;
        int a1,a2,b1,b2,c1,c2;
        for(i=0; i<n; i++)
        {
            for(j=0; j<m; j++)
            {
                scanf("%s",map[i][j]);
                if(flag<2)
                {
                    for(r=0; r<k; r++)
                    {
                        if(map[i][j][r] == 'S')
                        {
                            a1 = i;
                            b1 = j;
                            c1 = r;
                            flag++;
                        }
                        else if(map[i][j][r] == 'E')
                        {
                            a2 = i;
                            b2 = j;
                            c2 = r;
                            flag++;
                        }
                    }
                }

            }
        }
        BFS(a1,b1,c1,a2,b2,c2);

    }
    return 0;
}




上一篇:懒虫小鑫
下一篇:没有了
网友评论