POJ:2251 Dungeon Master(广搜)

来源:互联网 发布:百度seo是什么意思 编辑:程序博客网 时间:2024/05/17 20:31

Dungeon Master

Time Limit: 1000MS

Memory Limit: 65536K

Total Submissions: 26419

Accepted: 10285

Description

You are trapped in a 3D dungeon and need to find the quickestway out! The dungeon is composed of unit cubes which may or may not be filled withrock. It takes one minute to move one unit north, south, east, west, up ordown. You cannot move diagonally and the maze is surrounded by solid rock onall sides. 

Is an escape possible? If yes, how long will it take? 

Input

The input consists of a number of dungeons. Each dungeondescription starts with a line containing three integers L, R and C (alllimited 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. Eachcharacter describes one cell of the dungeon. A cell full of rock is indicatedby a '#' and empty cells are represented by a '.'. Your starting position isindicated by 'S' and the exit by the letter 'E'. There's a single blank lineafter 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 toreach 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

UlmLocal 1997

题目大意:给你一个l、n、m,分别代表迷宫的层数,每层的长、宽,问能否从S走到E,如果能的话就输出最短走几步(分钟),不能的话就输出trapped(困境)。

解题思路:还是搜索,只不过变成了三维的,除了以前的前后左右,还多了上下两个走法,用广搜队列来做。

代码如下:

#include<cstdio>#include<cstring>#include<queue>using namespace std;int visit[35][35][35];char map[35][35][35];struct node{          int ceng,hang,lie;          int step;};int main(){          int l,n,m;          while(scanf("%d%d%d",&l,&n,&m)!=EOF)          {                    if(l==0&&n==0&&m==0)                    break;                    structnode start;                    memset(visit,0,sizeof(visit));                    for(inti=0;i<l;i++)                    {                               for(intj=0;j<n;j++)                               {                                         scanf("%s",&map[i][j]);                                         for(intk=0;k<m;k++)                                         {                                                   if(map[i][j][k]=='S')                                                   {                                                             start.ceng=i;                                                             start.hang=j;                                                             start.lie=k;                                                             start.step=0;                                                             visit[i][j][k]=1;//走过的都要标记                                                   }                                         }                               }                    }                    queue<node>q;                    q.push(start);//起点入队                    intflag=0;                    while(!q.empty())                    {                               structnode tmp=q.front();                               q.pop();                               if(map[tmp.ceng][tmp.hang][tmp.lie]=='E')//找到终点的话                               {                                         flag=1;                                         printf("Escapedin %d minute(s).\n",tmp.step);//直接在这输出,别在循环外面输出q.front,因为刚才已经pop掉了,wa了几次。。。                                         break;                               }                               tmp.step++;//为下次走,提前算上步数                               structnode tmp2;//临时变量                               if(tmp.ceng+1<l)//向上走                               {                                         tmp2=tmp;                                         tmp2.ceng++;                                         if(map[tmp2.ceng][tmp2.hang][tmp2.lie]!='#'&&visit[tmp2.ceng][tmp2.hang][tmp2.lie]==0)//下一步能走的话,就入队                                         {                                                   visit[tmp2.ceng][tmp2.hang][tmp2.lie]=1;//走过的要标记,避免重复走                                                   q.push(tmp2);                                         }                               }                               if(tmp.ceng-1>=0)//向下走                               {                                         tmp2=tmp;                                         tmp2.ceng--;                                         if(map[tmp2.ceng][tmp2.hang][tmp2.lie]!='#'&&visit[tmp2.ceng][tmp2.hang][tmp2.lie]==0)                                         {                                                   visit[tmp2.ceng][tmp2.hang][tmp2.lie]=1;                                                   q.push(tmp2);                                         }                               }                               if(tmp.hang+1<n)//向前走                               {                                         tmp2=tmp;                                         tmp2.hang++;                                         if(map[tmp2.ceng][tmp2.hang][tmp2.lie]!='#'&&visit[tmp2.ceng][tmp2.hang][tmp2.lie]==0)                                         {                                                   visit[tmp2.ceng][tmp2.hang][tmp2.lie]=1;                                                   q.push(tmp2);                                         }                               }                               if(tmp.hang-1>=0)//向后走                               {                                         tmp2=tmp;                                         tmp2.hang--;                                         if(map[tmp2.ceng][tmp2.hang][tmp2.lie]!='#'&&visit[tmp2.ceng][tmp2.hang][tmp2.lie]==0)                                         {                                                   visit[tmp2.ceng][tmp2.hang][tmp2.lie]=1;                                                   q.push(tmp2);                                         }                               }                               if(tmp.lie+1<m)//向左走                               {                                         tmp2=tmp;                                         tmp2.lie++;                                         if(map[tmp2.ceng][tmp2.hang][tmp2.lie]!='#'&&visit[tmp2.ceng][tmp2.hang][tmp2.lie]==0)                                         {                                                   visit[tmp2.ceng][tmp2.hang][tmp2.lie]=1;                                                   q.push(tmp2);                                         }                               }                               if(tmp.lie-1>=0)//向右走                               {                                         tmp2=tmp;                                         tmp2.lie--;                                         if(map[tmp2.ceng][tmp2.hang][tmp2.lie]!='#'&&visit[tmp2.ceng][tmp2.hang][tmp2.lie]==0)                                         {                                                   visit[tmp2.ceng][tmp2.hang][tmp2.lie]=1;                                                   q.push(tmp2);                                         }                               }                    }                    if(flag==0)                    printf("Trapped!\n");          }          return 0;}


 

0 0
原创粉丝点击