zoukankan      html  css  js  c++  java
  • Dungeon Master(poj 2251)

    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!
    
    题意:3维迷宫!“.”是路;“#”是墙,“E"进“S”出
    这题的题目略有小坑;
    注意几个地方:1.数组开到105以上;2.队列清空;

     1 #include<cstdio>
     2 #include<cstdlib>
     3 #include<cstring>
     4 #include<iostream>
     5 #include<queue>
     6 using namespace std;
     7 int s[133][133][133];
     8 int vis[133][133][133];
     9 int r[6]= {-1,1,0,0,0,0};
    10 int c[6]= {0,0,-1,1,0,0};
    11 int h[6]= {0,0,0,0,1,-1};
    12 int ei,ej,ek;
    13 struct node
    14 {
    15     int a,b,c;
    16     int step;
    17 };
    18 node p,q;
    19 void getit(int I,int J,int K)//输入并处理
    20 {
    21     char ch;
    22     int i,j,k;
    23     for(k=1; k<K; k++)
    24     {
    25         for(i=1; i<I; i++)
    26         {
    27             for(j=1; j<J; j++)
    28             {
    29                 scanf("%c",&ch);
    30                 if(ch=='.')
    31                     s[i][j][k]=-1;
    32                 else if(ch=='E')
    33                 {
    34                     ei=i;
    35                     ej=j;
    36                     ek=k;
    37                 }
    38                 else if(ch=='S')
    39                     s[i][j][k]=1;
    40             }
    41             getchar();
    42         }
    43         getchar();
    44     }
    45 }
    46 void bfs(int ai,int aj, int ak,int step)//简单BFS
    47 {
    48     queue<node>que;
    49     vis[ai][aj][ak]=1;
    50     int t;
    51     p.a=ai;
    52     p.b=aj;
    53     p.c=ak;
    54     p.step=step;
    55     que.push(p);
    56     while(!que.empty())
    57     {
    58         p=que.front();
    59         que.pop();
    60         for(t=0; t<6; t++)
    61         {
    62             q.a=p.a+r[t];
    63             q.b=p.b+c[t];
    64             q.c=p.c+h[t];
    65             q.step=p.step+1;
    66             if(s[q.a][q.b][q.c]&&!vis[q.a][q.b][q.c])
    67             {
    68                 if(s[q.a][q.b][q.c]==1)
    69                 {
    70                     printf("Escaped in %d minute(s).
    ",q.step);
    71                     return ;
    72                 }
    73                 que.push(q);
    74                 vis[q.a][q.b][q.c]=1;
    75             }
    76         }
    77     }
    78     printf("Trapped!
    ");
    79 }
    80 int main()
    81 {
    82 
    83     int I,J,K;
    84     while(scanf("%d %d %d",&K,&I,&J)&&(I||J||K))
    85     {
    86         getchar();
    87         memset(s,0,sizeof(s));//置空
    88         memset(vis,0,sizeof(vis));
    89         getit(I+1,J+1,K+1);
    90         bfs(ei,ej,ek,0);
    91     }
    92     return 0;
    93 }
    View Code
  • 相关阅读:
    Binary Tree Zigzag Level Order Traversal
    Binary Tree Level Order Traversal
    Symmetric Tree
    Best Time to Buy and Sell Stock II
    Best Time to Buy and Sell Stock
    Triangle
    Populating Next Right Pointers in Each Node II
    Pascal's Triangle II
    Pascal's Triangle
    Populating Next Right Pointers in Each Node
  • 原文地址:https://www.cnblogs.com/kongkaikai/p/3281928.html
Copyright © 2011-2022 走看看