JAVA | 2 solutions | BFS
Please Upvote :D\n##### 1. Using an extra 2D \'visited\' array:\n\nclass Solution {\n    public int nearestExit(char[][] maze, int[] entrance) {\n        int m 
### **Please Upvote** :D\n##### 1. Using an extra 2D \'visited\' array:\n```\nclass Solution {\n    public int nearestExit(char[][] maze, int[] entrance) {\n        int m = maze.length, n = maze[0].length;\n        int[][] direction = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};\n\n        Queue<int[]> q = new LinkedList<>();\n        q.offer(new int[]{entrance[0], entrance[1], 0});\n\n        boolean[][] visited = new boolean[m][n];\n        visited[entrance[0]][entrance[1]] = true;\n\n        while (!q.isEmpty()) {\n            int[] currPos = q.poll();\n            int currRow = currPos[0], currCol = currPos[1], currSteps = currPos[2];\n\n            for (int[] d : direction) {\n                int nextRow = currRow + d[0];\n                int nextCol = currCol + d[1];\n\n                if (nextRow >= 0 && nextCol >= 0 && nextRow < m && nextCol < n && \n\t\t\t\t    maze[nextRow][nextCol] == \'.\' && !visited[nextRow][nextCol] ) {\n\n                    if (nextRow == 0 || nextCol == 0 || nextRow == m - 1 || nextCol == n - 1) {\n                        return currSteps + 1;\n                    }\n\n                    visited[nextRow][nextCol] = true;\n                    q.offer(new int[]{nextRow, nextCol, currSteps + 1});\n                }\n            }\n        }\n\n        return -1;\n    }\n}\n\n// TC: O(m * n)\n// SC: O(m * n) + O(m * n) => O(m * n)\n```\n\n##### 2. Marking if visited in-place (no extra array):\n```\nclass Solution {\n    public int nearestExit(char[][] maze, int[] entrance) {\n        int m = maze.length, n = maze[0].length;\n        int[][] direction = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};\n\n        Queue<int[]> q = new LinkedList<>();\n        q.offer(new int[]{entrance[0], entrance[1], 0});\n\n        maze[entrance[0]][entrance[1]] = \'+\';\n\n        while (!q.isEmpty()) {\n            int[] currPos = q.poll();\n            int currRow = currPos[0], currCol = currPos[1], currSteps = currPos[2];\n\n            for (int[] d : direction) {\n                int nextRow = currRow + d[0];\n                int nextCol = currCol + d[1];\n\n                if (nextRow >= 0 && nextCol >= 0 && nextRow < m && nextCol < n \n\t\t\t\t    && maze[nextRow][nextCol] == \'.\') {\n\n                    if (nextRow == 0 || nextCol == 0 || nextRow == m - 1 || nextCol == n - 1) {\n                        return currSteps + 1;\n                    }\n\n                    maze[nextRow][nextCol] = \'+\';\n                    q.offer(new int[]{nextRow, nextCol, currSteps + 1});\n                }\n            }\n        }\n\n        return -1;\n    }\n}\n\n// TC: O(m * n), SC: O(m * n)\n```
['Breadth-First Search', 'Queue', 'Java']