POJ:2251 Dungeon Master(广搜)

6 篇文章 0 订阅

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值