NYOJ 353 3D dungeon(三维数组BFS)

来源:互联网 发布:睿医人工智能研究中心 编辑:程序博客网 时间:2024/04/29 01:48

3D dungeon

时间限制:1000 ms  |  内存限制:65535 KB
难度:2
描述
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? 
输入
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.
输出
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!
样例输入
3 4 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0
样例输出
Escaped in 11 minute(s).Trapped!

三维数组

ac代码:

#include<stdio.h>#include<string.h>#include<string>#include<queue>#include<iostream>using namespace std;struct node{    int x;    int y;    int z;    int step;//步数};char map[50][50][50];int v[50][50][50]; //标志int n,m,k,BZ,num;int dx[6]={0,0,0,0,1,-1};  int dy[6]={0,0,1,-1,0,0};  int dz[6]={1,-1,0,0,0,0};int check(struct node aa){    if(aa.x>=n||aa.x<0||aa.y>=m||aa.y<0||aa.z>=k||aa.z<0)    return 0;    if(map[aa.x][aa.y][aa.z]=='#'||v[aa.x][aa.y][aa.z])    return 0;    return 1;}void bfs(int xx,int yy,int zz){    queue<struct node>a;//创建队列    struct node now,temp;    now.x=xx;now.y=yy;now.z=zz;now.step=0;    v[xx][yy][zz]=1;    a.push(now);    while(!a.empty())    {        temp=a.front();        a.pop();        for(int i=0;i<6;i++)        {            now.x=temp.x+dx[i];            now.y=temp.y+dy[i];            now.z=temp.z+dz[i];            now.step=temp.step+1;            if(check(now))            {                if(map[now.x][now.y][now.z]=='E')                {                    BZ=1;                    num=now.step;                    return;                }                v[now.x][now.y][now.z]=1;//记录走过                a.push(now);            }        }    }}int main(){    int bx,by,bz;    while(scanf("%d%d%d",&n,&m,&k)!=EOF,n||m||k)    {        for(int i=0;i<n;i++)        {            for(int j=0;j<m;j++)            {            scanf("%s",map[i][j]);            for(int q=0;q<k;q++)               if(map[i][j][q]=='S')               {                   bx=i;                   by=j;                   bz=q;               }            }        }        BZ=0;        memset(v,0,sizeof(v));        bfs(bx,by,bz);        if(BZ)        printf("Escaped in %d minute(s).\n",num);        else        printf("Trapped!\n");    }    return 0;}




0 0