Description - 题目描述
Input - 输入
输入的第一行包含一个数,表示地牢的数量。
每个地牢的描述,其第一行包含三个数L,R和C(均小于等于30)。
L表示地牢的层数;R和C分别表示每层地牢的行与列的大小。
随后输入地牢的层数L,每层中包含R行,每行中包含C个字符。
每个字符表示地牢的一个单元。'#'表示岩石单元,'.'表示空白单元。你的起始位置在点'S',出口为'E'。
每层地牢的输入后都有一个空行。当L,R和C均为0时,输入结束。
Output - 输出
每个迷宫对应一行输出。
如果可以逃生,则输出如下
Escaped in x minute(s).
x为最短脱离时间。
如果无法逃生,则输出如下
Trapped!
Sample Input - 输入样例
Sample Output - 输出样例
你被困在一个3D地牢中且继续寻找最短路径逃生!地牢由立方体单位构成,立方体单位中有的会充满岩石。向上下前后左右移动一个单位需要一分钟。你不能向对角线的四个方向移动且迷宫四周环绕着许多岩石。
是否可以逃出地牢?如果可以,则需要多少时间?
是否可以逃出地牢?如果可以,则需要多少时间?
每个地牢的描述,其第一行包含三个数L,R和C(均小于等于30)。
L表示地牢的层数;R和C分别表示每层地牢的行与列的大小。
随后输入地牢的层数L,每层中包含R行,每行中包含C个字符。
每个字符表示地牢的一个单元。'#'表示岩石单元,'.'表示空白单元。你的起始位置在点'S',出口为'E'。
每层地牢的输入后都有一个空行。当L,R和C均为0时,输入结束。
如果可以逃生,则输出如下
Escaped in x minute(s).
x为最短脱离时间。
如果无法逃生,则输出如下
Trapped!
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Escaped in 11 minute(s). Trapped!
分析:
BFS,因为是三维的,所以搜索方向为6个
code:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;
char maps[35][35][35];
int vis[35][35][35];
int df[6][3]={0,1,0,0,-1,0,1,0,0,-1,0,0,0,0,1,0,0,-1};
int l,r,c,sx,sy,sz,ex,ey,ez;
struct node{
int x,y,z,step;
};
int main(){
int i,j,k;
while(scanf("%d%d%d",&l,&r,&c)){
if(l==0&&r==0&&c==0)
break;
for(i=0;i<l;i++){
for(j=0;j<r;j++){
scanf("%s",maps[i][j]);
for(k=0;k<c;k++){
if(maps[i][j][k]=='S'){
sx=i;sy=j;sz=k;
}
if(maps[i][j][k]=='E'){
ex=i;ey=j;ez=k;
}
}
}
}
memset(vis,0,sizeof(vis));
int ans=0;
queue<node> que;
node a,next;
a.x=sx; a.y=sy; a.z=sz; a.step=0;
que.push(a);
vis[a.x][a.y][a.z]=1;
while(!que.empty()){
a=que.front();
que.pop();
if(a.x==ex&&a.y==ey&&a.z==ez){ ans=a.step; break; }
for(i=0;i<6;i++){
next=a;
next.x=a.x+df[i][0];
next.y=a.y+df[i][1];
next.z=a.z+df[i][2];
if(next.x<0||next.y<0||next.z<0||next.x>=l||next.y>=r||next.z>=c||vis[next.x][next.y][next.z]==1||maps[next.x][next.y][next.z]=='#')
continue;
vis[next.x][next.y][next.z]==1;
next.step=a.step+1;
que.push(next);
}
}
if(ans)
printf("Escaped in %d minute(s).\n",ans);
else
printf("Trapped!\n");
}
return 0;
}