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
  • 相关阅读:
    UVa 291 The House Of Santa Claus——回溯dfs
    (优先)队列简单总结
    POJ 2255 Tree Recovery——二叉树的前序遍历、后序遍历、中序遍历规则(递归)
    博弈论入门 Bash 、Nim 、Wythoff's Game结论及c++代码实现
    UVa 167(八皇后)、POJ2258 The Settlers of Catan——记两个简单回溯搜索
    欧几里得(辗转相除gcd)、扩欧(exgcd)、中国剩余定理(crt)、扩展中国剩余定理(excrt)简要介绍
    51nod 1135 原根 (数论)
    「学习笔记」扩展KMP (简)
    「解题报告」[luoguP6594]换寝室 (二分答案 树形DP)
    「解题报告」[luoguP6585]中子衰变 (交互题 分类讨论)
  • 原文地址:https://www.cnblogs.com/kongkaikai/p/3281928.html
Copyright © 2011-2022 走看看