B - Dungeon Master POJ2251 三维的图 进行搜索,注意三维图的读入细节


B - Dungeon Master
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit Status Practice POJ 2251

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!


//类比二维的图的搜索即可,但是注意三维图的存储细节

#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<queue>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=100;
const int inf=210000;
typedef long long ll;
int x,y,z;
char  a[maxn][maxn][maxn];
bool  vis[maxn][maxn][maxn];
int d[maxn][maxn][maxn];
struct node
{
    int x,y,z;
    node(int a,int b,int c){x=a;y=b;z=c;}
    node(){}
};
node end;
int xx[]={-1,0,0,1,0,0},yy[]={0,-1,0,0,1,0},zz[]={0,0,-1,0,0,1};
void bfs(int sx,int sy,int sz)
{
    node tmp=node(sx,sy,sz);
    queue<node> q;
    d[sx][sy][sz]=0;
    q.push(tmp);
    vis[sx][sy][sz]=true;
    while(!q.empty())
    {
        tmp=q.front();
        q.pop();
        if(tmp.x==end.x&&tmp.y==end.y&&tmp.z==end.z){return;}
        for(int i=0;i<6;i++)
        {
            int dx=tmp.x+xx[i];
            int dy=tmp.y+yy[i];
            int dz=tmp.z+zz[i];
            if(dx<x&&dx>=0&&dy<y&&dy>=0&&dz<z&&dz>=0&&!vis[dx][dy][dz]&&a[dx][dy][dz]!='#')
            {
                vis[dx][dy][dz]=true;
                q.push(node(dx,dy,dz));
                d[dx][dy][dz]=d[tmp.x][tmp.y][tmp.z]+1;
            }
        }
    }
}
int main()
{
    int m,i,j,t,k;
    //freopen("in.txt","r",stdin);
    while(cin>>z>>x>>y&&(x||y||z))
    {
        int sx,sy,sz;
        for(i=0;i<z;i++)
        for(j=0;j<x;j++)
        for(k=0;k<y;k++)
        {
            cin>>a[j][k][i];
            if(a[j][k][i]=='S')
            {
                sx=j;
                sy=k;
                sz=i;
            }
            else if(a[j][k][i]=='E')
            {
                end.x=j;
                end.y=k;
                end.z=i;
            }
        }
        //cout<<sx<<' '<<sy<<' '<<sz<<endl;
      //  cout<<end.x<<' '<<end.y<<' '<<end.z<<endl;
        memset(vis,0,sizeof(vis));
        memset(d,0,sizeof(d));
        bfs(sx,sy,sz);
        if(d[end.x][end.y][end.z]==0)
            printf("Trapped!\n");
        else
            printf("Escaped in %d minute(s).\n",d[end.x][end.y][end.z]);
    }
    return 0;
}















你可能感兴趣的:(master)