[BOJ] 6593: 상범 빌딩 (JAVA)

2024. 6. 20. 11:00·코테/Algorithm
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
'코테/Algorithm' 카테고리의 다른 글
  • [BOJ] 2512: 예산 (JAVA)
  • [BOJ] 2805: 나무 자르기 (JAVA)
  • [BOJ] 2668: 숫자 고르기 (JAVA)
  • [Programmers] 최솟값 만들기 (JAVA)
DROPDEW
DROPDEW
💻 Developer | 기록하지 않으면 존재하지 않는다
  • DROPDEW
    제 2장 1막
    DROPDEW
  • 전체
    오늘
    어제
    • Dev (417)
      • App·Android (1)
      • BE (44)
        • HTTP 웹 기본 지식 (8)
        • 스프링 입문 - 코드로 배우는 스프링 부트, 웹 .. (12)
        • 스프링부트와 JPA 활용 (11)
        • 스프링부트 시큐리티 & JWT (0)
        • PHP (6)
      • FE·Client (23)
        • HTML (1)
        • React (19)
        • Unity (1)
      • Data (17)
        • AI (7)
        • Bigdata (6)
        • Database (1)
        • 빅데이터분석기사 (2)
      • Infra (0)
      • Activity (0)
        • Education (0)
        • Intern (0)
        • 리모트 인턴십 6기 (0)
        • 구름톤 유니브 4기 (0)
        • SW교육기부단 15기 (0)
      • CS (8)
      • 취준 (13)
        • 자격증 (4)
        • 인적성·NCS (6)
        • 코테·필기·면접 후기 (3)
      • 코테 (270)
        • Algorithm (222)
        • SQL (35)
        • 정리 (13)
      • 인사이트 (27)
        • 회고 (0)
        • 금융경제뉴스 (7)
        • 금융용어·지식 (2)
        • 북마크 (7)
  • 블로그 메뉴

    • 홈
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    자료구조
    최단경로
    구현
    브루트포스 알고리즘
    시뮬레이션
    투포인터
    그래프탐색
    백준
    다이나믹프로그래밍
    이분탐색
    매개변수탐색
    누적합
    문자열
    오블완
    티스토리챌린지
    정렬
    그리디알고리즘
    그래프이론
    수학
    너비우선탐색
  • 최근 댓글

  • 최근 글

  • 250x250
  • hELLO· Designed By정상우.v4.10.3
DROPDEW
[BOJ] 6593: 상범 빌딩 (JAVA)
상단으로

티스토리툴바