Java BFS
\npublic int nearestExit(char[][] maze, int[] entrance) {\n\tint[][] directions = new int[][] {{0,1}, {1,0}, {0,-1},{-1,0}};\n\tQueue<Integer> queue = new Linke
```\npublic int nearestExit(char[][] maze, int[] entrance) {\n\tint[][] directions = new int[][] {{0,1}, {1,0}, {0,-1},{-1,0}};\n\tQueue<Integer> queue = new LinkedList<>();\n\tqueue.offer(entrance[0]); queue.offer(entrance[1]);\n\tfor(int currentDistance = 0; !queue.isEmpty(); currentDistance++) {\n\t\tint n = queue.size()/2;\n\t\tfor(int k=0;k<n;k++) {\n\t\t\tint i = queue.poll(), j = queue.poll();\n\t\t\tfor(int[] dir: directions) \n\t\t\t\tif(i+dir[0]>=0 && i+dir[0]<maze.length && j+dir[1]>=0 && j+dir[1]<maze[0].length) {\n\t\t\t\t\tif(maze[i+dir[0]][j+dir[1]]==\'.\') {\n\t\t\t\t\t\tmaze[i+dir[0]][j+dir[1]] = \'+\';\n\t\t\t\t\t\tqueue.offer(i+dir[0]); queue.offer(j+dir[1]); \n\t\t\t\t\t}\n\t\t\t\t} else if (i != entrance[0] || j != entrance[1]) return currentDistance;\n\t\t}\n\t}\n\treturn -1;\n}\n```
[]