Sponsored
Sponsored
In this approach, we use the Breadth-First Search (BFS) algorithm to explore the maze. BFS is suitable because it explores nodes layer by layer, ensuring that the shortest path is found. We initialize a queue with the entrance cell and explore its neighbors (up, down, left, right). If a neighbor is an unvisited empty cell and is on the boundary (except the entrance), it can be considered as an exit.
Each time we explore a cell, we mark it as visited by changing its state to '+'. This prevents revisiting nodes, which ensures that we won't traverse the same path multiple times. We keep track of steps taken from the entrance to the current cell, and if we reach an exit, we return the number of steps. If the queue is exhausted without finding an exit, we return -1.
Time Complexity: O(m * n), where m is the number of rows and n is the number of columns in the maze. In the worst case, all cells are traversed.
Space Complexity: O(m * n) for the queue used in BFS.
1import java.util.LinkedList;
2import java.util.Queue;
3
4class Solution {
5 public int nearestExit
This Java implementation uses a similar setup with a queue to facilitate BFS. The entrance is added to the Queue and marked as visited. For each cell, adjacent cells are checked; if a boundary cell is reached, the method returns the current step count. This ensures that the shortest path is found.
In this approach, we use the Depth-First Search (DFS) algorithm to explore the maze. DFS might not be as efficient as BFS for finding the shortest path, but for educational purposes, it demonstrates another way to traverse the maze. We recursively explore each path, marking cells as visited and backtracking when dead-ends or exits are encountered.
This approach keeps a global minimum step count, updating it whenever an exit (that isn't the entrance) is found. Through recursive calls, each possible path is explored until all directions are exhausted. The main objective is to identify the closest exit with respect to steps taken from the entrance cell.
Time Complexity: O((m * n)^2) in the worst case, where m and n are dimensions of the maze.
Space Complexity: O(m * n) due to recursion stack usage in deep recursive path exploration.
1import java.util.Arrays;
2
3class Solution {
4 int minSteps = Integer.MAX_VALUE;
5 int[][] directions = {{0,1},{1,0},{0,-1},{-1,0}};
6
7 public void dfs(char[][] maze, int x, int y, int steps) {
8 int m = maze.length, n = maze[0].length;
9 if (x < 0 || y < 0 || x >= m || y >= n || maze[x][y] != '.') return;
10 if ((x == 0 || y == 0 || x == m - 1 || y == n - 1) && steps != 0) {
11 minSteps = Math.min(minSteps, steps);
12 }
13 maze[x][y] = '+'; // mark as visited
14 for (int[] d : directions) {
15 dfs(maze, x + d[0], y + d[1], steps + 1);
16 }
17 maze[x][y] = '.'; // backtrack
18 }
19
20 public int nearestExit(char[][] maze, int[] entrance) {
21 dfs(maze, entrance[0], entrance[1], 0);
22 return minSteps == Integer.MAX_VALUE ? -1 : minSteps;
23 }
24}
In Java, DFS functions in a manner similar to other languages. Through recursion, each direction is probed from any cell until boundaries are hit or paths become blocked. Exits update a global variable keeping track of minimum steps. Recursive backtracking is employed at each step.