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!

    开三维数组用BFS求。
     1 #include<cstdio>
     2 #include<queue>
     3 using namespace std;
     4 int dx[6]={-1,1,0,0,0,0};
     5 int dy[6]={0,0,-1,1,0,0};
     6 int dz[6]={0,0,0,0,1,-1};
     7 char str[100][100][100];
     8 int l,r,c,i,j,k,ans,bx,bz,by;
     9 struct stu
    10 {
    11     int x,y,z,step;
    12 };
    13 bool f(stu st)
    14 {
    15     if(st.x<0 || st.z<0 || st.y<0 || st.x>=r || st.y>=c || st.z>=l || str[st.z][st.x][st.y] =='#')
    16     {
    17         return false;
    18     }
    19     return true;
    20 }
    21 int bfs(int bz,int bx,int by)
    22 {
    23     str[bz][bx][by]='#';
    24     int nx,ny,time,zz,xx,yy;
    25     queue<stu> que;
    26     stu st,next;
    27     st.x=bx;
    28     st.y=by;
    29     st.z=bz;
    30     st.step=0;
    31     que.push(st);
    32 
    33     while(!que.empty())
    34     {    
    35     
    36         st=que.front();
    37         que.pop();
    38         
    39         xx=st.x;
    40         yy=st.y;
    41         zz=st.z;
    42         time=st.step;
    43         for(i = 0 ;i < 6 ; i++)
    44         {
    45             st.x=xx+dx[i];
    46             st.y=yy+dy[i];
    47             st.z=zz+dz[i];
    48             if(!f(st))
    49             {
    50                 continue;
    51             }
    52             if(str[st.z][st.x][st.y] == 'E')
    53             {
    54                 return time+1;
    55             }
    56 
    57                 st.step=time+1;
    58                 que.push(st);
    59                 str[st.z][st.x][st.y]='#';
    60         }
    61     }
    62     return 0;
    63 }
    64 int main()
    65 {
    66 while(scanf("%d %d %d",&l,&r,&c)&&l&&r&&c)
    67 {
    68     for(i = 0 ; i < l ; i++)
    69     {
    70         for(j = 0 ; j < r ;j++)
    71         {
    72             scanf("%s",str[i][j]);
    73             for(k = 0 ; k < c ; k++)
    74             {
    75                 if(str[i][j][k] == 'S')
    76                 {
    77                     bz=i; 
    78                      bx=j;
    79                      by=k;
    80                 }
    81             }
    82         }
    83     }
    84     ans=bfs(bz,bx,by);
    85     if(ans)
    86         printf("Escaped in %d minute(s).
    ",ans);
    87     else
    88         printf("Trapped!
    ");
    89 
    90 }
    91 }
    ——将来的你会感谢现在努力的自己。
  • 相关阅读:
    第三次上机练习
    第三次作业
    第二次上级练习
    第二次作业
    第一次上机练习
    第一次作业
    4.20
    4.16
    4.10
    4.9
  • 原文地址:https://www.cnblogs.com/yexiaozi/p/5720197.html
Copyright © 2011-2022 走看看