Matrix Path Existence
Problem statement
Given a rectangular binary matrix grid, determine whether a path exists from the top-left cell to the bottom-right cell.
- A cell containing
1is passable. - A cell containing
0is blocked. - You may move one cell at a time up, down, left, or right.
Return true when such a path exists and false otherwise.
Function
pathExists(grid: int[][]) → booleanExamples
Example 1
grid = [[1,1,0],[0,1,1],[0,0,1]]return = trueA path follows the passable cells from the top-left cell through the center to the bottom-right cell.
Example 2
grid = [[1,0],[0,1]]return = falseDiagonal movement is not allowed, so the two passable cells are disconnected.
Example 3
grid = [[1]]return = trueThe single cell is both the start and the destination.
Example 4
grid = [[0,1],[1,1]]return = falseThe starting cell is blocked.
Constraints
1 <= grid.length <= 200.1 <= grid[i].length <= 200.- Every row has the same length.
grid[i][j]is0or1.