2
public int nearestExit(char[][] maze, int[] entrance) {3
int rows = maze.length, cols = maze[0].length, queueSize;4
Queue<int[]> queue = new LinkedList<>();5
boolean[][] visited = new boolean[rows][cols];7
int[][] dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};10
queue.offer(entrance);11
visited[entrance[0]][entrance[1]] = true;13
while (!queue.isEmpty()) {14
queueSize = queue.size();17
for (int i = 0; i < queueSize; i++) {20
for (int[] dir : dirs) {24
if (x < 0 || x >= rows || y < 0 || y >= cols) continue;25
if (visited[x][y] || maze[x][y] == '+') continue;27
// check if we have reached boundary28
if (x == 0 || x == rows - 1 || y == 0 || y == cols - 1) return steps;30
queue.offer(new int[] {x, y});