반응형
import java.util.*;
import java.io.*;
public class Main {
static int L, R, C;
static int[] dx = new int[]{1, 0, 0, -1, 0, 0};
static int[] dy = new int[]{0, 1, 0, 0, -1, 0};
static int[] dz = new int[]{0, 0, 1, 0, 0, -1};
static String[][][] building;
static Queue<int[]> Q;
static int[][][] dist;
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true){
StringTokenizer st = new StringTokenizer(br.readLine());
Q = new LinkedList<>();
L = Integer.parseInt(st.nextToken());
R = Integer.parseInt(st.nextToken());
C = Integer.parseInt(st.nextToken());
if (L+R+C==0) System.exit(0);
building = new String[L][R][C];
dist = new int[L][R][C];
for (int i=0; i<L; i++) {
for (int j=0; j<R; j++) {
building[i][j] = br.readLine().split("");
Arrays.fill(dist[i][j], -1);
for (int k=0; k<C; k++)
if (building[i][j][k].equals("S")){
Q.offer(new int[]{i, j, k});
dist[i][j][k] = 0;
}
}
br.readLine();
}
bfs();
}
}
static void bfs(){
while (!Q.isEmpty()){
int[] cur = Q.poll();
for (int dir=0; dir<6; dir++){
int nx = cur[2] + dx[dir];
int ny = cur[1] + dy[dir];
int nz = cur[0] + dz[dir];
if (nx<0||ny<0||nz<0 || nx>=C||ny>=R||nz>=L) continue;
if (dist[nz][ny][nx]>=0 || building[nz][ny][nx].equals("#")) continue;
if (building[nz][ny][nx].equals("E")){
System.out.println("Escaped in "+(dist[cur[0]][cur[1]][cur[2]]+1)+" minute(s).");
return;
}
dist[nz][ny][nx] = dist[cur[0]][cur[1]][cur[2]]+1;
Q.offer(new int[]{nz, ny, nx});
}
}
System.out.println("Trapped!");
}
}
반응형
'Algorithm' 카테고리의 다른 글
백준 13913번 숨바꼭질4 [ Java ] (0) | 2021.01.03 |
---|---|
백준 13549번 숨바꼭질 3 [ Java ] (0) | 2021.01.03 |
백준 2573번 빙산 [ Java ] (0) | 2021.01.02 |
백준 5014번 스타트링크 [ Java ] (0) | 2021.01.02 |
백준 2468번 안전 영역 [ Java ] (0) | 2021.01.02 |