📌[C++] Most Intuitive | 🔥BFS
Consider looking at some additional BFS-related questions.\n 542. 01 Matrix\n 994. Rotting Oranges\n 1091. Shortest Path in Binary Matrix\n\n-------------------
# Consider looking at some additional BFS-related questions.\n* [542. 01 Matrix](https://leetcode.com/problems/01-matrix/)\n* [994. Rotting Oranges](https://leetcode.com/problems/rotting-oranges/)\n* [1091. Shortest Path in Binary Matrix](https://leetcode.com/problems/shortest-path-in-binary-matrix/)\n\n----------------------------------------\n\n**C++**\n\n```\nclass Solution {\npublic:\n    int dx[4] = {1 , -1 ,  0 , 0};\n    int dy[4] = {0 ,  0 , -1 , 1};\n    \n    int nearestExit(vector<vector<char>>& maze, vector<int>& entrance) {\n        \n        queue<pair<int, int>> q;\n        int n = maze.size() , m = maze[0].size();\n        vector<vector<bool>> visited(n , vector<bool>(m, 0));   // keeping a visited matrix of the same size as that of the maze to keep track of visited cells\n        \n        visited[entrance[0]][entrance[1]] = 1;                  // The beginning point should be noted as visited.\n        q.push({entrance[0] , entrance[1]});                    // now add it to our queue for a subsequent BFS traversal.\n        \n        int level = 0;\n        \n        while( !q.empty() ) {\n            \n            int size = q.size();\n            level++;\n            \n            while( size-- ) {\n                \n                pair<int, int> p = q.front();\n                q.pop();\n                \n                for( int i = 0; i < 4; i++ ){\n                    \n                    int new_row = p.first+dx[i], new_col = p.second+dy[i];\n                    \n                    if( new_row >= 0 and new_col >= 0 and new_row < n and new_col < m){\n                        \n                        if(maze[new_row][new_col] != \'.\') continue;\n                        if(visited[new_row][new_col]) continue;                     // if this cell is already visited, don\'t check further        \n                        \n                        if( new_row == 0 or new_col==0 or new_row == n-1 or new_col == m-1) return level;       // if we reached a boundary, return the level\n                        \n                        visited[new_row][new_col] = 1;\n                        q.push({new_row, new_col});\n                    }\n                }\n            }\n        }\n        return -1;\n    }\n};\n```\n\n-----------------------\n------------------------\n
[]