poj 2251 Dungeon Master

来源:互联网 发布:淘宝掌中宝是什么 编辑:程序博客网 时间:2024/04/26 05:47
Dungeon Master
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 36311 Accepted: 13837

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 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0

Sample Output

Escaped in 11 minute(s).Trapped!
题目大意:
这个题目是一个三维的空间图形,#不能通行,.可以通行,每走一步花费一分钟的时间,问从S到E最短需要多长时间,不能到达
输出
Trapped!
题目解析:
这是一个简单的BFS题
代码:
#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>#include<queue>using namespace std;const int maxn=35;char node[maxn][maxn][maxn];int vis[maxn][maxn][maxn];int dir[6][3]={{0,0,1},{0,0,-1},{1,0,0},{-1,0,0},{0,1,0},{0,-1,0}};int x,y,z;int ex,ey,ez,ok;struct trip{    int x;    int y;    int z;    int step;};int check(int p,int r,int s){    if(p<0||p>=x||r<0||r>=y||s<0||s>=z||node[p][r][s]=='#')        return 1;    return vis[p][r][s];}void BFS(int p,int r,int s){    trip a,next;    queue<trip>q;    a.x=p;    a.y=r;    a.z=s;    a.step=0;    q.push(a);    while(!q.empty())    {      a=q.front();      q.pop();      if(a.x==ex&&a.y==ey&&a.z==ez)         {             cout<<"Escaped in "<<a.step<<" minute(s)."<<endl;             ok=1;             break;         }            for(int i=0;i<6;i++)            {                next.x=a.x+dir[i][0];                next.y=a.y+dir[i][1];                next.z=a.z+dir[i][2];                if(check(next.x,next.y,next.z))                    continue;                    next.step=a.step+1;                   vis[next.x][next.y][next.z]=1;                   q.push(next);            }    }}int main(){    //freopen("in.txt","r",stdin);    while(cin>>x>>y>>z&&x&&y&&z)    {        ok=0;        memset(node,0,sizeof(node));        memset(vis,0,sizeof(vis));     for(int i=0;i<x;i++)            for(int j=0;j<y;j++)            cin>>node[i][j];    for(int i=0;i<x;i++)        for(int j=0;j<y;j++)            for(int k=0;k<z;k++)             if(node[i][j][k]=='E')             {                 ex=i;                 ey=j;                 ez=k;                 break;             }    for(int i=0;i<x;i++)        for(int j=0;j<y;j++)            for(int k=0;k<z;k++)                if(node[i][j][k]=='S') BFS(i,j,k);                if(!ok) cout<<"Trapped!"<<endl;    }    return 0;}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 法院调解书被告不执行怎么办 电视cas授权已过期怎么办 网贷逾期被仲裁怎么办 word所选内容无效怎么办 wps批注不显示了怎么办 船舶挂靠船务公司去要怎么办 被螃蟹扎了肿了怎么办 手指被海鲜刺了怎么办 被海鲜刺进去了怎么办 手被海鲜扎了怎么办 环评设备写错了怎么办 物业对高空抛物怎么办 如果法官不公平判案我该怎么办? 民事案夸省审理案件不公平怎么办 积目头像未通过审核怎么办 电压不稳空调带不动怎么办 孩子吃了塑料珠怎么办 美的电压力锅打不开盖子怎么办 瓶盖打不开怎么办 瓶子是玻璃的 杯子口关错位了怎么办 大玻璃瓶的玻璃瓶盖拧不开怎么办 按压式洗发水瓶盖打不开怎么办 向下按压的瓶盖打不开怎么办 玻璃罐头开过了打不开怎么办 玻璃瓶的塑料盖子打不开怎么办 泡酒玻璃瓶盖子打不开怎么办 罐头的塑料瓶盖打不开怎么办 塑料水杯盖紧了怎么办 拧不开矿泉水瓶盖怎么办 新暖壶盖吸住了怎么办 暖瓶盖被吸住了怎么办 做面包和面粘手怎么办 面包面和稀了怎么办 鱼缸氧气泵声音大怎么办 中班安全遇到火灾怎么办反思 汤洒了怎么办活动反思 下水道被塑料盖堵了怎么办 卫生间地漏盖子掉到下水道怎么办 洗手池下水道翻盖打不开了怎么办 培乐多彩泥吃了怎么办 超轻橡皮泥干了怎么办