无声的月

 

poj 2251 BFS 三维迷宫

#include<iostream>
#include
<queue>
using namespace std;
/*
 1 means it can go through
 0 means it cann't go through
 -1 means the exit point
 2 means the starting point
 
*/

int maze[35][35][35],L,R,C,sx,sy,sz,dx,dy,dz,cnt;
struct point
{
    
int x,y,z;
    
int level;
}
;
bool v(int x,int y,int z)
{
    
return x<=L&&y<=R&&z<=C;
}

int main()
{
    
int i,j,k,x,y,z,nx,ny,nz,level,nlevel,px,py,pz;
    
bool find;
    
char ch;
    point p;
    memset(maze,
0,sizeof(maze));
    
    
while(cin>>L>>R>>C,L||R||C)
    
{
        queue
<point> q;
        find
=false;
        
for(i=1;i<=L;i++)
        
{
            
for(j=1;j<=R;j++)
            
{
                
for(k=1;k<=C;k++)
                
{
                    cin
>>ch;
                    
switch(ch)
                    
{
                        
case 'S':maze[i][j][k]=-1;sx=i;sy=j;sz=k;break;
                        
case '.':maze[i][j][k]=1;break;
                        
case '#':maze[i][j][k]=0;break;
                        
case 'E':maze[i][j][k]=2;dx=i;dy=j;dz=k;break;
                        
default:
                            
break;
                    }

                }

            }

        }

        
        p.x
=sx;p.y=sy;p.z=sz;p.level=0;
        q.push(p);
      
//  cout<<sx<<","<<sy<<","<<sz<<endl;
        while(!q.empty())
        
{
            p
=q.front();
            q.pop();
            x
=p.x;y=p.y;z=p.z; level=p.level;
         
//   cout<<x<<"  "<<y<<"  "<<z<<"  -->"<<level<<endl;
            if(x==dx&&y==dy&&z==dz)
            
{
                cnt
=level;
                find
=true;
                
break;
            }

            
for(i=0;i<6;i++)
            
{
                
switch(i)
                
{
                    
case 0:px=x-1;py=y;pz=z;break;
                    
case 1:px=x+1;py=y;pz=z;break;
                    
case 2:px=x;py=y+1;pz=z;break;
                    
case 3:px=x;py=y-1;pz=z;break;
                    
case 4:px=x;py=y;pz=z+1;break;
                    
case 5:px=x;py=y;pz=z-1;break;
                }

                
if(maze[px][py][pz]==1||maze[px][py][pz]==2)//don't forgot to let the last enter
                {
                    p.x
=px;p.y=py;p.z=pz;p.level=level+1;
                    q.push(p);
                    maze[px][py][pz]
=3;
                }

            }

        }

        
if(find)
        
{
            printf(
"Escaped in %d minute(s).\n",cnt);
        }

        
else
        cout
<<"Trapped!"<<endl;
    }

}

Dungeon Master
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6841 Accepted: 2718

Description

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?

Input

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.

Output

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!

Sample Input

3 4 5
S....
.###.
.##..
###.#
#####
#####
##.##
##...
#####
#####
#.###
####E
1 3 3
S##
#E#
###
0 0 0

Sample Output

Escaped in 11 minute(s).
Trapped!

Source

posted on 2010-07-25 11:14 Baron 阅读(379) 评论(0)  编辑 收藏 引用 所属分类: 搜索


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

统计

常用链接

留言簿

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜