E - Dungeon Master BFS

[NWUACM] 
You are trapped in a three-dimensional space, and now you want to find the shortest path to escape!
Space consists of a cube unit
every time you move around, up and down one unit to take a minute
you can not move the diagonal and fully enclosed
possibility of escape if there is? If so, how much time you need?

Input - Input

  The first input line number indicates the number of a space.
  First described the behavior of each space L, R and C (neither more than 30).
  L represents the height of the space.
  C and R respectively represent the size of each row and column of the space.
  Then the layer L dungeon, each R rows, C characters per line.
  Each character represents a unit space. '#' Is not represented by a unit, '.' Denotes a blank cell. Your starting position in the 'S', exports 'E'.
  After each space has one blank line. L, R and C are all 0 when the input end.

Output - Output

  Each output line corresponds to a space.

  If escape, the output follows

Escaped minute in X (S).

  X from the shortest time.



  If you can not escape, the output follows

Trapped!

Sample Input - Input Sample

3 4 5 
S .... 
. ###. 
. ## .. 
###. # 
##### ##### ##. ## ## ... ##### ##### #. ### #### E 1 3 3 O ## and # ### 0 0 0

Sample Output - Output Sample

In 11 minute ESCAPED (S). 
Trapped! 

Thinking: this topic is BFS 6 directions of the direction set up ,, find a starting point to find the end, then it BFS

#include<iostream>
#include<queue>
#include<cstdio> 
#include<cstdio>
#include<cstring>
#define N 33
//int base[6][3] = { {-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1} };
using namespace std;
int l,n,m;
char arr[N][N][N];
int mark[N][N][N];
int sa,sb,sc;
int ea,eb,ec;
struct stu{
    int a,b,c;//坐标
    int s;//距离
}e1,e2,e3;
int base[6][3] = {{- 1 , 0 , 0 }, { 1 , 0 , 0 }, { 0 - 1 , 0 }, { 0 , 1 , 0 }, { 0 , 0 - 1 }, { 0 , 0 , 1 }}; // six directions
 void the BFS () { 
    Memset (Mark, 0 , the sizeof (Mark)); 
    Queue <STU> S; 
    e1.a = SA, SB = e1.b, e1.c = SC ; 
    e1.s = 0;
    s.push(e1);
    mark[sa][sb][sc]=1;
    
    int ans=-1;
    while(s.size()){
        e2=s.front();
        s.pop();
        if(e2.a==ea && e2.b==eb && e2.c==ec)//判断是否到达了终点
        {
            ans=e2.s;
            break;
        }
        for(int i=0;i<6;i++){
            e3.a=e2.a+base[i][0];
            e3.b=e2.b+base[i][1];
            e3.c=e2.c+base[i][2];
            if((e3.a>= 0) && (e3.a < l) && (e3.b >= 0) && (e3.b < n) && (e3.c >= 0) && (e3.c < m)
             && (!mark[e3.a][e3.b][e3.c]) && (arr[e3.a][e3.b][e3.c] == '.' || arr[e3.a][e3.b][e3.c] == 'E'))
            {
                e3.s=e2.s+1;
                mark[e3.a][e3.b][e3.c]=1;
                s.push(e3);
            }
            
        }
    }
    if(ans==-1){
        cout<<"Trapped!"<<endl;
    }
    else {
        printf("Escaped in %d minute(s).\n",ans);
    }
}

int main()
{
    while(cin>>l>>n>>m){
        if(n==0&&m==0&&l==0)
            break;
        for(int i=0;i<l;i++){
            for(int j=0;j<n;j++){
                scanf("%s",&arr[i][j]);
            }
        }
        for(int i=0;i<l;i++){
            for(int j=0;j<n;j++){
                for(int k=0;k<m;k++){
                    if(arr[i][j][k]=='S')
                    {
                        sa = i; 
                        sb = j; 
                        sc = k; 
                    } 
                    Else  if (arr [i] [j] [k] == ' E ' ) { 
                        ea = i; 
                        eb = j; 
                        H = k; 
                    } 
                } 
            } 
        } 
        BFS (); 
    } 
    Return  0 ; 
}

 




Guess you like

Origin www.cnblogs.com/Accepting/p/11241617.html