1926. Nearest Exit from Entrance in Maze

양성준·2025년 6월 17일

코딩테스트

목록 보기
82/102

문제

https://leetcode.com/problems/nearest-exit-from-entrance-in-maze/description/

풀이

class Solution {
    int[] dx = {1,0,-1,0};
    int[] dy = {0,1,0,-1};
    public int nearestExit(char[][] maze, int[] entrance) {
        Queue<Node> queue = new LinkedList<>();

        int level = 1;
        queue.add(new Node(entrance[0], entrance[1]));
        maze[entrance[0]][entrance[1]] = '+';

        while(!queue.isEmpty()) {
            int size = queue.size();
            for(int i = 0; i < size; i++) {
                Node node = queue.poll();
                for(int j = 0; j < 4; j++) {
                    int nx = node.x + dx[j];
                    int ny = node.y + dy[j];
                    if(nx >= 0 && nx < maze.length && ny >= 0 && ny < maze[0].length && maze[nx][ny] == '.') {
                        if(nx == 0 || nx == maze.length - 1 || ny == 0 || ny == maze[0].length - 1) {
                            return level;
                        }
                        maze[nx][ny] = '+';
                        queue.add(new Node(nx, ny));
                    }
                }
            }
            level++;
        }
        return -1;
    }
}

class Node {
    int x;
    int y;

    public Node(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
profile
백엔드 개발자

0개의 댓글