地牢手册-3d

Description

你进入了一个3D的宝藏地宫中探寻到了宝藏,你可以找到走出地宫的路带出宝藏,或者使用炉石空手回家。
地宫由立方体单位构成,立方体中不定会充满岩石。向上、下、前、后、左、右移动一个单位需要一分钟。你不能对角线移动并且地宫四周坚石环绕。
请问你是否可以走出地宫带出宝藏?如果存在,则需要多少时间?

Input

地宫描述的第一行为L,R和C(皆不超过3030)。
L表示地宫的层数。
R和C分别表示每层地宫的行与列的大小。
随后L层地宫,每层R行,每行C个字符。
每个字符表示地宫的一个单元。'#'表示岩石单元,′.′′.′表示空白单元。你的起始位置在′S′,出口为′E′。

Output

如果可以带出宝藏,则输出
Escaped in x minute(s).
x为最短脱离时间。
如果无法带出,则输出
Trapped!

Sample Input 1 

3 4 5
S....
.###.
.##..
###.#
#####
#####
##.##
##...
#####
#####
#.###
####E

Sample Output 1

Escaped in 11 minute(s).

Sample Input 2 

1 3 3
S##
#E#
###

Sample Output 2

Trapped!

AC:

#include
#include
#include
#include
#include
#include
using namespace std;
int q[10005][55],r,c,l,as,asd,asdf;
char a[55][55][55];
int fx[6]={1,-1,0,0,0,0};
int fy[6]={0,0,1,-1,0,0};
int fz[6]={0,0,0,0,1,-1};
bool v[50][50][50],f;
void bfs(){
	int head=0;
	int tail=1;
	while(head=1&&xx<=l&&yy>=1&&yy<=r&&zz<=c&&zz>=1&&v[xx][yy][zz]==false&&a[xx][yy][zz]!='#'){
				v[xx][yy][zz]=true;
				tail++;
				q[tail][1]=xx;
				q[tail][2]=yy;
				q[tail][3]=zz;
				q[tail][4]=q[head][4]+1;
				if(xx==as&&yy==asd&&zz==asdf){
					cout<<"Escaped in "<>l>>r>>c;
	for(int i=1;i<=l;i++){
		for(int j=1;j<=r;j++){
			for(int k=1;k<=c;k++){
				cin>>a[i][j][k];
				if(a[i][j][k]=='S'){
					q[1][1]=i;
		            q[1][2]=j;
		            q[1][3]=k;
		            q[1][4]=1;
		            v[i][j][k]=true;
				}if(a[i][j][k]=='E'){
					as=i;
					asd=j;
					asdf=k;
				}
			}
		}
    }bfs();
	if(f==false){
		cout<<"Trapped!";
	}return 0;
}

你可能感兴趣的:(bfs&栈,算法)