#1391
Check if There is a Valid Path in a Grid
specialist · 900 · lc medium +31 · verified · 50.2% accepted · 881 likes · top 38%
Description
You are given an m x n grid where each cell holds a street type:
- 1 connects left and right cells.
- 2 connects upper and lower cells.
- 3 connects left and lower cells.
- 4 connects right and lower cells.
- 5 connects left and upper cells.
- 6 connects right and upper cells.
Starting at the upper-left cell (0, 0), follow the streets and determine whether a valid path to the bottom-right cell (m - 1, n - 1) exists. Streets cannot be changed. Return true if such a path exists, false otherwise.
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).
Code
1
2
3