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?
你被困在一个3D地牢里,需要找到最快捷的出路!地牢由单位立方体组成,可以填充或不填充岩石。向北,向东,向西,向上或向下移动一个单位需要一分钟。你不能对角移动,迷宫的四周都是坚硬的岩石。
逃脱可能吗?如果是,需要多长时间?
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.
输入包含许多地下城。每个地下城描述都以包含三个整数L,R和C(全部限制为30个大小)的行开头。
L是组成地牢的等级数。
R和C是构成每个级别计划的行数和列数。
然后将跟随L个R行,每行包含C个字符。每个角色描述一个地牢的一个单元格。充满岩石的细胞用‘#‘表示,空细胞用‘‘‘表示。您的起始位置用‘S‘表示,出口用‘E‘表示。每个级别后面都有一个空白行。对于L,R和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!
每个迷宫产生一行输出。如果可以到达出口,请打印表格的一行
在x分钟内逃脱。
其中x被逃逸所需的最短时间所取代。
如果无法逃脱,请打印该线
被困!
3 4 5
S....
.###.
.##..
###.#
#####
#####
##.##
##...
#####
#####
#.###
####?
1 3 3
S##
·E·
###
0 0 0
Escaped in 11 minute(s).
Trapped!
在11分钟后逃脱。
被困!
#include <iostream>
#include "stdio.h"
#include "string.h"
#include "queue"
using namespace std;
#define N 35
char map[N][N][N];//存入地图
int mark[N][N][N];//
int l,r,c;//记录3d迷宫的长 宽 高
int i,j,k;//for循环变量
int si,sj,sk;//储存起始点的坐标
int ei,ej,ek;//储存终止点的坐标
int dx[6]={1,-1,0,0,0,0};//
int dy[6]={0,0,1,-1,0,0};//6种行走方向
int dz[6]={0,0,0,0,1,-1};//
struct ac{
int x,y,z,step;//储存实时的坐标及步数
};
int judge(int x,int y,int z){//判断条件,过界,是否为路可走;
if (x>=0&&x<l&&y>=0&&y<r&&z>=0&&z<c&&map[x][y][z]!='#')
return 1;
return 0;
}
int bfs(int x,int y,int z){//bfs宽度(层次)搜索,借用队列
int i,di,dj,dk;
queue<ac>qu;
ac cur,next;//cur 记录当前步数 next 记录变化后的坐标
cur.x=x;
cur.y=y;
cur.z=z;
cur.step=0;//初始化步数
qu.push(cur);//向队列中存入初始坐标,第一个点
mark[x][y][z]=1;//将第一个坐标标记;
while (!qu.empty()) {//判断队列是否为空
cur=qu.front();//记录当前初始点
qu.pop();//清空当前队列
if (cur.x==ei&&cur.y==ej&&cur.z==ek)//判断是否结束,结束返回步数
return cur.step;
for (i=0; i<6; i++) {//尝试6种方向
next.x=di=cur.x+dx[i];
next.y=dj=cur.y+dy[i];
next.z=dk=cur.z+dz[i];
next.step=cur.step+1;
if(judge(di,dj,dk)&&!mark[di][dj][dk]) {//判断是否过界(judge)是否走过;
qu.push(next);//向队列中存入变化坐标
mark[di][dj][dk]=1;//标记走过的点
}
}
}
return -1;//不能逃脱,返回-1
}
int main() {
while (scanf("%d%d%d",&l,&r,&c),l||r||c) {//输入及输入结束条件
for (i=0; i<l; i++)
for (j=0; j<r; j++) {
scanf("%s",map[i][j]);//记录地图
for (k=0; k<c; k++) {
if (map[i][j][k]=='S') {
si=i;sj=j;sk=k;//记录起始点
}else if(map[i][j][k]=='E'){
ei=i;ej=j;ek=k;//记录终止点
}
}
}
memset(mark, 0, sizeof(mark));//标记数组初始化,(队列初始化);
int ans=bfs(si,sj,sk);
if (ans==-1) cout<<"Trapped!\n";
else printf("Escaped in %d minute(s).\n",ans);
}
return 0;
}
原文:https://www.cnblogs.com/minikk0220/p/10838095.html