ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Java 6593 상범빌딩
    PS 2023. 7. 25. 14:49

    https://www.acmicpc.net/problem/6593

     

    6593번: 상범 빌딩

    당신은 상범 빌딩에 갇히고 말았다. 여기서 탈출하는 가장 빠른 길은 무엇일까? 상범 빌딩은 각 변의 길이가 1인 정육면체(단위 정육면체)로 이루어져있다. 각 정육면체는 금으로 이루어져 있어

    www.acmicpc.net

     

    풀이 :

    핵심 로직은 다음과 같습니다.

     

    1. 일반적인 2차원 배열로는 층수를 표현할 수 없기에 3차원 배열로 각층에 대한 정보를 입력받는다.

    2. bfs 함수 내에서는 몇번째에 탈출했는 지 알기위해서 무한루프로 반복문을 진행하며 그 안에서 큐의 사이즈만큼 반복한다.

    3. 미리 정의해둔 dh, dx, dy 배열 정보를 활용하여 지나갈 수 있는 곳이거나, 방문하지 않은 좌표값이라면 큐에 넣어주고 함수가 종료될 때까지 반복한다.

    4. bfs 함수가 종료되는 조건은 두가지이다. 큐에 더 이상 탐색을 진행할 원소가 없는 경우와 출구를 만났을 경우.

         출구를 만났을 경우에는 StringBuilder에 출력할 메시지를 추가해주고 아닐 경우에는 실패시의 메시지를 추가해준다.

    5. 모든 반복문이 끝났을 경우 StringBuilder를 출력해준다.

     

    어려움 없이 풀 수 있었던 문제였습니다.

     

     

     

     

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.IOException;
    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.StringTokenizer;
    
    public class Main {
        static int l,r,c;
        static int[][][] map;
        static boolean[][][] visited;
        static StringBuilder sb;
        static int[] dh = {1, -1, 0, 0, 0 ,0};
        static int[] dx = {0, 0, -1, 0 , 1, 0};
        static int[] dy = {0, 0, 0, 1, 0 , -1};
        static Queue<Node> q;
    
        static Node exit;
    
        static class Node {
            int h;
            int x;
            int y;
    
            public Node(int h, int x, int y){
                this.h = h;
                this.x = x;
                this.y = y;
            }
        }
        public static void main(String[] args) throws IOException {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            sb = new StringBuilder();
            StringTokenizer st;
    
            while (true) {
                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){
                    break;
                }
    
                map = new int[l][r][c];
                visited = new boolean[l][r][c];
                q = new LinkedList<>();
    
                for (int i = 0; i < l; i++) {
                    for (int j = 0; j < r; j++) {
                        String str = br.readLine();
                        for (int k = 0; k < c; k++) {
                            if (str.charAt(k) == '#') {
                                map[i][j][k] = -1;
                            } else if (str.charAt(k) == 'E') {
                                map[i][j][k] = 2;
                                exit = new Node(i, j, k);
                            } else if (str.charAt(k) == 'S') {
                                map[i][j][k] = 1;
                                visited[i][j][k] = true;
                                q.add(new Node(i, j, k));
                            } else {
                                map[i][j][k] = 0;
                            }
                        }
                    }
                    br.readLine();
                }
                bfs();
            }
    
            System.out.println(sb);
    
        }
        static void bfs(){
            int answer = 0;
    
            while(true){
                answer++;
    
                int size = q.size();
    
                while(size --> 0){
                    Node poll = q.poll();
    
                    for(int i = 0; i < 6; i++){
                        int nh = poll.h + dh[i];
                        int nx = poll.x + dx[i];
                        int ny = poll.y + dy[i];
    
                        if(nh < 0 || nx < 0 || ny < 0 || nh >= l || nx >= r || ny >= c){
                            continue;
                        }
                        if(nh == exit.h && nx == exit.x && ny == exit.y){
                            sb.append("Escaped in ").append(answer).append(" minute(s).").append("\n");
                            return;
                        }
                        if(map[nh][nx][ny] == 0 && !visited[nh][nx][ny]){
                            q.add(new Node(nh, nx, ny));
                            visited[nh][nx][ny] = true;
                        }
                    }
                }
                if(q.isEmpty()){
                    sb.append("Trapped!").append("\n");
                    return;
                }
            }
        }
    }

    'PS' 카테고리의 다른 글

    Java 11404 플로이드  (0) 2023.08.08
    Java 16235 나무 재테크  (0) 2023.07.28
    Java 1926 그림  (0) 2023.07.25
    Java 1138 한 줄로 서기  (0) 2023.07.21
    Java 1244 스위치 켜고 끄기  (0) 2023.07.19
Designed by Tistory.