pku acm 2251源码

#include <iostream>
#include <cstring>
using namespace std;
const int N=31;
char maze[N][N][N];
int visited[N][N][N];

int dir[6][3]={{1,0,0},{0,-1,0},{-1,0,0},{0,1,0},{0,0,-1},{0,0,1}};

int R,C,L;

struct Node
{
	int x; int y; int z;
	int pre;
};

Node Q[N*N*N];

int bfs(int x,int y,int z)
{ 
	visited[x][y][z] = 1;
	Node node; node.x=x; node.y=y; node.z=z; node.pre=-1;
	Q[0]=node;
	int first=-1,last=0;
	
	while(first!=last)
	{ 
		Node v=Q[++first];
		for (int i=0;i<6;i++)
		{
			int x=v.x+dir[i][0];
			int y=v.y+dir[i][1];
			int z=v.z+dir[i][2];

			if (x<R&&x>=0&&y<C&&y>=0&&z<L&&z>=0)
			{
				if ((maze[x][y][z]=='.'||maze[x][y][z]=='E')&&(visited[x][y][z]==0))
				{	
					node.x=x;node.y=y;node.z=z;node.pre=first;
					Q[++last]=node;                     
					visited[x][y][z] = 1; 
				}
				if (maze[x][y][z]=='E')
				{
					int cnt=0;
					int pre=Q[last].pre;
					while (pre!=-1)
					{
						cnt++;
						pre=Q[pre].pre;
					}
					return cnt;
				}
			}
		}	
	} 
	return 0;
}
int main()
{
	while (cin>>L>>R>>C)
	{
		if (L==0 &&R==0 &&C==0)
		    break;

        memset(visited,0,sizeof(visited));
		int i,j,k,r,c,l;
		for (k=0;k<L;k++)
		{
			for (i=0;i<R;i++)
			{
				for (j=0;j<C;j++)
				{
					cin>>maze[i][j][k];
					if (maze[i][j][k]=='S')
					{
                        r=i;  c=j;  l=k;
					}
				}
			}
		}

		int n=bfs(r,c,l);
		if (n>0)
			cout<<"Escaped in "<<n<<" minute(s)."<<endl;
		else
			cout<<"Trapped!"<<endl;

	}
	return 0;
}

你可能感兴趣的:(ACM,bfs,pku,2251)