Skip to content

Instantly share code, notes, and snippets.

@midaslmg94
Last active June 23, 2020 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save midaslmg94/421eab3cdef7441ee06395e01038f88a to your computer and use it in GitHub Desktop.
Save midaslmg94/421eab3cdef7441ee06395e01038f88a to your computer and use it in GitHub Desktop.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
// 상범빌딩
class INFO{
int y;
int x;
int f; // 현재 몇 층인지
int t; // 여기까지 오는데 걸린 시간
INFO(int y, int x, int f, int t){
this.y=y;
this.x=x;
this.f=f;
this.t=t;
}
}
public class BOJ_5568 {
// 전역변수 선언
static int L,R,C;
static char [][][] building;
static boolean [][][] visited;
static int[] dy = {0, 0, -1, 1, 0, 0};
static int[] dx = {1, -1, 0, 0, 0, 0};
static int[] df = {0, 0, 0, 0, 1, -1};
static Queue q;
public static int bfs(INFO start){
Queue<INFO>q = new LinkedList<>();
q.add(start);
visited[start.y][start.x][start.f] = true;
while (!q.isEmpty()){
INFO tmp = q.poll();
int y = tmp.y;
int x = tmp.x;
int f = tmp.f;
int t = tmp.t;
if(building[y][x][f]=='E') return t;
for(int dir = 0; dir<6; dir++){
int ny=y+dy[dir];
int nx=x+dx[dir];
int nf=f+df[dir];
if (0 > ny || ny > R - 1 || 0 > nx || nx > C - 1 || 0 > nf || nf > L - 1 || visited[ny][nx][nf]) continue;
if (building[ny][nx][nf] == '#') continue;
visited[ny][nx][nf] = true;
q.add(new INFO(ny,nx,nf,t+1));
}
}
return 0;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // 입력을 위한 선언
while (true){
StringTokenizer st = new StringTokenizer(br.readLine());
if(!st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
L = Integer.parseInt(st.nextToken());
R = Integer.parseInt(st.nextToken());
C = Integer.parseInt(st.nextToken());
if (L == 0 && R == 0 & C == 0) break;
building = new char[31][31][31];
visited = new boolean[31][31][31];
INFO start = null;
for(int k=0; k<L; k++){
for(int i=0; i<R; i++){
String str = br.readLine();
if(str.equals(""))
str = br.readLine(); // 공백이 들어오면 처리?..
for(int j=0; j<C; j++){
char ch = str.charAt(j);
if(ch == 'S'){
start = new INFO(i,j,k,0);
}
building[i][j][k] = ch;
}
}
}
int res = bfs(start);
if (res != 0) {
System.out.println("Escaped in "+res+" minute(s).");
} else {
System.out.println("Trapped!");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment