Shortest Maze Path With One Block Removal
Problem statement
maze is a rectangular grid where 0 is free and 1 is blocked. Start at the top-left cell and reach the bottom-right cell using four-directional moves.
You may change at most one blocked cell into a free cell when entering it. Return the minimum number of moves needed, or -1 when the destination cannot be reached. The start and destination cells are free.
Function
shortestPathWithOneRemoval(maze: int[][]) → intExamples
Example 1
maze = [[0,1],[0,0]]return = 2Move down and then right without using the removal.
Example 2
maze = [[0,1,0],[1,1,0],[0,0,0]]return = 4Remove either blocked neighbor of the start, then continue to the destination in four moves.
Example 3
maze = [[0,1,1],[1,1,1],[1,1,0]]return = -1Every route needs more than one blocked cell to be removed.
Constraints
1 <= maze.length, maze[0].length <= 200.- Every row has the same length.
- Every cell is
0or1. maze[0][0] == maze[m - 1][n - 1] == 0.