728x90
https://www.acmicpc.net/problem/6593
풀이
우리가 확인해야할 공간이 3차원이기 때문에 3차원 배열을 사용해서 map을 생성해줬다.
문제를 푸는 데는 어려움이 없었지만, map에 입력값 받아오는게 까다로왔음..
움직임은 상 하 좌 우 아래층 윗층 이렇게 6가지로 움직일 수 있고,
Node를 사용한 LinkedList BFS로 문제를 해결했다.
전체코드
package CodingTest;
import java.io.*;
import java.util.*;
public class 골드5_6593_상범빌딩 {
static int L, R, C, ans;
static char map[][][];
static boolean visited[][][];
static StringBuilder sb = new StringBuilder();
//상하좌우 위층 아래층
static int moveF[] = {0, 0, 0, 0, -1, 1};
static int moveR[] = {-1, 1, 0, 0, 0, 0};
static int moveC[] = {0, 0, -1, 1, 0, 0};
static class Node{
int f;
int x;
int y;
int cnt;
Node(int f, int x, int y, int cnt){
this.f = f;
this.x = x;
this.y = y;
this.cnt = cnt;
}
}
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
while(true) {
StringTokenizer st = new StringTokenizer(br.readLine());
L = Integer.parseInt(st.nextToken()); //층수
R = Integer.parseInt(st.nextToken()); //행
C = Integer.parseInt(st.nextToken()); //열
ans = 0;
if(L == 0 && R == 0 && C == 0) {
break;
}
map = new char[L][R][C];
visited = new boolean[L][R][C];
//map 셋팅
int floor = 0;
int c = 0;
while(true) {
String tmp = br.readLine();
if(tmp.length() == 0) {
if(floor == L) {
break;
}
continue;
}
if(c == R-1) {
map[floor][c] = tmp.toCharArray();
floor++;
c = 0;
continue;
}
map[floor][c] = tmp.toCharArray();
c++;
}
//시작점 찾기
for(int i=0; i<L; i++) {
for(int j=0; j<R; j++) {
for(int k=0; k<C; k++) {
if(map[i][j][k] == 'S') {
Escape(i, j, k);
}else {
continue;
}
}
}
}
if(ans > 0) {
sb.append("Escaped in ").append(ans).append(" minute(s).").append('\n');
}else {
sb.append("Trapped!").append('\n');
}
}
bw.write(sb.toString());
bw.close();
}
public static void Escape(int f, int x, int y) {
Queue<Node> q = new LinkedList<>();
q.offer(new Node(f, x, y, 0));
visited[f][x][y] = true;
while(!q.isEmpty()) {
Node n = q.poll();
if(map[n.f][n.x][n.y] == 'E') {
ans = n.cnt;
}
for(int i=0; i<6; i++) {
int newF = moveF[i] + n.f;
int newX = moveR[i] + n.x;
int newY = moveC[i] + n.y;
if(newF >= L || newF < 0 || newX >= R || newX < 0 || newY >= C || newY < 0
|| map[newF][newX][newY] == '#' || visited[newF][newX][newY]) {
continue;
}
visited[newF][newX][newY] = true;
q.offer(new Node(newF, newX, newY, n.cnt + 1));
}
}
}
}
728x90
'코딩테스트 > Algorithm' 카테고리의 다른 글
[BOJ] 2512: 예산 (JAVA) (0) | 2024.06.21 |
---|---|
[BOJ] 2805: 나무 자르기 (JAVA) (0) | 2024.06.21 |
[BOJ] 2668: 숫자 고르기 (JAVA) (0) | 2024.06.20 |
[Programmers] 최솟값 만들기 (JAVA) (0) | 2024.06.19 |
[Programmers] 더 맵게 (JAVA) (0) | 2024.06.18 |