zoukankan      html  css  js  c++  java
  • POJ 2251 三维BFS(基础题)

    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!
    

    Source

    思路:比较二维的增加两种转移方式,其他基本不变。
    代码:
     1 #include "cstdio"
     2 #include "stdlib.h"
     3 #include "iostream"
     4 #include "algorithm"
     5 #include "string"
     6 #include "cstring"
     7 #include "queue"
     8 #include "cmath"
     9 #include "vector"
    10 #include "map"
    11 #include "set"
    12 #define mj
    13 #define db double
    14 #define ll long long
    15 using namespace std;
    16 const int N=1e8+5;
    17 const int mod=1e9+7;
    18 const ll inf=1e16+10;
    19 bool v[35][35][35];
    20 int dx[6]={0,0,1,0,-1,0},dy[6]={0,0,0,1,0,-1},dz[6]={-1,1,0,0,0,0};
    21 int a,b,c;
    22 char s[35][35][35];
    23 int d[35][35][35];
    24 typedef struct P{
    25     int x,y,z;
    26     P(int c,int d,int e){
    27         z=c,x=d,y=e;
    28     }
    29 };
    30 void bfs(P t){
    31     queue<P> q;
    32     for(int i=0;i<35;i++)
    33         for(int j=0;j<35;j++)
    34             for(int k=0;k<35;k++)
    35                 d[i][j][k]=N;
    36     q.push(t);
    37     memset(v,0, sizeof(v));
    38     d[t.z][t.x][t.y]=0;
    39     v[t.z][t.x][t.y]=1;
    40     while(q.size()){
    41         P p=q.front();
    42         q.pop();
    43         if(s[p.z][p.x][p.y]=='E'){
    44             printf("Escaped in %d minute(s).
    ",d[p.z][p.x][p.y]);
    45             return;
    46         }
    47 
    48         for(int i=0;i<6;i++){
    49             int nx=p.x+dx[i],ny=p.y+dy[i],nz=p.z+dz[i];
    50             if(0<=nx&&nx<b&&0<=ny&&ny<c&&0<=nz&&nz<a&&s[nz][nx][ny]!='#'&&!v[nz][nx][ny]){
    51                 d[nz][nx][ny]=d[p.z][p.x][p.y]+1;
    52                 q.push(P(nz,nx,ny));
    53                 v[nz][nx][ny]=1;
    54 
    55             }
    56         }
    57 
    58     }
    59     printf("Trapped!
    ");
    60 
    61 
    62 }
    63 int main()
    64 {
    65     while(scanf("%d%d%d",&a,&b,&c)==3,a||b||c){
    66         int t=a;
    67         int x,y,z;
    68         memset(s,'', sizeof(s));// 一开始把x,y,z搞反了,调了好一会
    69         for(int i=0;i<t;i++){
    70             for(int j=0;j<b;j++){
    71                 scanf("%s",s[i][j]);
    72                 for(int k=0;k<c;k++)
    73                     if(s[i][j][k]=='S') x=j,y=k,z=i;
    74             }
    75         }
    76         bfs(P(z,x,y));
    77     }
    78     return 0;
    79 }
  • 相关阅读:
    [刘阳Java]_eayui-pagination分页组件_第5讲
    [刘阳Java]_easyui-draggable拖动组件_第4讲
    [刘阳Java]_easyui-panel组件入门级_第3讲
    [刘阳Java]_TortoiseSVN基础应用_第1讲
    [刘阳Java]_SpringMVC文件上传第2季_第11讲
    [刘阳Java]_Spring中IntrospectorCleanupListener的用途【补充】_第16讲
    使用fetch代替ajax请求 post传递方式
    react购物车demo
    react-redux异步数据操作
    redux模块化demo
  • 原文地址:https://www.cnblogs.com/mj-liylho/p/7183869.html
Copyright © 2011-2022 走看看