poj 2251 三维BFS 无坑点

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
char map[35][35][35];
bool vis[35][35][35];
int t,r,c;
int dirx[6]={ 1,-1, 0, 0, 0, 0};  #多加两个dir
int diry[6]={ 0, 0, 1,-1, 0, 0};
int dirz[6]={ 0, 0, 0, 0, 1,-1};
struct loc
{
	int x,y,z;
	int step;
};
int main()
{
	while(scanf("%d%d%d",&t,&r,&c)==3)
	{
		if(t==0 && r==0 && c==0) break;
		int sx,sy,sz;
		memset(map,0,sizeof(map));
		memset(vis,0,sizeof(vis));
		for(int i=1;i<=t;i++)
		{
			for(int j=1;j<=r;j++)
			{
				for(int k=1;k<=c;k++)
				{
					cin >> map[i][j][k];
					if(map[i][j][k]=='S')
					{
						sx=i;
						sy=j;
						sz=k;
					}
				}
			}
		}
//		cout << 1 << endl;
		queue<loc> q;
		loc point;
		point.x=sx;
		point.y=sy;
		point.z=sz;
		point.step=0;
//		cout << "*"<< point.x <<","<<point.y<<","<<point.z<<endl;
		q.push(point);
		int ans=0;
		while(!q.empty())
		{
			if(ans) break;
			loc tmp=q.front();
			q.pop();
			for(int i=0;i<6;i++)
			{
				
				loc nextstep=tmp;
				nextstep.x+=dirx[i];
				nextstep.y+=diry[i];
				nextstep.z+=dirz[i];
//				cout << nextstep.x <<','<<nextstep.y<<','<<nextstep.z<<endl;
				if(vis[nextstep.x][nextstep.y][nextstep.z]) continue;
				if(nextstep.x<1 || nextstep.x>t) continue;
				if(nextstep.y<1 || nextstep.y>r) continue;
				if(nextstep.z<1 || nextstep.z>c) continue;
				if(map[nextstep.x][nextstep.y][nextstep.z]=='#') continue;
				if(map[nextstep.x][nextstep.y][nextstep.z]=='E')
				{
					ans=nextstep.step+1;
					break;
				}
				vis[nextstep.x][nextstep.y][nextstep.z]=1;
				if(ans) break;
				nextstep.step++;
				q.push(nextstep);
			}
		}
		if(ans)
		{
			printf("Escaped in %d minute(s).\n",ans);
		}
		else 
		{
			printf("Trapped!\n");
		}
		
	}
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值