|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author openset <[email protected]> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](../four-divisors "Four Divisors") |
| 9 | + |
| 10 | +[Next >](../longest-happy-prefix "Longest Happy Prefix") |
| 11 | + |
| 12 | +## [1391. Check if There is a Valid Path in a Grid (Medium)](https://leetcode.com/problems/check-if-there-is-a-valid-path-in-a-grid "检查网格中是否存在有效路径") |
| 13 | + |
| 14 | +Given a <em>m</em> x <em>n</em> <code>grid</code>. Each cell of the <code>grid</code> represents a street. The street of <code>grid[i][j]</code> can be: |
| 15 | +<ul> |
| 16 | + <li><strong>1</strong> which means a street connecting the left cell and the right cell.</li> |
| 17 | + <li><strong>2</strong> which means a street connecting the upper cell and the lower cell.</li> |
| 18 | + <li><b>3</b> which means a street connecting the left cell and the lower cell.</li> |
| 19 | + <li><b>4</b> which means a street connecting the right cell and the lower cell.</li> |
| 20 | + <li><b>5</b> which means a street connecting the left cell and the upper cell.</li> |
| 21 | + <li><b>6</b> which means a street connecting the right cell and the upper cell.</li> |
| 22 | +</ul> |
| 23 | + |
| 24 | +<p><img alt="" src="https://assets.leetcode.com/uploads/2020/03/05/main.png" style="width: 450px; height: 708px;" /></p> |
| 25 | + |
| 26 | +<p>You will initially start at the street of the upper-left cell <code>(0,0)</code>. A valid path in the grid is a path which starts from the upper left cell <code>(0,0)</code> and ends at the bottom-right cell <code>(m - 1, n - 1)</code>. <strong>The path should only follow the streets</strong>.</p> |
| 27 | + |
| 28 | +<p><strong>Notice</strong> that you are <strong>not allowed</strong> to change any street.</p> |
| 29 | + |
| 30 | +<p>Return <i>true</i> if there is a valid path in the grid or <em>false</em> otherwise.</p> |
| 31 | + |
| 32 | +<p> </p> |
| 33 | +<p><strong>Example 1:</strong></p> |
| 34 | +<img alt="" src="https://assets.leetcode.com/uploads/2020/03/05/e1.png" style="width: 455px; height: 311px;" /> |
| 35 | +<pre> |
| 36 | +<strong>Input:</strong> grid = [[2,4,3],[6,5,2]] |
| 37 | +<strong>Output:</strong> true |
| 38 | +<strong>Explanation:</strong> As shown you can start at cell (0, 0) and visit all the cells of the grid to reach (m - 1, n - 1). |
| 39 | +</pre> |
| 40 | + |
| 41 | +<p><strong>Example 2:</strong></p> |
| 42 | +<img alt="" src="https://assets.leetcode.com/uploads/2020/03/05/e2.png" style="width: 455px; height: 293px;" /> |
| 43 | +<pre> |
| 44 | +<strong>Input:</strong> grid = [[1,2,1],[1,2,1]] |
| 45 | +<strong>Output:</strong> false |
| 46 | +<strong>Explanation:</strong> 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) |
| 47 | +</pre> |
| 48 | + |
| 49 | +<p><strong>Example 3:</strong></p> |
| 50 | + |
| 51 | +<pre> |
| 52 | +<strong>Input:</strong> grid = [[1,1,2]] |
| 53 | +<strong>Output:</strong> false |
| 54 | +<strong>Explanation:</strong> You will get stuck at cell (0, 1) and you cannot reach cell (0, 2). |
| 55 | +</pre> |
| 56 | + |
| 57 | +<p><strong>Example 4:</strong></p> |
| 58 | + |
| 59 | +<pre> |
| 60 | +<strong>Input:</strong> grid = [[1,1,1,1,1,1,3]] |
| 61 | +<strong>Output:</strong> true |
| 62 | +</pre> |
| 63 | + |
| 64 | +<p><strong>Example 5:</strong></p> |
| 65 | + |
| 66 | +<pre> |
| 67 | +<strong>Input:</strong> grid = [[2],[2],[2],[2],[2],[2],[6]] |
| 68 | +<strong>Output:</strong> true |
| 69 | +</pre> |
| 70 | + |
| 71 | +<p> </p> |
| 72 | +<p><strong>Constraints:</strong></p> |
| 73 | + |
| 74 | +<ul> |
| 75 | + <li><code>m == grid.length</code></li> |
| 76 | + <li><code>n == grid[i].length</code></li> |
| 77 | + <li><code>1 <= m, n <= 300</code></li> |
| 78 | + <li><code>1 <= grid[i][j] <= 6</code></li> |
| 79 | +</ul> |
| 80 | + |
| 81 | +### Related Topics |
| 82 | + [[Depth-first Search](../../tag/depth-first-search/README.md)] |
| 83 | + [[Breadth-first Search](../../tag/breadth-first-search/README.md)] |
| 84 | + |
| 85 | +### Hints |
| 86 | +<details> |
| 87 | +<summary>Hint 1</summary> |
| 88 | +Start DFS from the node (0, 0) and follow the path till you stop. |
| 89 | +</details> |
| 90 | + |
| 91 | +<details> |
| 92 | +<summary>Hint 2</summary> |
| 93 | +When you reach a cell and cannot move anymore check that this cell is (m - 1, n - 1) or not. |
| 94 | +</details> |
0 commit comments