• poj 2251 Dungeon Master (BFS 三维)


    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 <cstdio>
     3 #include<algorithm>
     4 #include<string>
     5 #include<cstring>
     6 #include<queue>
     7 using namespace std;
     8 
     9 int h,m,n,res;
    10 char ma[35][35][35];
    11 bool vis[35][35][35];
    12 int rx[]={0,0,0,0,1,-1};
    13 int ry[]={0,0,1,-1,0,0};
    14 int rz[]={1,-1,0,0,0,0};
    15 
    16 struct node
    17 {
    18     int x,y,z;
    19     int t;
    20 }per;
    21 int ex,ey,ez;
    22 
    23 bool judge(node a)
    24 {
    25     if(a.x>=0&&a.x<m&&a.y>=0&&a.y<n&&a.z>=0&&a.z<h)
    26         if(!vis[a.x][a.y][a.z])
    27             if(ma[a.x][a.y][a.z]!='#')
    28             return true;
    29     return false;
    30 }
    31 
    32 bool bfs()
    33 {
    34     queue<node>Q;
    35     memset(vis,false,sizeof(vis));
    36     Q.push(per);
    37     vis[per.x][per.y][per.z]=true;
    38     node tmp,next;
    39     while(!Q.empty())
    40     {
    41         tmp=Q.front();
    42         Q.pop();
    43         if(tmp.x==ex&&tmp.y==ey&&tmp.z==ez)
    44         {
    45             res=tmp.t;
    46             return true;
    47         }
    48         for(int i=0;i<6;i++)
    49         {
    50             next.x=tmp.x+rx[i];
    51             next.y=tmp.y+ry[i];
    52             next.z=tmp.z+rz[i];
    53             next.t=tmp.t+1;
    54             if(judge(next))
    55             {
    56                 Q.push(next);
    57                 vis[next.x][next.y][next.z]=true;
    58             }
    59         }
    60     }
    61     return false;
    62 }
    63 
    64 int main()
    65 {
    66     while(~scanf("%d%d%d
    ",&h,&m,&n)&&(h+m+n))
    67     {
    68         for(int q=0;q<h;q++)
    69         for(int i=0;i<m;i++)
    70         for(int j=0;j<n;j++)
    71         {
    72             cin>>ma[i][j][q];
    73             if(ma[i][j][q]=='S')
    74             {
    75                 per.x=i;per.y=j;per.z=q;
    76                 per.t=0;
    77             }
    78             else if(ma[i][j][q]=='E')
    79             {
    80                 ex=i;ey=j;ez=q;
    81             }
    82         }
    83         //
    84         if(bfs()) printf("Escaped in %d minute(s).
    ",res);
    85         else printf("Trapped!
    ");
    86     }
    87     return 0;
    88 }
    
    
    


  • 相关阅读:
    微信小程序之base64转为本地图片
    微信小程序之页面跳转方法
    一些收集的社区网址+学习网站、文档
    11-DOM介绍
    10-关于DOM的事件操作
    09-伪数组 arguments
    前端---css
    前端---html
    并发编程------网络IO模型、IO多路复用
    并发编程------协程
  • 原文地址:https://www.cnblogs.com/Annetree/p/7206018.html
Copyright © 2020-2023  润新知