Dungeon Master

来源:互联网 发布:windows 与ntp同步 编辑:程序博客网 时间:2024/06/05 17:02

Dungeon Master

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 76   Accepted Submission(s) : 26
Problem 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). <br>L is the number of levels making up the dungeon. <br>R and C are the number of rows and columns making up the plan of each level. <br>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 <br><blockquote>Escaped in x minute(s). </blockquote> <br>where x is replaced by the shortest time it takes to escape. <br>If it is not possible to escape, print the line <br><blockquote>Trapped! </blockquote>
 

Sample Input
3 4 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0
 
Sample Output
Escaped in 11 minute(s).Trapped!
题目意思:有一个三维的空间 S是起点 E是终点 #是障碍 .是可行的地方
问你能否可以出去 如果出去的最小时间是多少
思路:典型的bfs
之前刷steps的时候做过一道三维的题目 方向多了点而已
但是这个题的输入竟然把我卡住了 每一次都有一个换行
交上试了试 果然wa
百思不得其解 看一个牛人的代码
搜索的过程跟我没什么两样 完全就是输入的问题
他加了三个getchar() 在循环中
就copy了那么一下
还有就是这种 更加巧妙
因为在每一面输入完成后有一个空行
这个方法就巧妙地避开干扰
    while(scanf("%d%d%d",&l,&w,&h))//http://blog.csdn.net/libin56842/article/details/23702395    {        if(l==0&&w==0&&h==0)break;        memset(vis,0,sizeof(vis));        //getchar();        for(i=0;i<l;i++)        {            for(j=0;j<w;j++)            {                    scanf("%s",&map[i][j]);                    for(k=0;k<h;k++)                    {                        if(map[i][j][k]=='S')                    {                        startx=i,starty=j,startz=k;                    }                    else if(map[i][j][k]=='E')                    {                        endx=i,endy=j,endz=k;                    }                    }               // getchar();            }            //getchar();        }
#include<iostream>#include<stdio.h>#include<string.h>#include<queue>using namespace std;bool vis[31][31][31];int dx[]={0,0,0,0,1,-1};int dy[]={-1,1,0,0,0,0};int dz[]={0,0,1,-1,0,0};char map[31][31][31];int n;int startx,starty,startz,endx,endy,endz;int l,w,h;struct node{    int x;    int y;    int z;    int step;    };int judge(int x,int y,int z){    if(x>=0&&y>=0&&z>=0&&x<l&&y<w&&z<=h)    return 1;    else return 0;    }int bfs(){    int i,j;    queue<node>Q;    node now,next;    while(!Q.empty())    Q.pop();    now.x=startx;    now.y=starty;    now.z=startz;    now.step=0;    vis[startx][starty][startz]=1;    Q.push(now);    while(!Q.empty())    {        now=Q.front();        Q.pop();        if(now.x==endx&&now.y==endy&&now.z==endz)        return now.step;        for(i=0;i<6;i++)        {            next.x=now.x+dx[i];            next.y=now.y+dy[i];            next.z=now.z+dz[i];            if(judge(next.x,next.y,next.z)==1&&!vis[next.x][next.y][next.z]&&map[next.x][next.y][next.z]!='#')            {                vis[next.x][next.y][next.z]=1;                next.step=now.step+1;                Q.push(next);            }        }    }    return -1;}int main(){    int i,j,k,ans;    while(scanf("%d%d%d",&l,&w,&h))    {        if(l==0&&w==0&&h==0)break;        memset(vis,0,sizeof(vis));        getchar();        for(i=0;i<l;i++)        {            for(j=0;j<w;j++)            {                for(k=0;k<h;k++)                {                    scanf("%c",&map[i][j][k]);                    if(map[i][j][k]=='S')                    {                        startx=i,starty=j,startz=k;                    }                    else if(map[i][j][k]=='E')                    {                        endx=i,endy=j,endz=k;
                    }                }                getchar();            }            getchar();        }        ans=bfs();        if(ans==-1)        cout<<"Trapped!"<<endl;        else        cout<<"Escaped in "<<ans<<" minute(s)."<<endl;    }    return 0;}

原创粉丝点击