AcWing 1096. 地牢大师(三维BFS)

输入样例:

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

#####
#####
##.##
##...

#####
#####
#.###
####E

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

0 0 0

输出样例:

Escaped in 11 minute(s).
Trapped!

 

#include<bits/stdc++.h>
using namespace std;
const int N=100+5;
int l,n,m,sx,sy,sz,ex,ey,ez;
char ch[N][N][N];
int vis[N][N][N];
struct node{
	int x,y,z,w;
};
int dir[6][3]={
	1,0,0,
	-1,0,0,
	0,1,0,
	0,-1,0,
	0,0,1,
	0,0,-1
};
int check(int x,int y,int z){
	return x>0&&y>0&&z>0&&x<=l&&y<=n&&z<=m;
}
void bfs(){
	memset(vis,0,sizeof vis);
	queue<node>q;
	node start={sx,sy,sz,0};
	node next;
	vis[start.x][start.y][start.z]=1;
	q.push(start);
	while(q.size()){
		start=q.front();
		q.pop();
		if(start.x==ex&&start.y==ey&&start.z==ez){
			printf("Escaped in %d minute(s).\n",start.w);
			return;
		}
		for(int i=0;i<6;i++){
			next.x=start.x+dir[i][0];
			next.y=start.y+dir[i][1];
			next.z=start.z+dir[i][2];
			if(check(next.x,next.y,next.z)&&ch[next.x][next.y][next.z]!='#'&&!vis[next.x][next.y][next.z]){
				next.w=start.w+1;
				vis[next.x][next.y][next.z]=1;
				q.push(next);
			}
		}
	}
	cout<<"Trapped!"<<endl;
}
int main(){
	while(cin>>l>>n>>m){
		if(l==0&&n==0&&m==0) break;
		for(int i=1;i<=l;i++){
			for(int j=1;j<=n;j++){
				for(int k=1;k<=m;k++){
					cin>>ch[i][j][k];
					if(ch[i][j][k]=='S') sx=i,sy=j,sz=k;
					else if(ch[i][j][k]=='E') ex=i,ey=j,ez=k;
				}
			}
		}
		bfs();
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值