Python | BFS | Easy to Understand | Beats 86%
Hope my code helps!\n\nBFS -- Beats 86%\n\nclass Solution:\n    def nearestExit(self, maze: List[List[str]], entrance: List[int]) -> int:\n        """\n        
Hope my code helps!\n\nBFS -- Beats 86%\n```\nclass Solution:\n    def nearestExit(self, maze: List[List[str]], entrance: List[int]) -> int:\n        """\n        > With BFS, The first exit it comes across is the closest exit\n        > Level represents number of steps ahead\n        """\n        m, n = len(maze), len(maze[0])\n        directions = [(1,0), (0,1), (-1,0), (0,-1)]\n        \n        queue = deque([(*entrance, 0)])\n        visited = [[False] * n for _ in range(m)]\n        visited[entrance[0]][entrance[1]] = True\n        \n        while queue:\n            i, j, level = queue.popleft()\n            \n            # Return as soon as the nearest exit is found\n            # If the current position is on the edges of the grid, thats our exit.\n            if (i in {0, m - 1} or j in {0, n - 1}) and [i, j] != entrance:\n                # Level represents the shortest distance from the entrance\n                return level     \n            \n            # Iterate through all the directions\n            for x, y in directions:\n                x, y = x + i, y + j\n                # Make sure each neighbour cell is within the boundary and \n                # not yet visited and it is an empty cell\n                if 0 <= x < m and 0 <= y < n and \\\n                not visited[x][y] and maze[x][y] == \'.\':\n                    queue.append((x, y, level + 1))\n                    visited[x][y] = True\n\n        return -1\n```\n![image](https://assets.leetcode.com/users/images/22c5d14d-6da5-4f82-bf7a-48fcb95f3372_1712013208.171477.png)
['Breadth-First Search', 'Graph', 'Python3']