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

    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!



      1 #include <iostream>
      2 #include <stdio.h>
      3 #include <string.h>
      4 #include <queue>
      5 using namespace std;
      6 struct node
      7 {
      8     int x,y,z;
      9     int time;
     10 };
     11 char map[33][33][33];
     12 bool flag[33][33][33];
     13 int dir[][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
     14 int L,R,C;
     15 int sx,sy,sz,ex,ey,ez;
     16 
     17 bool judge(node p)
     18 {
     19     if(p.x<0 || p.z<0 || p.y<0 || p.x>=L || p.y>=R || p.z>=C || flag[p.x][p.y][p.z] || map[p.x][p.y][p.z]=='#')
     20     {
     21         return false;
     22     }
     23     return true;
     24 }
     25 void bfs()
     26 {
     27     node p,temp;
     28     int i;
     29     queue<node>q;
     30     while(!q.empty())
     31     {
     32         q.pop();
     33     }
     34     flag[sx][sy][sz]=true;
     35     p.x=sx;
     36     p.y=sy;
     37     p.z=sz;
     38     p.time=0;
     39     q.push(p);
     40     while(!q.empty())
     41     {
     42         p=q.front();
     43         q.pop();
     44         for(i=0;i<6;i++)
     45         {
     46             temp=p;
     47             temp.x+=dir[i][0];
     48             temp.y+=dir[i][1];
     49             temp.z+=dir[i][2];
     50             temp.time=p.time+1;
     51             if(!judge(temp))
     52             {
     53                 continue;
     54             }
     55             if(map[temp.x][temp.y][temp.z]=='E')
     56             {
     57                 printf("Escaped in %d minute(s).\n",temp.time);
     58                 return;
     59             }
     60             flag[temp.x][temp.y][temp.z]=true;
     61             q.push(temp);
     62         }
     63     }
     64     printf("Trapped!\n");
     65 
     66 }
     67 int main()
     68 {
     69     while(~scanf("%d%d%d",&L,&R,&C))
     70     {
     71         if(L==0 && R==0 && C==0)
     72         {
     73             break;
     74         }
     75         int i,j,k;
     76         for(i=0;i<L;i++)
     77         {
     78             for(j=0;j<R;j++)
     79             {
     80                 scanf("%s",map[i][j]);
     81             }
     82         }
     83         for(i=0;i<L;i++)
     84         {
     85             for(j=0;j<R;j++)
     86             {
     87                 for(k=0;k<C;k++)
     88                 {
     89                     if(map[i][j][k]=='S')
     90                     {
     91                         sx=i;
     92                         sy=j;
     93                         sz=k;
     94                     }
     95                 }
     96             }
     97         }
     98         memset(flag,false,sizeof(flag));
     99         bfs();
    100     }
    101     return 0;
    102 }
    View Code

    简单的BFS,

    不过WA了一次,原因是方向数组dir[][][],的移动顺序错了,当时觉得没什么问题,就随便写的顺序,交上去没想到WA了,后来先考虑向上或者向下移动,再交,就AC了,具体什么原因我也不知道怎么解释,看到的人要是清楚,还请大神讲解

  • 相关阅读:
    python+requests+excel 接口测试
    Pycharm配置git
    ubuntu16.04+ROS安装kinectV1
    ubuntu16.04安装有道词典
    ROS kinetic语音识别
    在Ubuntu16.04中python环境下实现tab键补全
    ros kinetic安装rbx1
    ubuntu14.04安装opencv3.1
    ubuntu16.04SSH无法连接
    VC6中函数点go to definition报告the symbol XXX is undefined
  • 原文地址:https://www.cnblogs.com/ouyangduoduo/p/3109319.html
Copyright © 2011-2022 走看看