문제:
https://www.acmicpc.net/problem/6593
6593번: 상범 빌딩
당신은 상범 빌딩에 갇히고 말았다. 여기서 탈출하는 가장 빠른 길은 무엇일까? 상범 빌딩은 각 변의 길이가 1인 정육면체(단위 정육면체)로 이루어져있다. 각 정육면체는 금으로 이루어져 있어
www.acmicpc.net
코드:
import java.util.*;
public class Main {
public static int [][][] board = new int[32][32][32];
public static int [][][] dist = new int[32][32][32];
public static int n,m,h; // n > 행 m > 열 h > 높
public static int [] dx = {1,0,0,-1,0,0};
public static int [] dy = {0,1,0,0,-1,0};
public static int [] dz = {0,0,1,0,0,-1};
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(true) {
h = sc.nextInt(); // 층 L
n = sc.nextInt(); // 행 R
m = sc.nextInt(); // 열 C
if(h == 0 && n == 0 && m == 0) System.exit(0);
// 0 0 0 일 경우 시스템 종료
Queue<Pos> q = new LinkedList<>();
for(int k=0;k<h;k++) {
for(int i=0;i<n;i++) {
String s = sc.next();
for(int j=0;j<m;j++) {
if(s.charAt(j) == 'S') {
// 시작지점 add
q.add(new Pos(i,j,k));
dist[i][j][k] = 0;
board[i][j][k] = 'S';
}else {
// 그 외는 모두 -1
dist[i][j][k] = -1;
board[i][j][k] = s.charAt(j);
}
}
}
}
// 탈출 판단용 boolean, true시 탈출 불가능
boolean flag = true;
OutLoop:
while(!q.isEmpty()) {
Pos cur = q.poll();// 큐에서 하나 뽑아냄
for(int dir = 0;dir<6;dir++) {
int nx = cur.x + dx[dir];
int ny = cur.y + dy[dir];
int nz = cur.z + dz[dir];
if(nx < 0 || nx >=n || ny < 0 || ny >= m || nz < 0 || nz >= h) continue;
if(dist[nx][ny][nz] >= 0 || board[nx][ny][nz] == '#') continue;
if(board[nx][ny][nz] == 'E') {
//탈출 위치 도달하면 시간 출력후 탈출
System.out.println("Escaped in "+(dist[cur.x][cur.y][cur.z]+1)+" minute(s).");
flag = false;
break OutLoop;
}
dist[nx][ny][nz] = dist[cur.x][cur.y][cur.z]+1;
q.add(new Pos(nx,ny,nz));
}
}
if(flag) {
System.out.println("Trapped!");
}
}
}
}
class Pos{
int x;
int y;
int z;
Pos(int x,int y,int z){
this.x = x;
this.y = y;
this.z = z;
}
}
'Java > 백준' 카테고리의 다른 글
22 8 19 백준 골드 (0) | 2022.08.19 |
---|---|
[Java][2583] 영역 구하기 (0) | 2022.08.19 |
[Java][7562] 나이트의 이동 (0) | 2022.08.17 |
[Java][10026] 적록색약 (0) | 2022.08.17 |
[Java][6588] 골드바흐의 추측 (1) | 2022.08.14 |