zoukankan      html  css  js  c++  java
  • POJ2251 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

    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!
    三维迷宫最短路,BFS直接套板子即可。
    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    #include <vector> 
    #include <cstring>
    #include <queue>
    using namespace std;
    int L,R,C;
    char mmap[35][35][35];
    bool vis[35][35][35]={0};
    int dir[6][3]={{-1,0,0},{1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};//x y z
    struct point 
    {
        int x;
        int y;
        int z;
        int cnt;
    };
    point start,end;
    void bfs()//cnt统计步数 
    {
       memset(vis,0,sizeof(vis));
      queue<point>q;
       point pre,nxt;
       vis[start.z][start.x][start.y]=1;
       start.cnt=0;
       q.push(start);
       while(!q.empty())
       {
               pre=q.front();
               q.pop();
               if(pre.x==end.x&&pre.y==end.y&&pre.z==end.z)
               {
                   cout<<"Escaped in "<<pre.cnt<<" minute(s)."<<endl;
                   return;
            } 
            int i;
            for(i=0;i<6;i++)
            {
                int nx=pre.x+dir[i][0];
                int ny=pre.y+dir[i][1];
                int nz=pre.z+dir[i][2];
                if(nx>=0&&nx<R&&ny>=0&&ny<C&&nz>=0&&nz<L&&mmap[nz][nx][ny]!='#'&&!vis[nz][nx][ny])//也可以写成mmap[nz][nx][ny]=='.'||mmap[nz][nx][ny]=='E' 但不要只写 =='.' 
                {
                    nxt.x=nx;
                    nxt.y=ny;
                    nxt.z=nz;
                    nxt.cnt=pre.cnt+1;
                    q.push(nxt);
                    vis[nz][nx][ny]=1;
                }
            }
       }
    
           cout<<"Trapped!"<<endl;
    }
    int main()
    {
        while(scanf("%d%d%d",&L,&R,&C)&&L&&R&&C)
        {
            int i,j,k; 
            for(i=0;i<L;i++)
            {
                for(j=0;j<R;j++)
                {
                    scanf("%s",&mmap[i][j]);
                }
                getchar();
                getchar();
            }
            for(i=0;i<L;i++)
            {
                for(j=0;j<R;j++)
                {
                    for(k=0;k<C;k++)
                    {
                        if(mmap[i][j][k]=='S')
                        {
                            start.x=j;
                            start.y=k;
                            start.z=i;
                        }
                        else if(mmap[i][j][k]=='E')
                        {
                            end.x=j;
                            end.y=k;
                            end.z=i;
                        }
                    }
                }
            }
            bfs();
        }     
    }

  • 相关阅读:
    分区助手怎么调整磁盘分区的大小
    3dsmax2014的下载、安装与注册激活教程详解
    U深度U盘启动盘制作工具怎么用?U深度U盘启动盘制作工具使用教学
    CAD出现向程序发送命令时出现问题提示解决方法分享
    TeamViewer——可以实现在手机上随时远程控制你的电脑
    CPU-Z五大主要功能及使用方法初步了解
    vs中更改项目名称注意事项
    Oracle 存储过程例子返回记录集
    oracle 调用包体的函数并返回return值
    oracle 中更新update不成功的原因
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/12317538.html
Copyright © 2011-2022 走看看