Dungeon Master - UVa 532 搜索

本文深入解析如何在限定条件下寻找三维迷宫中从起点S到终点E的最短逃生路径,通过详细步骤和算法实现,提供了解决类似问题的策略和思考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Dungeon Master

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 Specification 

The input file consists of a number of dungeons. Each dungeon description starts with a line containing three integers LR 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 LR and C.

Output Specification 

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!

题意:在一个三维的房间内,问从S走到E至少需要多少步。

思路:简单搜索,没什么可说的。

AC代码如下:

#include<cstdio>
#include<cstring>
using namespace std;
char s[40][40][40];
int t,vis[40][40][40],vc[2][100010][3],ox[6]={-1,0,1,0,0,0},oy[6]={0,1,0,-1,0,0},oz[6]={0,0,0,0,1,-1};
bool flag;
void dfs(int x,int y,int z,int to)
{
    int i,j,k;
    if(s[z][x][y]=='E')
    {
        flag=true;
        return;
    }
    for(i=0;i<6;i++)
       if(s[z+oz[i]][x+ox[i]][y+oy[i]]!='#' && vis[z+oz[i]][x+ox[i]][y+oy[i]]!=t)
       {
           vc[to][0][0]++;
           vc[to][vc[to][0][0]][0]=x+ox[i];
           vc[to][vc[to][0][0]][1]=y+oy[i];
           vc[to][vc[to][0][0]][2]=z+oz[i];
           vis[z+oz[i]][x+ox[i]][y+oy[i]]=t;
       }
}
int main()
{
    int i,j,k,l,r,c,step,a,b;
    bool ok;
    while(scanf("%d%d%d",&l,&r,&c)&& l+r+c)
    {
        t++;
        ok=false;
        flag=false;
        for(i=1;i<=l;i++)
           for(j=1;j<=r;j++)
           {
               scanf("%s",s[i][j]+1);
               if(!ok)
               for(k=1;k<=c;k++)
                  if(s[i][j][k]=='S')
                  {
                      ok=true;
                      vc[0][1][0]=j;
                      vc[0][1][1]=k;
                      vc[0][1][2]=i;
                      vis[i][j][k]=t;
                  }
           }
        for(i=0;i<=r+1;i++)
           for(j=0;j<=c+1;j++)
              s[0][i][j]=s[l+1][i][j]='#';
        for(i=1;i<=l;i++)
        {
            for(j=0;j<=r+1;j++)
               s[i][j][0]=s[i][j][c+1]='#';
            for(j=0;j<=c+1;j++)
               s[i][0][j]=s[i][r+1][j]='#';
        }
        vc[0][0][0]=1;
        step=0;
        while(true)
        {
            if(flag)
              break;
            step++;
            if(step&1)
              a=0,b=1;
            else
              a=1,b=0;
            if(vc[a][0][0]==0)
              break;
            vc[b][0][0]=0;
            for(i=1;i<=vc[a][0][0];i++)
               dfs(vc[a][i][0],vc[a][i][1],vc[a][i][2],b);
        }
        if(flag)
          printf("Escaped in %d minute(s).\n",step-1);
        else
          printf("Trapped!\n");
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值