简单搜索篇 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;
 }

   
   

猜你喜欢

转载自blog.csdn.net/Stydwn/article/details/105390604
今日推荐