✅✅ Classical BFS || Beginner Friendly || Easy Solution
IntuitionWe need to find the shortest path from the entrance to any exit in the maze.
Since we are looking for the shortest path in an unweighted grid, BFS is t
# Intuition
We need to find the shortest path from the entrance to any exit in the maze.  
Since we are looking for the shortest path in an unweighted grid, **BFS** is the optimal approach.

# Approach
1. **Initialize BFS**:  
   - Push the `entrance` into a queue and mark it as visited (`'+'`).
   - Use a `directions` array to explore up, down, left, and right.

2. **BFS Traversal**:
   - At each level, process all nodes in the queue.
   - If a node (not the entrance) is on the boundary, return the current step count.
   - Otherwise, explore its unvisited neighbors and mark them as visited.

3. **If No Exit is Found**:  
   - Return `-1`.

# Time Complexity
- **O(M * N)** in the worst case when all cells are visited.

# Space Complexity
- **O(M * N)** for the queue in the worst case.

# Code
```cpp []
class Solution {
public:
    vector<vector<int>>directions={{0,1},{0,-1},{1,0},{-1,0}};
    int nearestExit(vector<vector<char>>& maze, vector<int>& entrance) {
        int m=maze.size(),n=maze[0].size();
        int ans=0;

        queue<pair<int,int>>q;
        q.push({entrance[0],entrance[1]});
        maze[entrance[0]][entrance[1]]='+';

        auto hasReached=[&](int i,int j){
            return (i==0 || j==0 || i==m-1 || j==n-1);
        };

        while(!q.empty()){
            int N=q.size();
            while(N--){
                auto currNode=q.front();
                q.pop();

                if(currNode!=make_pair(entrance[0],entrance[1]) && 
                    hasReached(currNode.first,currNode.second)){
                    return ans;
                }

                for(auto &dir:directions){
                    int i_=currNode.first+dir[0];
                    int j_=currNode.second+dir[1];

                    if(i_>=0 && i_<m && j_>=0 && j_<n && maze[i_][j_]!='+'){
                        q.push({i_,j_});
                        maze[i_][j_]='+';
                    }
                }
            }
            ans++;
        }
        return -1;
    }
};
```
# Have a Good Day 😊 UpVote?
['Array', 'Breadth-First Search', 'Matrix', 'C++']