zoukankan      html  css  js  c++  java
  • Dungeon Master POJ

    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!
    3D BFS
     1 #include <iostream>
     2 using namespace std;
     3 #include<string.h>
     4 #include<set>
     5 #include<stdio.h>
     6 #include<math.h>
     7 #include<queue>
     8 #include<map>
     9 #include<algorithm>
    10 #include<queue>
    11 #define ll long long
    12 struct lll
    13 {
    14     int x,y,z,bu;
    15 }qidian,s;
    16 
    17  queue<lll>TM;
    18 int b[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
    19 int main()
    20 {
    21     int lenx,leny,lenz;
    22     char a[33][33][33];
    23     int i,j,k;
    24     while(cin>>lenz>>leny>>lenx)
    25     {
    26         if(lenx==0&&leny==0&&lenz==0)
    27             break;
    28 
    29 
    30         queue<lll>TM;
    31         for(i=1;i<=lenz;i++)
    32         {
    33             for(j=1;j<=leny;j++)
    34             {
    35                 for(k=1;k<=lenx;k++)
    36                     {
    37                         cin>>a[i][j][k];
    38                         if(a[i][j][k]=='S')
    39                         {
    40                             qidian.x=k;
    41                             qidian.y=j;
    42                             qidian.z=i;
    43                         }
    44                     }
    45             }
    46         }
    47 
    48             qidian.bu=0;
    49             int flag =0;
    50             int bu;
    51             TM.push(qidian);
    52             while(!TM.empty())
    53             {
    54                 if(a[TM.front().z][TM.front().y][TM.front().x]=='E')
    55                 {
    56                     flag=1;
    57                     bu=TM.front().bu;
    58                     break;
    59                 }
    60                 for(i=0;i<6;i++)
    61                 {
    62                     s.z=TM.front().z+b[i][2];
    63                     s.y=TM.front().y+b[i][1];
    64                     s.x=TM.front().x+b[i][0];
    65                     s.bu=TM.front().bu+1;
    66                     if(s.x>lenx||s.x<1||s.y>leny||s.y<1||s.z>lenz||s.z<1)
    67                         continue;
    68                     else if(a[s.z][s.y][s.x]=='#')
    69                         continue;
    70                     else if(a[s.z][s.y][s.x]=='E')
    71                     {
    72                         TM.push(s);
    73                     }
    74                     else
    75                     {
    76                         a[s.z][s.y][s.x]='#';
    77                         TM.push(s);
    78                     }
    79                 }
    80                 TM.pop();
    81             }
    82             if(flag)
    83                 cout<<"Escaped in "<<bu<<" minute(s)."<<endl;
    84             else
    85                 cout<<"Trapped!"<<endl;
    86     }
    87     return 0;
    88 }
    View Code
  • 相关阅读:
    DFS-B
    DFS/BFS-A
    DFS-回溯与剪枝-C
    BFS-八数码问题与状态图搜索
    PTA-1003 我要通过!
    二分-G
    二分-F
    二分-E
    二分-D
    二分-C
  • 原文地址:https://www.cnblogs.com/dulute/p/7272634.html
Copyright © 2011-2022 走看看