3D dungeon

算法:广搜;

描述 You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.

Is an escape possible? If yes, how long will it take?
输入The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.输出Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
Escaped in x minute(s).

where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!样例输入3 4 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0
样例输出

Escaped in 11 minute(s).
Trapped!

代码:

 #include <iostream>
   #include <cstring>
   #include <queue>
   #include <algorithm>
   #include <iomanip>
   #include <stdio.h>
   using namespace std;
   char ch[35][35][35];
   int n,m,k,x1,x2,y1,y2,z1,z2;
   int a[6][3]={1,0,0,-1,0,0,0,1,0,0,-1,0,0,0,1,0,0,-1};
   struct dot
   {
   	   int x,y,z,step;
   } ;
   int cmp(int ax,int ay,int az)
   {
   	   if(ax>=0&&ax<n&&ay>=0&&ay<m&&az>=0&&az<k&&ch[ax][ay][az]=='.') return 1;
   	   return 0;
   }
   int bfs()
   {
   	   queue<dot>que;
   	   dot cur,loer;
   	   cur.x=x1;cur.y=y1;cur.z=z1;
   	   cur.step=0;
   	   ch[x1][y1][z1]='#';
   	   que.push(cur);
   	   while(que.size())
   	   {
   	   	    loer=que.front();
   	   	    que.pop();
   	   	    if(loer.x==x2&&loer.y==y2&&loer.z==z2)
   	   	    return loer.step;
   	   	    for(int i=0;i<6;i++)
   	   	    {
   	   	    	cur=loer;
   	   	    	cur.x+=a[i][0];
   	   	    	cur.y+=a[i][1];
   	   	    	cur.z+=a[i][2];
   	   	    	if(cmp(cur.x,cur.y,cur.z))
   	   	    	{
   	   	    		ch[cur.x][cur.y][cur.z]='#';
   	   	    		cur.step++;
   	   	    		que.push(cur);
				}
			}
	   }
	   return -1;
   }
   int main()
   {
   	   int i,j,p;
   	   while(cin>>n>>m>>k&&n&&m&&k)
		{
		     for(i=0;i<n;i++)
			{
			    for(j=0;j<m;j++)
				{
					for(p=0;p<k;p++)
					{
						cin>>ch[i][j][p];
						if(ch[i][j][p]=='S')
						{x1=i;y1=j;z1=p;}
						if(ch[i][j][p]=='E')
						{x2=i;y2=j;z2=p;ch[i][j][p]='.';}
					}
				}	
			} 
			int ans=bfs();
			if(ans>=0) cout<<"Escaped in "<<ans<<" minute(s)."<<endl;
			else cout<<"Trapped!"<<endl;	
		} 
		return 0;
   }



posted @ 2016-03-21 18:45  (慎独)  阅读(260)  评论(0编辑  收藏  举报