简单搜索篇 Dungeon Master POJ - 225(三维BFS)

#include<iostream>
#include<algorithm>
#include<map>
#include<queue>
#include<cstring>
#include <cstdio>
#include <cmath>
#include <set>
#include<vector>
#include<cstring>
#define mod 998244353
#define maxn 100
using namespace std;
int ex,ey,ez;
int f=1;
int dir[10][10]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
int bx,by,bz;
struct node{
int x,y,z,count;
node(){x=0;y=0;z=0;count=0;}
}pos;
int G[maxn][maxn][maxn];
int vis[maxn][maxn][maxn];
int count;
queue<node> q;
void bfs(){
while(!q.empty())q.pop();
q.push(pos);
while(!q.empty())
{
 	node t=q.front();
 	q.pop();
 	vis[t.x][t.y][t.z]=1;
 	for(int i=0;i<6;i++)
 	{
 		int tx=t.x+dir[i][0];
 		int ty=t.y+dir[i][1];
 		int tz=t.z+dir[i][2];
 		if(tx==ex&&ty==ey&&tz==ez)
 		{
 			f=0;
 			cout<<"Escaped in "<<t.count+1<<" minute(s)."<<endl;
 			return ;
 		}
 		if(vis[tx][ty][tz]==0&&G[tx][ty][tz])
 		{
 			node temp;
 			temp.x=tx;
 			temp.y=ty;
 			temp.z=tz;
 			vis[tx][ty][tz]=1;
 			temp.count=t.count+1;
 			q.push(temp);
 		}
 	}
}
}
int main()
{
int l,r,c;
while(cin>>l>>r>>c&&l!=0)
{
	f=1;
	for(int i=1;i<=l;i++)
		{
			for(int j=1;j<=r;j++)
			{
				for(int k=1;k<=c;k++)
				{
					char c;cin>>c;
					if(c=='.')G[i][j][k]=1;
					else if(c=='S')bx=i,by=j,bz=k;
					else if(c=='E')ex=i,ey=j,ez=k;
				}
			}
		}
		pos.x=bx;pos.y=by;pos.z=bz;
		bfs();
		if(f)cout<<"Trapped!"<<endl;

		for(int i=1;i<=l;i++)
		{
			for(int j=1;j<=r;j++)
			{
				for(int k=1;k<=c;k++)
				{
					G[i][j][k]=0;
					vis[i][j][k]=0;
				}
			}
		}//注意清空数组 memst容易T
}
 	return 0;
 }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值