zoukankan      html  css  js  c++  java
  • 暑假集训(1)第三弹 -----Dungeon Master(Poj2251)

    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!





    问题分析:依旧是简单bfs,但是要用三维数组储存地图,但是有6个分支,用数组来储存方向可以让程序更简洁。

     1 #include "iostream"
     2 #include "queue"
     3 using namespace std;
     4 char maze[32][32][32];
     5 int v[6][3]={0,0,-1,0,0,1,0,-1,0,0,1,0,-1,0,0,1,0,0};
     6 int L,R,C;
     7 struct escaper
     8 {
     9     int i;
    10     int j;
    11     int k;
    12     int time;
    13 };
    14 escaper fir;
    15 void mbegin()
    16 {
    17    for (int i=0;i<=L+1;i++)
    18     for (int j=0;j<=R+1;j++)
    19      for (int k=0;k<=C+1;k++)
    20         if (i*j*k == 0 || i == L+1 || j==R+1 || k==C+1)
    21                   maze[i][j][k] = '#';
    22        else
    23        {
    24             cin>>maze[i][j][k];
    25          if (maze[i][j][k] == 'S')
    26               {
    27                   fir.i = i;
    28                   fir.j = j;
    29                   fir.k = k;
    30               }
    31         }
    32 }
    33 void bfs()
    34 {
    35    queue <escaper> p;
    36    escaper sec;
    37    fir.time=0;
    38    p.push(fir);
    39      while (!p.empty())
    40    {
    41       sec = p.front();
    42       p.pop();
    43      for (int i=0;i<6;i++)
    44       {
    45          fir.i = sec.i+v[i][0];
    46          fir.j = sec.j+v[i][1];
    47          fir.k = sec.k+v[i][2];
    48         if (maze[fir.i][fir.j][fir.k] != '#')
    49           {
    50               fir.time = sec.time+1;
    51               if (maze[fir.i][fir.j][fir.k] == 'E')
    52                    {
    53                        cout<<"Escaped in "<<fir.time<<" minute(s)."<<endl;
    54                        return;
    55                    }
    56               maze[fir.i][fir.j][fir.k] = '#';
    57               p.push(fir);
    58           }
    59       }
    60    }
    61     cout<<"Trapped!"<<endl;
    62 }
    63 int main()
    64 {
    65     while (cin>>L>>R>>C && L && R && C)
    66     {
    67          mbegin();
    68           bfs();
    69     }
    70    return 0;
    71 }
    View Code

  • 相关阅读:
    JS中解析JSON。
    对不同浏览器实现图片旋转。
    FF和IE内容不透明,字体透明。
    C# 通过身份证查询出生日期
    C# v3微信 access token 过期处理的问题
    C# 微信v3退款
    codesmith生成java类
    IOS调用WCF服务,WCF服务器进行上传图片
    安装VS 2013遇到的问题,及解决方案
    接口,个人理解
  • 原文地址:https://www.cnblogs.com/huas-zlw/p/5676774.html
Copyright © 2011-2022 走看看