Skip to content

#1926 - Nearest Exit from Entrance in Maze

MediumGraphs - BFS
Open on LeetCode

Given an m × n matrix maze with empty cells '.' and walls '+', and an entrance position [row, col], return the number of steps in the shortest path from the entrance to the nearest exit. An exit is any empty cell on the border of the maze (not the entrance itself). Return -1 if no path exists.

InputOutput
maze = [["+","+",".","+"],[".",".",".","+"],["+","+","+","."]], entrance = [1,2]1
maze = [["+","+","+"],[".",".","."],["+","+","+"]], entrance = [1,0]2
maze = [[".","+"]-1

Released under the MIT License.