Class Solution
- java.lang.Object
-
- g1301_1400.s1391_check_if_there_is_a_valid_path_in_a_grid.Solution
-
public class Solution extends Object
1391 - Check if There is a Valid Path in a Grid.Medium
You are given an
m x ngrid. Each cell ofgridrepresents a street. The street ofgrid[i][j]can be:1which means a street connecting the left cell and the right cell.2which means a street connecting the upper cell and the lower cell.3which means a street connecting the left cell and the lower cell.4which means a street connecting the right cell and the lower cell.5which means a street connecting the left cell and the upper cell.6which means a street connecting the right cell and the upper cell.

You will initially start at the street of the upper-left cell
(0, 0). A valid path in the grid is a path that starts from the upper left cell(0, 0)and ends at the bottom-right cell(m - 1, n - 1). The path should only follow the streets.Notice that you are not allowed to change any street.
Return
trueif there is a valid path in the grid orfalseotherwise.Example 1:

Input: grid = [[2,4,3],[6,5,2]]
Output: true
Explanation: As shown you can start at cell (0, 0) and visit all the cells of the grid to reach (m - 1, n - 1).
Example 2:

Input: grid = [[1,2,1],[1,2,1]]
Output: false
Explanation: As shown you the street at cell (0, 0) is not connected with any street of any other cell and you will get stuck at cell (0, 0)
Example 3:
Input: grid = [[1,1,2]]
Output: false
Explanation: You will get stuck at cell (0, 1) and you cannot reach cell (0, 2).
Constraints:
m == grid.lengthn == grid[i].length1 <= m, n <= 3001 <= grid[i][j] <= 6
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanhasValidPath(int[][] grid)
-