Loading...
Loading...
You are given an m x n integer matrix grid where every cell is 0 or 1:
0 represents an open cell.1 represents a blocked cell.You can move one step up, down, left, or right, but only between open cells. Before you start walking, you may clear any blocked cells you like, which turns each of them into an open cell.
Return the minimum number of blocked cells you need to clear so that you can walk from the top-left cell (0, 0) to the bottom-right cell (m - 1, n - 1).
You can assume both of these corner cells are open.
The walk clears 2 blocked cells.
Input: grid = [[0, 1, 1],
[1, 1, 0],
[1, 1, 0]]
Output: 2
Explanation: Both neighbors of (0, 0) are blocked, and so is every other neighbor of those
two cells. So every walk passes through at least two blocked cells. Clearing (0, 1) and
(0, 2) opens the walk (0, 0) -> (0, 1) -> (0, 2) -> (1, 2) -> (2, 2).
Input: grid = [[0, 1, 0, 0, 0],
[0, 1, 0, 1, 0],
[0, 0, 0, 1, 0]]
Output: 0
Explanation: The walk down column 0, along row 2 to (2, 2), up to (0, 2), right to (0, 4),
and down to (2, 4) uses only open cells, so nothing needs to be cleared.
grid.lengthgrid[i].lengthgrid[i][j] is either 0 or 1.grid[0][0] == grid[m - 1][n - 1] ==0