BFS Solution | C++
Intuition\nStarting in every direction from the entrance we move by 1 step and as soon we approach a boundary return the steps.\n\n# Approach\nApply BFS which w
# Intuition\nStarting in every direction from the entrance we move by 1 step and as soon we approach a boundary return the steps.\n\n# Approach\nApply BFS which works radially outwards from the entrance point.\n\n# Complexity\n- Time complexity:\nO(N^2)\n\n- Space complexity:\nO(N)\n\n# Code\n```\nclass Solution {\npublic:\n    int nearestExit(vector<vector<char>>& maze, vector<int>& entrance) {\n        queue<vector<int>> q;\n        int ans = 0;\n        int n = maze.size();\n        int m = maze[0].size();\n        q.push({entrance[0],entrance[1],0});\n        maze[entrance[0]][entrance[1]] = \'#\';\n        while(!q.empty())\n        {\n            int i = q.front()[0];\n            int j = q.front()[1];\n            int d = q.front()[2];\n            q.pop();\n            if(i==0||i==n-1 || j==0 || j == m-1)\n            {\n                if(i!=entrance[0] || j!=entrance[1])\n                    return d;\n            }\n            if(i+1<n and i>=0 and j<m and j>=0 and maze[i+1][j]==\'.\')\n            {\n                q.push({i+1,j,d+1});\n                maze[i+1][j] = \'#\';\n            }  \n            if(i<n and i-1>=0 and j<m and j>=0 and maze[i-1][j]==\'.\')\n            {\n                q.push({i-1,j,d+1});\n                maze[i-1][j] = \'#\';\n            }    \n            if(i<n and i>=0 and j+1<m and j>=0 and maze[i][j+1]==\'.\')\n            {\n                q.push({i,j+1,d+1});\n                maze[i][j+1] = \'#\';\n            }    \n            if(i<n and i>=0 and j<m and j-1>=0 and maze[i][j-1]==\'.\')\n            {\n                q.push({i,j-1,d+1});\n                maze[i][j-1] = \'#\';\n            }        \n        }\n        return -1;\n    }\n};\n```
['C++']