Solution By Dare2Solve | Detailed Explanation | Clean Code
Explanation []\nauthorslog.com/blog/xkpfvNACri\n\n# Code\n\ncpp []\nclass Solution {\npublic:\n    int nearestExit(vector<vector<char>>& maze, vector<int>& entr
```Explanation []\nauthorslog.com/blog/xkpfvNACri\n```\n# Code\n\n```cpp []\nclass Solution {\npublic:\n    int nearestExit(vector<vector<char>>& maze, vector<int>& entrance) {\n        vector<tuple<int, int, int>> st;\n        int rows = maze.size();\n        int cols = maze[0].size();\n\n        for (int i = 0; i < rows; ++i) {\n            for (int j = 0; j < cols; ++j) {\n                if (maze[i][j] == \'.\' && (i == 0 || i == rows - 1 || j == 0 || j == cols - 1) && !isEquals(vector<int>{i, j}, entrance)) {\n                    st.push_back({i, j, 0});\n                }\n            }\n        }\n        return bfs(st, maze, entrance);\n    }\n\nprivate:\n    bool isEquals(const vector<int>& a, const vector<int>& b) {\n        return a[0] == b[0] && a[1] == b[1];\n    }\n\n    int bfs(vector<tuple<int, int, int>>& st, vector<vector<char>>& maze, vector<int>& start) {\n        vector<vector<int>> directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};\n        unordered_map<int, unordered_map<int, bool>> visited;\n\n        while (!st.empty()) {\n            auto [row, col, len] = st.front();\n            st.erase(st.begin());\n            visited[row][col] = true;\n\n            for (auto& dir : directions) {\n                int r = row + dir[0];\n                int c = col + dir[1];\n\n                if (isEquals(vector<int>{r, c}, start)) return len + 1;\n                if (r < 0 || c < 0 || r >= maze.size() || c >= maze[0].size() || maze[r][c] == \'+\' || visited[r][c]) continue;\n\n                st.push_back({r, c, len + 1});\n                visited[r][c] = true;\n            }\n        }\n        return -1;\n    }\n};\n```\n\n```python []\nclass Solution:\n    def nearestExit(self, maze: List[List[str]], entrance: List[int]) -> int:\n        st = []\n        rows, cols = len(maze), len(maze[0])\n\n        for i in range(rows):\n            for j in range(cols):\n                if maze[i][j] == "." and (i == 0 or i == rows - 1 or j == 0 or j == cols - 1) and [i, j] != entrance:\n                    st.append((i, j, 0))\n\n        return self.bfs(st, maze, entrance)\n\n    def isEquals(self, a: List[int], b: List[int]) -> bool:\n        return a[0] == b[0] and a[1] == b[1]\n\n    def bfs(self, st: List[tuple], maze: List[List[str]], start: List[int]) -> int:\n        directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]\n        visited = set()\n\n        while st:\n            row, col, length = st.pop(0)\n            visited.add((row, col))\n\n            for r_offset, c_offset in directions:\n                r, c = row + r_offset, col + c_offset\n\n                if [r, c] == start:\n                    return length + 1\n                if r < 0 or c < 0 or r >= len(maze) or c >= len(maze[0]) or maze[r][c] == "+" or (r, c) in visited:\n                    continue\n\n                st.append((r, c, length + 1))\n                visited.add((r, c))\n\n        return -1\n```\n\n```java []\nclass Solution {\n    public int nearestExit(char[][] maze, int[] entrance) {\n        List<int[]> st = new ArrayList<>();\n        int rows = maze.length;\n        int cols = maze[0].length;\n\n        for (int i = 0; i < rows; i++) {\n            for (int j = 0; j < cols; j++) {\n                if (maze[i][j] == \'.\' && (i == 0 || i == rows - 1 || j == 0 || j == cols - 1) && !isEquals(new int[]{i, j}, entrance)) {\n                    st.add(new int[]{i, j, 0});\n                }\n            }\n        }\n        return bfs(st, maze, entrance);\n    }\n\n    private boolean isEquals(int[] a, int[] b) {\n        return a[0] == b[0] && a[1] == b[1];\n    }\n\n    private int bfs(List<int[]> st, char[][] maze, int[] start) {\n        int[][] directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};\n        Set<String> visited = new HashSet<>();\n\n        while (!st.isEmpty()) {\n            int[] cur = st.remove(0);\n            int row = cur[0], col = cur[1], len = cur[2];\n            visited.add(row + "," + col);\n\n            for (int[] dir : directions) {\n                int r = row + dir[0];\n                int c = col + dir[1];\n\n                if (isEquals(new int[]{r, c}, start)) return len + 1;\n                if (r < 0 || c < 0 || r >= maze.length || c >= maze[0].length || maze[r][c] == \'+\' || visited.contains(r + "," + c)) continue;\n\n                st.add(new int[]{r, c, len + 1});\n                visited.add(r + "," + c);\n            }\n        }\n        return -1;\n    }\n}\n```\n\n```javascript []\nvar nearestExit = function (maze, entrance) {\n    let st = []\n    for (let i = 0; i < maze.length; i++) {\n        for (let j = 0; j < maze[0].length; j++) {\n            if (maze[i][j] == "." && (i == 0 || i == maze.length - 1 || j == 0 || j == maze[0].length - 1) && !isEquals([i, j], entrance)) {\n                st.push([i, j, 0])\n            }\n        }\n    }\n    return bfs(st, maze, entrance)\n};\nfunction isEquals(a, b) {\n    return a[0] == b[0] && a[1] == b[1]\n}\nfunction bfs(st, maze, start) {\n    let directions = [[0, 1], [0, -1], [1, 0], [-1, 0]], visited = {}\n    while (st.length) {\n        const [row, col, len] = st.shift()\n        visited[[row, col]] = 1\n        for (let i of directions) {\n            let r = row + i[0], c = col + i[1]\n            if (isEquals([r, c], start)) return len + 1\n            if (r < 0 || c < 0 || r > maze.length - 1 || c > maze[0].length - 1 || maze[r][c] == "+" || visited[[r, c]]) continue\n            st.push([r, c, len + 1])\n            visited[[r, c]] = 1\n        }\n    }\n    return -1\n}\n```
['Matrix', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']