码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3279 Dungeon Master

时间:2015-08-19 22:35:04      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:

Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 21242   Accepted: 8265

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 注意xyz别弄混了
/* ***********************************************
Author        :pk29
Created Time  :2015/8/19 18:40:40
File Name     :4.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 10000+10
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;
struct node{
    int x,y,z;
    int dist;
};
bool cmp(int a,int b){
    return a>b;
}
int l,r,c;
char mp[50][50][50];
int vis[50][50][50];
int sx,sy,sz,ex,ey,ez;
int dir[6][3]={0,1,0,0,0,1,0,0,-1,1,0,0,-1,0,0,0,-1,0};

void bfs(){
    node u,v;
    u.x=sx,u.y=sy,u.z=sz, u.dist=0;
    queue<node>q;
    q.push(u);
    int mark=0;
    vis[u.x][u.y][u.z]=1;
    while(!q.empty()){
        u=q.front(),q.pop();
        if(u.x==ex&&u.y==ey&&u.z==ez){
            mark=1;printf("Escaped in %d minute(s).\n",u.dist);break;
        }
        for(int i=0;i<6;i++){
            int nx=u.x+dir[i][0];
            int ny=u.y+dir[i][1];
            int nz=u.z+dir[i][2];
            if(!vis[nx][ny][nz]&&mp[nx][ny][nz]!=#&&nx>=1&&ny>=1&&nz>=1&&nx<=r&&ny<=c&&nz<=l){
                v.x=nx,v.y=ny,v.z=nz;
                v.dist=u.dist+1;
                q.push(v);
                vis[nx][ny][nz]=1;
            }
        }
    }
    if(!mark)printf("Trapped!\n");
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif
    //freopen("out.txt","w",stdout);
    while(cin>>l>>r>>c){
        if(l==0&&c==0&&r==0)break;
        for(int i=1;i<=l;i++)
            for(int j=1;j<=r;j++)
                for(int k=1;k<=c;k++){
                    cin>>mp[j][k][i];
                    if(mp[j][k][i]==S)sx=j,sy=k,sz=i;
                    if(mp[j][k][i]==E)ex=j,ey=k,ez=i;
                }
        cle(vis);
        //cout<<sx<<sy<<sz<<endl;
        //cout<<ex<<ey<<ez<<endl;

        bfs();
    }
    return 0;
}

 

POJ 3279 Dungeon Master

标签:

原文地址:http://www.cnblogs.com/pk28/p/4743157.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!