1 x 1 크기의 칸들로 이루어진 직사각형 격자 형태의 미로에서 탈출하려고 합니다. 각 칸은 통로 또는 벽으로 구성되어 있으며, 벽으로 된 칸은 지나갈 수 없고 통로로 된 칸으로만 이동할 수 있습니다. 통로들 중 한 칸에는 미로를 빠져나가는 문이 있는데, 이 문은 레버를 당겨서만 열 수 있습니다. 레버 또한 통로들 중 한 칸에 있습니다. 따라서, 출발 지점에서 먼저 레버가 있는 칸으로 이동하여 레버를 당긴 후 미로를 빠져나가는 문이 있는 칸으로 이동하면 됩니다. 이때 아직 레버를 당기지 않았더라도 출구가 있는 칸을 지나갈 수 있습니다. 미로에서 한 칸을 이동하는데 1초가 걸린다고 할 때, 최대한 빠르게 미로를 빠져나가는데 걸리는 시간을 구하려 합니다.
미로를 나타낸 문자열 배열 maps가 매개변수로 주어질 때, 미로를 탈출하는데 필요한 최소 시간을 return 하는 solution 함수를 완성해주세요. 만약, 탈출할 수 없다면 -1을 return 해주세요.

제한사항
5 ≤ maps의 길이 ≤ 100
5 ≤ maps[i]의 길이 ≤ 100
maps[i]는 다음 5개의 문자들로만 이루어져 있습니다.
S : 시작 지점
E : 출구
L : 레버
O : 통로
X : 벽
시작 지점과 출구, 레버는 항상 다른 곳에 존재하며 한 개씩만 존재합니다.
출구는 레버가 당겨지지 않아도 지나갈 수 있으며, 모든 통로, 출구, 레버, 시작점은 여러 번 지나갈 수 있습니다.
입출력 예
maps	result
["SOOOL","XXXXO","OOOOO","OXXXX","OOOOE"]	16
["LOOXS","OOOOX","OOOOO","OOOOO","EOOOO"]	-1
import java.util.ArrayDeque;
import java.util.Queue;
class Solution {
    public int solution(String[] maps) {
        int n = maps.length;   //maps의 배열 길이만큼 판을 세로칸을 만든다
        int m = maps[0].length(); //maps[0]의 문자열 길이만큼 가로칸을 만든다.

        // 시작 지점, 출구, 레버의 위치를 찾습니다./시작지점과 출구 레버 위치는 매번 다르기 때문이다
        int[] start = null;
        int[] exit = null;
        int[] lever = null;

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (maps[i].charAt(j) == 'S') {
                    start = new int[]{i, j};
                } else if (maps[i].charAt(j) == 'E') {
                    exit = new int[]{i, j};
                } else if (maps[i].charAt(j) == 'L') {
                    lever = new int[]{i, j};
                }
            }
        }

        // BFS를 위한 방향을 정의합니다.
        int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

   

        // 시작 지점부터 레버까지의 최단 경로와 레버부터 출구까지의 최단 경로를 찾습니다.
        int startToLever = bfs(start, lever, maps, n, m, directions);
        int leverToExit = bfs(lever, exit, maps, n, m, directions);

        // 탈출이 불가능한 경우 -1을 반환합니다.
        if (startToLever == -1 || leverToExit == -1) {
            
            return -1;
        }
        else{
            return startToLever + leverToExit;  // 최단 경로의 합을 반환합니다.

        }
    }
     static int bfs(int[] start, int[] target, String[] maps, int n, int m, int[][] directions) {
        boolean[][] visited = new boolean[n][m];
        Queue<int[]> queue = new ArrayDeque<>();
        queue.add(new int[]{start[0], start[1], 0});
        visited[start[0]][start[1]] = true;

        while (!queue.isEmpty()) {
            int[] current = queue.poll();
            int x = current[0];
            int y = current[1];
            int steps = current[2]; //움직인 숫자

            if (x == target[0] && y == target[1]) {
                return steps;
            }

            for (int[] direction : directions) {
                int newX = x + direction[0];
                int newY = y + direction[1];

                if (newX >= 0 && newX < n && newY >= 0 && newY < m && !visited[newX][newY] && maps[newX].charAt(newY) != 'X') {
                    queue.add(new int[]{newX, newY, steps + 1});
                    visited[newX][newY] = true;
                }
            }
        }

        return -1;
    }
}

+ Recent posts