poj 2251 Dungeon Master

来源:互联网 发布:sql从两个表中查询数据 编辑:程序博客网 时间:2024/05/10 15:55
Dungeon Master
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 20937 Accepted: 8115

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为目的地,只能前后左右上下六个方向走,'#'不能走'.'可以走,问能否将在E处的人救出,就广搜,队列啊
#include<cstdio>#include<cstring>#include<queue>using namespace std;char map[32][32][32];int vis[32][32][32];int l,r,c;int dirz[6]={1,-1,0,0,0,0};int dirx[6]={0,0,1,-1,0,0};int diry[6]={0,0,0,0,1,-1};struct node{    int x,y,z,t;};int judge(int x,int y,int z){    if(z<0||z>=l||x<0||x>=r||y<0||y>=c)        return 0;    if(vis[z][x][y])        return 0;    if(map[z][x][y]=='#')        return 0;    return 1;}void bfs(int z,int x,int y){    node ss,now,next;    ss.x=x;    ss.y=y;    ss.z=z;    ss.t=0;    vis[z][x][y]=1;    queue<node>q;    q.push(ss);    while(!q.empty())    {        now=q.front();        q.pop();        if(map[now.z][now.x][now.y]=='E')        {            printf("Escaped in %d minute(s).\n",now.t);            return ;        }        for(int i=0;i<6;++i)        {            next.z=now.z+dirz[i];            next.x=now.x+dirx[i];            next.y=now.y+diry[i];            if(judge(next.x,next.y,next.z))            {                next.t=now.t+1;                vis[next.z][next.x][next.y]=1;                q.push(next);            }        }    }    printf("Trapped!\n");}int main(){    int x,y,z;    while(scanf("%d%d%d",&l,&r,&c)&&(l||r||c))    {        memset(map,0,sizeof(map));        memset(vis,0,sizeof(vis));        for(int i=0;i<l;++i)        {            for(int j=0;j<r;++j)                scanf("%s",map[i][j]);        }        for(int i=0;i<l;++i)        {            for(int j=0;j<r;++j)            {                for(int k=0;k<c;++k)                {                    if(map[i][j][k]=='S')                    {                        z=i;                        x=j;                        y=k;                    }                }            }        }        bfs(z,x,y);    }    return 0;}

0 0
原创粉丝点击