Dungeon Master POJ - 2251 BFS求最短路 三维BFS

题目链接 

PS:POJ玄学CE(我猜是不支持初始化列表?懒得改了),UVA上过了。

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
struct node{
	int l,x,y,step;
}Node,t;
int l,r,c,ans;
int dr[][3]={
			{0,-1,0},
			{0,1,0},
			{0,0,-1},
			{0,0,1},
			{-1,0,0},
			{1,0,0}
};
char a[35][35][35];
bool inq[35][35][35];
bool check(int h,int x,int y){
	if(x<0||x>=r||y<0||y>=c||h<0||h>=l||a[h][x][y]=='#'||inq[h][x][y])return false;
	return true;
}
int BFS(){

	queue<node>q;
	q.push(Node);
	while(!q.empty()){
		node top=q.front();
		q.pop();
		//cout<<top.l<<" "<<top.x<<" "<<top.y<<" "<<top.step<<endl;
		if(top.l==t.l&&top.x==t.x&&top.y==t.y) {
			return top.step;
		}
		for(int i=0;i<6;++i){
			int newL=top.l+dr[i][0];
			int newX=top.x+dr[i][1];
			int newY=top.y+dr[i][2];
			if(check(newL,newX,newY)){
				inq[newL][newX][newY]=true;
				q.push({newL,newX,newY,top.step+1});
			}
		}
	}
	return 0;
}
int main(){
	while(cin>>l>>r>>c&&l){
		ans=0;
		memset(a,0,sizeof(a));
		memset(inq,0,sizeof(inq));
		for(int i=0;i<l;++i){
			for(int j=0;j<r;++j){
				cin>>a[i][j];
			}
		}
//		for(int i=0;i<l;++i){
//			for(int j=0;j<r;++j){
//				cout<<a[i][j]<<endl;
//			}
//		}
		bool flag=0;

		for(int i=0;i<l&&(!flag);++i){
			for(int j=0;j<r&&(!flag);++j){
				for(int k=0;k<c&&(!flag);++k){
					//cout<<"t? "<<a[i][j][k]<<endl;
					if(a[i][j][k]=='E'){
						t.l=i,t.x=j,t.y=k;
						//cout<<"t "<<i<<j<<k<<endl;
						flag=1;
					}
				}
			}
		}

		flag=0;
		for(int i=0;i<l&&(!flag);++i){
			for(int j=0;j<r&&(!flag);++j){
				for(int k=0;k<c&&(!flag);++k){
					if(a[i][j][k]=='S'){
						Node.x=j,Node.y=k,Node.l=i;
						flag=1;
						inq[i][j][k]=true;
						ans=BFS();
					}
				}
			}
		}
		if(!ans)cout<<"Trapped!\n";
		else cout<<"Escaped in "<<ans<<" minute(s).\n";
	}

}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值