Java Optimal , Simple Approach
Nearest Exit from Entrance in MazeProblem DescriptionYou are given an m x n maze (a 2D grid) where:
'.' represents an empty cell,
'#' represents a wall,
The ent
# Nearest Exit from Entrance in Maze

## Problem Description
You are given an `m x n` maze (a 2D grid) where:
- `'.'` represents an empty cell,
- `'#'` represents a wall,
- The entrance is represented by a specific cell in the maze given as an array `entrance` of length 2, i.e., `[entrance[0], entrance[1]]`.

The goal is to find the nearest exit from the entrance. An exit is defined as a cell that is at the border of the maze (first row, last row, first column, or last column). You can only move in four directions: up, down, left, and right.

Write a function `nearestExit(char[][] maze, int[] entrance)` that returns the number of steps to reach the nearest exit. If there is no exit, return `-1`.

## Approach
The problem can be solved using **Breadth-First Search (BFS)** because we are looking for the shortest path to an exit. Here are the steps followed in the code:

1. **Initialization:**
   - We store the dimensions of the maze (`r` for rows, `c` for columns).
   - We initialize the `minMove` variable to keep track of the minimum number of steps.
   - We define the possible movement directions as `up, right, down, left`.
   - We initialize a queue and add the entrance to it.
   - We mark the entrance as visited by changing its value to `'#'`.

2. **BFS Traversal:**
   - Start a BFS where we explore the maze level by level.
   - For each level (i.e., each cell in the queue), we check all possible movements.
   - If a valid cell (within bounds and not a wall) is found, we check if it's at the border of the maze. If it is, return the current step count as the answer.
   - If not, we add the cell to the queue and mark it as visited.

3. **Exit Conditions:**
   - If the BFS completes and no exit is found, return `-1`.

## Time Complexity:
- **Time complexity:** $$O(m \times n)$$, where `m` is the number of rows and `n` is the number of columns in the maze. In the worst case, we will visit each cell once.

## Space Complexity:
- **Space complexity:** $$O(m \times n)$$, as we store the queue and the modified maze with the visited cells.

## Code:

```java
class Solution {
    public int nearestExit(char[][] maze, int[] entrance) {
        int r = maze.length, c = maze[0].length;

        int minMove = 0;

        int[][] directions = new int[][]{{-1, 0}, {0, 1}, {1, 0}, {0, -1}};

        Queue<int[]> q = new ArrayDeque<>();
        q.offer(entrance);
        maze[entrance[0]][entrance[1]] = '#';

        while (!q.isEmpty()) {
            minMove++;
            int qLen = q.size();

            for (int i = 0; i < qLen; i++) {
                int[] cur = q.poll();

                for (int[] dist : directions) {
                    int row = cur[0] + dist[0];
                    int col = cur[1] + dist[1];

                    if (row >= 0 && row < r && col >= 0 && col < c && maze[row][col] == '.') {
                        if (row == 0 || row == r - 1 || col == 0 || col == c - 1)
                            return minMove;

                        q.offer(new int[]{row, col});
                        maze[row][col] = '#';
                    }
                }
            }
        }
        return -1;
    }
}

['Breadth-First Search', 'Matrix', 'Java']