#include <iostream>
#include <string>
#include <queue>
#include <algorithm>

using namespace std;
#define X first
#define Y second
//순서 중요!
struct Pos {
int z;
int y;
int x;
};

int dx[6]={1,0,0,-1,0,0};
int dy[6]={0,1,0,0,-1,0};
int dz[6]={0,0,1,0,0,-1};

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    while(1){
        int x,y,z;
        cin >> z>> y >> x;
        if(x==0  &&y==0 && z==0) return 0;
        string board[32][32];
        int dist[32][32][32];
        //-1로 초기화
        fill(&dist[0][0][0],&dist[0][0][0]+32 * 32 * 32,-1);
        for(int i=0; i<z; i++) {
            for(int j=0; j<y; j++) {
                cin >> board[i][j];
            }
        }
        queue<Pos> q;
        //시작점 찾기
        for(int i=0; i<z; i++) {
            for(int j=0;j<y;j++){
                for(int k=0; k<x; k++) {
                    if(board[i][j][k]=='S') {
                        dist[i][j][k]=0;
                        q.push({i,j,k});
                    } 
                }
            }
        }
        int flag=0;
        while(!q.empty()) {
            if(flag) break;
            Pos cur=q.front();q.pop();
            for(int idx=0;idx<6; idx++) {
                int nx=cur.x+dx[idx];
                int ny=cur.y+dy[idx];
                int nz=cur.z+dz[idx];

                if(nx<0||nx>=x|| ny<0|| ny>=y || nz<0 || nz>=z) continue;
                //목표 도착
                if(board[nz][ny][nx]=='E') {
                    cout << "Escaped in " <<dist[cur.z][cur.y][cur.x] +1 <<" minute(s).\n";
                    flag=1;
                    break;
                }
                if(board[nz][ny][nx]=='#' || dist[nz][ny][nx]>=0) continue;
                dist[nz][ny][nx]=dist[cur.z][cur.y][cur.x]+1;
                q.push({nz,ny,nx});    
            }
        }
        if(!flag) cout << "Trapped!\n";
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: