✅Easy to understand✅ Beats 100% 
IntuitionThe problem involves finding the shortest path from the given entrance to the nearest exit in a maze. Each exit lies on the boundary of the maze, and t
# Intuition
The problem involves finding the shortest path from the given entrance to the nearest exit in a maze. Each exit lies on the boundary of the maze, and the path must traverse only empty cells (denoted by '.'). The shortest path can be efficiently computed using Breadth-First Search (BFS), which explores all possible paths layer by layer.

BFS is well-suited for this problem because:

It guarantees finding the shortest path in an unweighted grid.
It systematically explores neighboring cells in all directions before proceeding to cells farther away.

# Approach
Model the Maze as a Grid:

Treat the maze as a 2D grid where each cell is either empty ('.') or blocked ('+').
The goal is to navigate from the entrance to the nearest boundary cell that is not the entrance.
Breadth-First Search (BFS):

Initialize a queue with the entrance coordinates and a step counter (starting at 0).
Use a directions array to explore the four possible moves (up, down, left, right) from each cell.
Mark Cells as Visited:

To avoid revisiting cells, mark each visited cell (including the entrance) as blocked (e.g., by changing '.' to '+').
This ensures that the algorithm doesn't revisit the same cell, reducing unnecessary computations.
Boundary Exit Check:

For every valid neighboring cell, check if it is on the boundary of the grid. If it is, return the number of steps taken to reach this cell.
If no exit is found after exploring all possible paths, return -1.
Efficiency:

The BFS ensures that the shortest path is found with minimal computational overhead.
The grid is updated in place, minimizing additional space usage.

# Complexity
- Time complexity: O(m×n)

- Space complexity: O(min(m,n)),

# Code
```python3 []
class Solution:
    def nearestExit(self, maze: List[List[str]], entrance: List[int]) -> int:
        q = deque([(entrance[0], entrance[1], 0)])
        directions = [(1, 0), (0, 1), (-1, 0), (0, -1)]
        maze[entrance[0]][entrance[1]] = '+'  # Mark the entrance as visited
        
        while q:
            r, c, steps = q.popleft()
            for dr, dc in directions:
                nr, nc = r + dr, c + dc
                # Check if the next cell is valid and open
                if 0 <= nr < len(maze) and 0 <= nc < len(maze[0]) and maze[nr][nc] == '.':
                    # If it's on the boundary and not the entrance, return steps + 1
                    if nr == 0 or nr == len(maze) - 1 or nc == 0 or nc == len(maze[0]) - 1:
                        return steps + 1
                    # Mark as visited and enqueue
                    maze[nr][nc] = '+'
                    q.append((nr, nc, steps + 1))
        
        return -1

```
['Python3']