poj 2251 Dungeon Master(BFS)

简介:
Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 21394   Accepted: 8312

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

题目大意:首先给你三个数l,r,c;分别表示有l个平面,r行,c列,我们需要从 S 走到 E ,可以通过6个方向行走(即上下东西南北),看是否能够走到,如果能的话,输出最小的分钟数;否则输出Trapped!;

解题思路:首先声明一点,如果遇到最短路的话,首先考虑的是BFS,然后就是固定的套路了。。。

上代码:

/*
Date : 2015-09-08 晚上

Author : ITAK

Motto :

今日的我要超越昨日的我,明日的我要胜过今日的我;
以创作出更好的代码为目标,不断地超越自己。
*/
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

///最短路,用DFS一定很难做出来,一定要BFS

const int maxn = 30000;

struct Point
{
    int x, y, z;
}q[maxn];

int len[maxn];
int xx[6] = {-1,1,0,0,0,0};
int yy[6] = {0,0,-1,1,0,0};
int zz[6] = {0,0,0,0,-1,1};
bool vis[35][35][35];
char map[35][35][35];
int l,r,c,sx,sy,sz;

int bfs()
{
    int rear, front, dx, dy, dz;
    memset(vis, false, sizeof(vis));
    memset(len, 0, sizeof(len));
    q[0].x=sx, q[0].y=sy, q[0].z=sz;
    front = rear = 0;
    while(front <= rear)
    {
        for(int i=0; i<6; i++)
        {
            dx = q[front].x + xx[i];
            dy = q[front].y + yy[i];
            dz = q[front].z + zz[i];
            if(!vis[dx][dy][dz] && (map[dx][dy][dz]=='.'||map[dx][dy][dz]=='E')
               && dx>=0 && dx<l && dy>=0 && dy<r && dz>=0 && dz<c)
               {
                 vis[dx][dy][dz] = 1;
                 rear++;
                 q[rear].x = dx;
                 q[rear].y = dy;
                 q[rear].z = dz;
                 len[rear] = len[front]+1;
                 if(map[dx][dy][dz] == 'E')
                    return len[rear];
               }
        }
        front++;
    }
    return 0;
}
int main()
{
     while(cin>>l>>r>>c)
     {
         if(l==0 && r==0 && c==0)
            break;
         for(int i=0; i<l; i++)
         {
             for(int j=0; j<r; j++)
             {
                 for(int k=0; k<c; k++)
                 {
                     cin>>map[i][j][k];
                     if(map[i][j][k] == 'S')
                        sx=i, sy=j, sz=k;
                 }
             }
         }
         int ret = bfs();
         if(ret)
            printf("Escaped in %d minute(s).\n",ret);
         else
            puts("Trapped!");
     }
    return 0;
}


目录
相关文章
Dungeon Master(BFS广度优先搜索)
Dungeon Master(BFS广度优先搜索)
40 0
|
定位技术 ice
POJ-3009,Curling 2.0(DFS)
POJ-3009,Curling 2.0(DFS)
POJ-2251,Dungeon Master(三维迷宫BFS)
POJ-2251,Dungeon Master(三维迷宫BFS)
|
机器学习/深度学习
POJ-1321,棋盘问题(DFS)
POJ-1321,棋盘问题(DFS)
洛谷P1331-海战(简单的DFS)
洛谷P1331-海战(简单的DFS)
|
移动开发 JavaScript
POJ 2676 Sudoku (数独 DFS)
Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14368   Accepted: 7102   Special Judge   Description Sudoku is a very simple task.
1161 0
poj 1562 dfs
http://poj.org/problem?id=1562 #include using namespace std; int n=0,m=0,sum=0; bool aa[105][105]; int dir[8][2]={-1,0, 1,0, ...
689 0
POJ 1979 DFS
题目链接:http://poj.org/problem?id=1979 #include #include using namespace std; int n=0,h=0,sum=0; char aa[21][21]; void DFS(int p,int q) { if(aa[p][q]=='.
751 0