728x90
https://www.acmicpc.net/problem/6593
package BKD_0x9_BFS;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class BOJ_6593 {
static int[] dz ={0,0,0,0,1,-1};
static int[] dy ={0,0,1,-1,0,0};
static int[] dx ={1,-1,0,0,0,0};
static int L;
static int R;
static int C;
static Location start;
static char[][][] building;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true){
StringTokenizer st = new StringTokenizer(br.readLine()," ");
L = Integer.parseInt(st.nextToken());
R = Integer.parseInt(st.nextToken());
C = Integer.parseInt(st.nextToken());
if(L+R+C==0) {
br.close();
break;
}
building = new char[L][R][C];
for(int k=0;k<L;k++){
for(int i=0;i<=R;i++){
String input = br.readLine();
if(input.isEmpty()) break;
for(int j=0;j<C;j++){
char c = input.charAt(j);
if(c =='S'){
start = new Location(k,i,j,0);
}
building[k][i][j]=input.charAt(j);
}
}
}
int escape_time = escape(start.z, start.y, start.x);
if(escape_time==-1){
System.out.println("Trapped!");
}else{
System.out.printf("Escaped in %d minute(s).\n",escape_time);
}
}
}
public static int escape(int z, int y, int x){
Queue<Location> q = new LinkedList<>();
q.offer(new Location(z,y,x,0));
building[z][y][x]='#';
int escape_time=-1;
while(!q.isEmpty()){
Location l = q.poll();
for(int i=0;i<6;i++){
int nz = l.z + dz[i];
int ny = l.y + dy[i];
int nx = l.x + dx[i];
if(nz>=0 && ny>=0 && nx >= 0){
if(nz<L && ny<R && nx<C){
if(building[nz][ny][nx]=='E'){
escape_time = l.l+1;
q.clear();
break;
}
if(building[nz][ny][nx]=='.'){
q.offer(new Location(nz,ny,nx,l.l+1));
building[nz][ny][nx]='#';
}
}
};
}
}
return escape_time;
}
static class Location{
int z;
int y;
int x;
int l;
public Location(int z, int y, int x, int l){
this.z =z;
this.y = y;
this.x = x;
this.l =l;
}
}
}
728x90
'🐣 알고리즘 삐약 > 💻 백준 삐약' 카테고리의 다른 글
66 삐약 : 백준 13549| 숨바꼭질3 [바킹독 문제 풀이|BFS|JAVA] (0) | 2024.06.04 |
---|---|
65 삐약 : 백준 5014| 스타트 링크 [바킹독 문제 풀이|BFS|JAVA] (0) | 2024.06.04 |
63 삐약 : 백준 10026| 적록색약 [바킹독 문제 풀이|BFS|JAVA] (0) | 2024.06.03 |
62 삐약 : 백준 7569| 토마토 [바킹독 문제 풀이|BFS|JAVA] (0) | 2024.06.03 |
61 삐약 : 백준 7576| 토마토 [바킹독 문제 풀이|BFS|JAVA] (0) | 2024.05.31 |