poj 2251 Dungeon Master

#include<iostream>
#include<queue>
using namespace std;

typedef struct node
{
	int x,y,z;
}Point;

int L,R,C,sum,go[6][3]={{1,0,0},{-1,0,0},{0,-1,0},{0,1,0},{0,0,1},{0,0,-1}};
char ch[32][32][32];
bool flag[32][32][32];//标记是否可走
Point start,end;
queue<Point> Q;

int step[32][32][32];

bool fun(int x,int y,int z)
{
	if(x>=1&&x<=L&&y>=1&&y<=R&&z>=1&&z<=C)
		return true;
	return false;
}
int BFS()
{
	int i,nx,ny,nz;
	Point now,tmp;	
	while(!Q.empty())
	{
		now=Q.front();
		Q.pop();
		for(i=0;i<6;i++)
		{
			nx=now.x+go[i][0];
			ny=now.y+go[i][1];
			nz=now.z+go[i][2];				
			if(!flag[nx][ny][nz]&&fun(nx,ny,nz))
			{			
				tmp.x=nx;
				tmp.y=ny;
				tmp.z=nz;				
				step[nx][ny][nz] = step[now.x][now.y][now.z] + 1;
				Q.push(tmp);

				flag[nx][ny][nz]=true;			//标记为已访问

				if(nx==end.x && end.y==ny && end.z==nz)
					return 1;
			}
		}
	}
	return 0;
}
int main()
{	
	int i,j,k;	
	while(cin>>L>>R>>C && L && R&& C)
	{		
		memset(flag,false,sizeof(flag));
		sum=0;		
		while(!Q.empty())
			Q.pop();	
		for(i=1;i<=L;i++)//z
			for(j=1;j<=R;j++)//x
				for(k=1;k<=C;k++)//y
				{
					cin>>ch[i][j][k];
					if(ch[i][j][k]=='#')
						flag[i][j][k]=true;
					if(ch[i][j][k]=='S')
					{
						start.x=i;
						start.y=j;
						start.z=k;
						step[i][j][k]=0;
						flag[i][j][k]=true;
					}
					if(ch[i][j][k]=='E')
					{
						end.x=i;
						end.y=j;
						end.z=k;
					}
				}	
				Q.push(start);
				if(BFS())
					cout<<"Escaped in "<<step[end.x][end.y][end.z]<<" minute(s)."<<endl;
				else
					cout<<"Trapped!"<<endl;
	}
	return 0;
}

你可能感兴趣的:(ACM,acm宽度搜索)