Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 32684   Accepted: 12529

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

  1. 3 4 5
  2. S....
  3. .###.
  4. .##..
  5. ###.#
  6.  
  7. #####
  8. #####
  9. ##.##
  10. ##...
  11.  
  12. #####
  13. #####
  14. #.###
  15. ####E
  16.  
  17. 1 3 3
  18. S##
  19. #E#
  20. ###
  21.  
  22. 0 0 0

Sample Output

  1. Escaped in 11 minute(s).
  2. Trapped!

Source

解题思路:只是把二维的变成三维的了,本质上不变,就增加了两个方向。
但我还是卡了好久,我刚开始只记录了上一步的位置,不往上一步的位置走,结果一直超时。
后来发现只要走过的地方就把vis数组置为1,以后就不能走到这个位置了。
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <queue>
  6.  
  7. using namespace std;
  8.  
  9. struct node{
  10. int x,y,z;
  11. int step;
  12. };
  13.  
  14. int l,r,c;
  15. char maze[][][];
  16. int vis[][][];
  17. node start;
  18. node endd;
  19. int directx[]={,,,,,-};
  20. int directy[]={-,,,,,};
  21. int directz[]={,,-,,,};
  22.  
  23. int findd(){
  24. queue<node> que;
  25. memset(vis,,sizeof(vis));
  26. start.step=;
  27. que.push(start);
  28. while(!que.empty()){
  29. node now=que.front();
  30. que.pop();
  31. for(int i=;i<;i++){
  32. node next=now;
  33. next.x+=directx[i];
  34. next.y+=directy[i];
  35. next.z+=directz[i];
  36. int xx=next.x;
  37. int yy=next.y;
  38. int zz=next.z;
  39. if(maze[xx][yy][zz]=='#'||vis[xx][yy][zz]||!(xx>=&&xx<l&&yy>=&&yy<r&&yy>=&&zz>=&&zz<c)){
  40. continue;
  41. }
  42. if(maze[xx][yy][zz]=='E'){
  43. next.step+=;
  44. return next.step;
  45. }
  46. if(maze[xx][yy][zz]=='.'){
  47. next.step+=;
  48. vis[xx][yy][zz]=;
  49. que.push(next);
  50. }
  51. }
  52. }
  53. return ;
  54. }
  55.  
  56. int main()
  57. {
  58. while(scanf("%d %d %d",&l,&r,&c)&&(l+r+c!=)){
  59. getchar();
  60. for(int i=;i<l;i++,getchar()){
  61. for(int j=;j<r;j++,getchar()){
  62. for(int k=;k<c;k++){
  63. scanf("%c",&maze[i][j][k]);
  64. if(maze[i][j][k]=='S'){
  65. start.x=i;
  66. start.y=j;
  67. start.z=k;
  68. }
  69. }
  70. }
  71. }
  72. int ans=findd();
  73. if(ans){
  74. printf("Escaped in %d minute(s).\n",ans);
  75. }else{
  76. printf("Trapped!\n");
  77. }
  78.  
  79. }
  80.  
  81. return ;
  82. }

poj2251_kuagnbin带你飞专题一的更多相关文章

  1. 【算法系列学习三】[kuangbin带你飞]专题二 搜索进阶 之 A-Eight 反向bfs打表和康拓展开

    [kuangbin带你飞]专题二 搜索进阶 之 A-Eight 这是一道经典的八数码问题.首先,简单介绍一下八数码问题: 八数码问题也称为九宫问题.在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的 ...

  2. [kuangbin带你飞]专题1-23题目清单总结

    [kuangbin带你飞]专题1-23 专题一 简单搜索 POJ 1321 棋盘问题POJ 2251 Dungeon MasterPOJ 3278 Catch That CowPOJ 3279 Fli ...

  3. [kuangbin带你飞]专题十 匹配问题

        A-L 二分匹配 M-O 二分图多重匹配 P-Q 二分图最大权匹配 R-S 一般图匹配带花树 模板请自己找     ID Origin Title   61 / 72 Problem A HD ...

  4. [kuangbin带你飞]专题十 匹配问题 一般图匹配

    过去做的都是二分图匹配 即 同一个集合里的点 互相不联通 但是如果延伸到一般图上去 求一个一般图的最大匹配 就要用带花树来解决 带花树模板 用来处理一个无向图上的最大匹配 看了一会还是不懂  抄了一遍 ...

  5. [kuangbin带你飞]专题十 匹配问题 二分匹配部分

    刚回到家 开了二分匹配专题 手握xyl模板 奋力写写写 终于写完了一群模板题 A hdu1045 对这个图进行 行列的重写 给每个位置赋予新的行列 使不能相互打到的位置 拥有不同的行与列 然后左行右列 ...

  6. [kuangbin带你飞]专题八 生成树 - 次小生成树部分

    百度了好多自学到了次小生成树 理解后其实也很简单 求最小生成树的办法目前遇到了两种 1 prim 记录下两点之间连线中的最长段 F[i][k] 之后枚举两点 若两点之间存在没有在最小生成树中的边 那么 ...

  7. [kuangbin带你飞]专题六 最小生成树

    学习最小生成树已经有一段时间了 做一些比较简单的题还算得心应手..花了三天的时间做完了kuangbin的专题 写一个题解出来记录一下(虽然几乎都是模板题) 做完的感想:有很多地方都要注意 n == 1 ...

  8. [kuangbin带你飞]专题十五 数位DP

            ID Origin Title   62 / 175 Problem A CodeForces 55D Beautiful numbers   30 / 84 Problem B HD ...

  9. [kuangbin带你飞]专题十 匹配问题 二分图多重匹配

    二分图的多重匹配问题不同于普通的最大匹配中的"每个点只能有最多一条边" 而是"每个点连接的边数不超过自己的限定数量" 最大匹配所解决的问题一般是"每个 ...

随机推荐

  1. Wordpress显示文章摘要

    放在文章的循环里: <?php if (!empty($post->post_excerpt) ) { //如果文章有摘要则输出摘要 the_excerpt(); } ?>

  2. matplotlib、PIL、cv2图像操作 && caffe / tensorflow 通道顺序

    用python进行图像处理中分别用到过matplotlib.pyplot.PIL.cv2三种库,这三种库图像读取和保存方法各异,并且图像读取时顺序也有差异,如plt.imread和PIL.Image. ...

  3. KVM之CPU虚拟化

    1.1 为什么要虚拟化CPU 虚拟化技术是指在x86的系统中,一个或以上的客操作系统(Guest Operating System,简称:Guest OS)在一个主操作系统(Host Operatin ...

  4. HashMap代码解析

    hashmap (jdk 1.7)使用 “数组-链表” 方式进行存储,图形化表示如下: 即,前面是一个数组,后面跟一个链表,那么数据结构这个对应到HashMap的代码里面是什么样子的呢? 在HashM ...

  5. Django 复习

    Django 基础1 day49 老师的博客:https://www.cnblogs.com/yuanchenqi/articles/6083427.html http://www.cnblogs.c ...

  6. Atitit 关于处理环保行动联盟和动物解放阵线游击队的任命书 委任状

    Atitit 关于处理环保行动联盟和动物解放阵线游击队的任命书 委任状 Uke 集团文化部部长兼emir 大酋长圣旨到!! In god we trust ,Emir Decree大酋长圣旨:: En ...

  7. golang:iconv

    最近在做邮件解析的工作,遇到需要转字符集编码的情况,go官方好像没有提供这样的库,于是从github上找了一下. https://github.com/qiniu/iconv 开发环境: linux ...

  8. new和delete操作符

    C 语言中提供了 malloc 和 free 两个系统函数, 完成对堆内存的申请和释放.而 C++则提供了两个操作符 new 和 delete. 1. newnew 分配内存空间时,  分配内存空间大 ...

  9. [转]kindeditor隐藏上传图片框网络图片或本地上传的功能

    原文地址:http://www.lingchenliang.com/post/154.html kindeditor富文本编辑器点击上传图片按钮,在弹出的窗口中去掉上传网络图片的功能,只留下本地上传, ...

  10. mysql表空间加密 keyring encryption

    从5.7.11开始,mysql开始支持物理表空间的加密,它使用两层加密架构.包括:master key 和 tablespace key master key用于加密tablespace key,加密 ...