ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [프로그래머스] 미로탈출
    알고리즘 2023. 8. 7. 18:19

    문제

    1 x 1 크기의 칸들로 이루어진 직사각형 격자 형태의 미로에서 탈출하려고 합니다.
    각 칸은 통로 또는 벽으로 구성되어 있으며, 벽으로 된 칸은 지나갈 수 없고 통로로 된 칸으로만 이동할 수 있습니다.

    통로들 중 한 칸에는 미로를 빠져나가는 문이 있는데, 이 문은 레버를 당겨서만 열 수 있습니다. 레버 또한 통로들 중 한 칸에 있습니다.

    따라서, 출발 지점에서 먼저 레버가 있는 칸으로 이동하여 레버를 당긴 후 미로를 빠져나가는 문이 있는 칸으로 이동하면 됩니다.

    이때 아직 레버를 당기지 않았더라도 출구가 있는 칸을 지나갈 수 있습니다. 미로에서 한 칸을 이동하는데 1초가 걸린다고 할 때,
    최대한 빠르게 미로를 빠져나가는데 걸리는 시간을 구하려 합니다.

    미로를 나타낸 문자열 배열 maps가 매개변수로 주어질 때, 미로를 탈출하는데 필요한 최소 시간을 return 하는 solution 함수를 완성해주세요.

    만약, 탈출할 수 없다면 -1을 return 해주세요.

    풀이

    문제를 보면 map이 주어지고 이를 탐색하는 것이므로 bfs를 이용해야 겠다고 생각했다.
    탈출 하려면 우선 레버를 먼저 열어야 하므로 레버까지 도달해야 한다.
    레버를 열었으면 바로 탈출위치로 이동하면 된다.
    즉, 출발위치 -> 레버 -> 탈출위치 로 탐색하면 된다.

    bfs를 두번 이용해서 탐색을 하였다.
    이때 visited는 두번에 탐색에 맞추어 따로 만들어주어서 적용시켜야 한다.

    
    import java.lang.*;
    import java.util.*;
    
    class Solution {
        static String[][] map;
        static int[] x_loc = {1, -1, 0, 0};
        static int[] y_loc = {0, 0, -1, 1};
        public int solution(String[] maps) {
            int answer = 0;
            map = new String[maps.length][maps[0].length()];
            boolean[][] leverVisited = new boolean[maps.length][maps[0].length()];
            boolean[][] endVisited = new boolean[maps.length][maps[0].length()];
    
            int xLever = 0;
            int yLever = 0;
            int xE = 0;
            int yE = 0;
    
            for(int i = 0; i < maps.length; i++) {
                String s = maps[i];
                for(int j = 0; j < s.length(); j++) {
                    map[i][j] = s.substring(j, j + 1);
                    if (map[i][j].equals("L")) {
                        xLever = i;
                        yLever = j;
                    }
    
                    if (map[i][j].equals("E")) {
                        xE = i;
                        yE = j;
                    }
                }
            }
    
            for(int i = 0; i < map.length; i++) {
                String[] s = map[i];
                for(int j = 0; j < s.length; j++) {
                    if (map[i][j].equals("S")) {
                        answer = bfs(i, j, xLever, yLever, leverVisited);
                        System.out.println(answer);
                        break;
                    }
                }
            }
    
            if (answer > -1) {
                int tmp = bfs(xLever, yLever, xE, yE, endVisited);
                if (tmp == -1) {
                    return tmp;
                } else {
                    answer += tmp;
                }
            }
    
            return answer;
        }
    
        static int bfs(int x, int y, int xEnd, int yEnd, boolean[][] visited) {
            Queue<Location> queue = new LinkedList<>();
            queue.add(new Location(x, y, 0));
            visited[x][y] = true;
    
            while(!queue.isEmpty()) {
                Location loc = queue.poll();
    
                for(int i = 0; i < 4; i++) {
                    int xLoc = loc.x + x_loc[i];
                    int yLoc = loc.y + y_loc[i];
    
                    if (xLoc == xEnd && yLoc == yEnd) {
                        return loc.count + 1;
                    }
    
                    if (check(xLoc, yLoc, visited)) {
                        visited[xLoc][yLoc] = true;
                        queue.add(new Location(xLoc, yLoc, loc.count + 1));
                    }
                }
            }
    
            return -1;
        }
    
        static boolean check(int x, int y, boolean[][] visited) {
            return (x >= 0 && x < map.length) && (y >= 0 && y < map[0].length) 
                && !map[x][y].equals("X") && !visited[x][y];
        }
    
        static class Location {
            private int x;
            private int y;
            private int count;
    
            public Location(int x,int y, int count) {
                this.x = x;
                this.y = y;
                this.count = count;
            }
        }
    }

    https://github.com/Win-9/Algorism/tree/main/programers/%EB%AF%B8%EB%A1%9C%ED%83%88%EC%B6%9C

    728x90

    댓글

Designed by Tistory.