POJ 2251 Dungeon Master

来源:互联网 发布:mac里的照片怎么导出 编辑:程序博客网 时间:2024/06/08 00:04
Dungeon Master
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 26176 Accepted: 10198

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(投入) isterminated(终止) 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!深搜里的题目,但是感觉广搜做更好,就是一个裸的三维的bfs求最短路,第一发就AC了。
#include<iostream>#include<cstdio>#include<cstring>#include<queue>using namespace std;int n,k;long long sum;int a[33][33][33];bool vis[33][33][33];int xx[]={0,0,1,-1};int yy[]={1,-1,0,0};int l,r,c;struct node{    int x;    int y;    int z;    int ss;}lu[66666],st,ed,t,tt;char ch;char te[66]; int bfs() {    queue<node>Q;    Q.push(st);    vis[st.x][st.y][st.z]=1;    while(!Q.empty())    {        t=Q.front();        Q.pop();        if(t.x==ed.x&&t.y==ed.y&&t.z==ed.z) return t.ss;        for(int i=0;i<4;i++)        {            int nx=t.x+xx[i];            int ny=t.y+yy[i];            if(a[nx][ny][t.z]&&!vis[nx][ny][t.z])            {                vis[nx][ny][t.z]=1;                tt={nx,ny,t.z,t.ss+1};                Q.push(tt);            }        }        if(a[t.x][t.y][t.z+1]&&!vis[t.x][t.y][t.z+1])            {                vis[t.x][t.y][t.z+1]=1;                tt={t.x,t.y,t.z+1,t.ss+1};                Q.push(tt);            }            if(a[t.x][t.y][t.z-1]&&!vis[t.x][t.y][t.z-1])            {                vis[t.x][t.y][t.z-1]=1;                tt={t.x,t.y,t.z-1,t.ss+1};                Q.push(tt);            }    }    return -1; }int main(){    while(cin>>l>>r>>c&&(l||r||c))    {        memset(a,0,sizeof(a));        memset(vis,0,sizeof(vis));        gets(te);        for(int i=1;i<=l;i++)        {            for(int j=1;j<=r;j++)            {                for(int k=1;k<=c;k++)                {                    scanf("%c",&ch);                    if(ch!='#') a[i][j][k]=1;                    if(ch=='S') st={i,j,k,0};                    else if(ch=='E') ed={i,j,k,0};                }                gets(te);            }            getchar();        }       /* for(int i=1;i<=l;i++)        {            for(int j=1;j<=r;j++)            {                for(int k=1;k<=c;k++)                {                    cout<<a[i][j][k];                }                cout<<endl;            }            cout<<endl;        }*/        int sum=bfs();        if(sum==-1) cout<<"Trapped!"<<endl;        else printf("Escaped in %d minute(s).\n",sum);    }    return 0;}


0 0
原创粉丝点击