|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author openset <openset.wang@gmail.com> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](../minimum-numbers-of-function-calls-to-make-target-array "Minimum Numbers of Function Calls to Make Target Array") |
| 9 | + |
| 10 | +[Next >](../most-visited-sector-in-a-circular-track "Most Visited Sector in a Circular Track") |
| 11 | + |
| 12 | +## [1559. Detect Cycles in 2D Grid (Hard)](https://leetcode.com/problems/detect-cycles-in-2d-grid "二维网格图中探测环") |
| 13 | + |
| 14 | +<p>Given a 2D array of characters <code>grid</code> of size <code>m x n</code>, you need to find if there exists any cycle consisting of the <strong>same value</strong> in <code>grid</code>.</p> |
| 15 | + |
| 16 | +<p>A cycle is a path of <strong>length 4 or more</strong> in the grid that starts and ends at the same cell. From a given cell, you can move to one of the cells adjacent to it - in one of the four directions (up, down, left, or right), if it has the <strong>same value</strong> of the current cell.</p> |
| 17 | + |
| 18 | +<p>Also, you cannot move to the cell that you visited in your last move. For example, the cycle <code>(1, 1) -> (1, 2) -> (1, 1)</code> is invalid because from <code>(1, 2)</code> we visited <code>(1, 1)</code> which was the last visited cell.</p> |
| 19 | + |
| 20 | +<p>Return <code>true</code> if any cycle of the same value exists in <code>grid</code>, otherwise, return <code>false</code>.</p> |
| 21 | + |
| 22 | +<p> </p> |
| 23 | +<p><strong>Example 1:</strong></p> |
| 24 | + |
| 25 | +<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/07/15/1.png" style="width: 231px; height: 152px;" /></strong></p> |
| 26 | + |
| 27 | +<pre> |
| 28 | +<strong>Input:</strong> grid = [["a","a","a","a"],["a","b","b","a"],["a","b","b","a"],["a","a","a","a"]] |
| 29 | +<strong>Output:</strong> true |
| 30 | +<strong>Explanation: </strong>There are two valid cycles shown in different colors in the image below: |
| 31 | +<img alt="" src="https://assets.leetcode.com/uploads/2020/07/15/11.png" style="width: 225px; height: 163px;" /> |
| 32 | +</pre> |
| 33 | + |
| 34 | +<p><strong>Example 2:</strong></p> |
| 35 | + |
| 36 | +<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/07/15/22.png" style="width: 236px; height: 154px;" /></strong></p> |
| 37 | + |
| 38 | +<pre> |
| 39 | +<strong>Input:</strong> grid = [["c","c","c","a"],["c","d","c","c"],["c","c","e","c"],["f","c","c","c"]] |
| 40 | +<strong>Output:</strong> true |
| 41 | +<strong>Explanation: </strong>There is only one valid cycle highlighted in the image below: |
| 42 | +<img alt="" src="https://assets.leetcode.com/uploads/2020/07/15/2.png" style="width: 229px; height: 157px;" /> |
| 43 | +</pre> |
| 44 | + |
| 45 | +<p><strong>Example 3:</strong></p> |
| 46 | + |
| 47 | +<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/07/15/3.png" style="width: 183px; height: 120px;" /></strong></p> |
| 48 | + |
| 49 | +<pre> |
| 50 | +<strong>Input:</strong> grid = [["a","b","b"],["b","z","b"],["b","b","a"]] |
| 51 | +<strong>Output:</strong> false |
| 52 | +</pre> |
| 53 | + |
| 54 | +<p> </p> |
| 55 | +<p><strong>Constraints:</strong></p> |
| 56 | + |
| 57 | +<ul> |
| 58 | + <li><code>m == grid.length</code></li> |
| 59 | + <li><code>n == grid[i].length</code></li> |
| 60 | + <li><code>1 <= m <= 500</code></li> |
| 61 | + <li><code>1 <= n <= 500</code></li> |
| 62 | + <li><code>grid</code> consists only of lowercase English letters.</li> |
| 63 | +</ul> |
| 64 | + |
| 65 | +### Related Topics |
| 66 | + [[Depth-first Search](../../tag/depth-first-search/README.md)] |
| 67 | + |
| 68 | +### Hints |
| 69 | +<details> |
| 70 | +<summary>Hint 1</summary> |
| 71 | +Keep track of the parent (previous position) to avoid considering an invalid path. |
| 72 | +</details> |
| 73 | + |
| 74 | +<details> |
| 75 | +<summary>Hint 2</summary> |
| 76 | +Use DFS or BFS and keep track of visited cells to see if there is a cycle. |
| 77 | +</details> |
0 commit comments