|
| 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](https://github.com/openset/leetcode/tree/master/problems/remove-all-adjacent-duplicates-in-string-ii "Remove All Adjacent Duplicates in String II") |
| 9 | + |
| 10 | +Next > |
| 11 | + |
| 12 | +## [5208. Minimum Moves to Reach Target with Rotations (Hard)](https://leetcode.com/problems/minimum-moves-to-reach-target-with-rotations "穿过迷宫的最少移动次数") |
| 13 | + |
| 14 | +<p>In an <code>n*n</code> grid, there is a snake that spans 2 cells and starts moving from the top left corner at <code>(0, 0)</code> and <code>(0, 1)</code>. The grid has empty cells represented by zeros and blocked cells represented by ones. The snake wants to reach the lower right corner at <code>(n-1, n-2)</code> and <code>(n-1, n-1)</code>.</p> |
| 15 | + |
| 16 | +<p>In one move the snake can:</p> |
| 17 | + |
| 18 | +<ul> |
| 19 | + <li>Move one cell to the right if there are no blocked cells there. This move keeps the horizontal/vertical position of the snake as it is.</li> |
| 20 | + <li>Move down one cell if there are no blocked cells there. This move keeps the horizontal/vertical position of the snake as it is.</li> |
| 21 | + <li>Rotate clockwise if it's in a horizontal position and the two cells under it are both empty. In that case the snake moves from <code>(r, c)</code> and <code>(r, c+1)</code> to <code>(r, c)</code> and <code>(r+1, c)</code>.<br /> |
| 22 | + <img alt="" src="https://assets.leetcode.com/uploads/2019/09/24/image-2.png" style="width: 300px; height: 134px;" /></li> |
| 23 | + <li>Rotate counterclockwise if it's in a vertical position and the two cells to its right are both empty. In that case the snake moves from <code>(r, c)</code> and <code>(r+1, c)</code> to <code>(r, c)</code> and <code>(r, c+1)</code>.<br /> |
| 24 | + <img alt="" src="https://assets.leetcode.com/uploads/2019/09/24/image-1.png" style="width: 300px; height: 121px;" /></li> |
| 25 | +</ul> |
| 26 | + |
| 27 | +<p>Return the minimum number of moves to reach the target.</p> |
| 28 | + |
| 29 | +<p>If there is no way to reach the target, return <code>-1</code>.</p> |
| 30 | + |
| 31 | +<p> </p> |
| 32 | +<p><strong>Example 1:</strong></p> |
| 33 | + |
| 34 | +<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/24/image.png" style="width: 400px; height: 439px;" /></strong></p> |
| 35 | + |
| 36 | +<pre> |
| 37 | +<strong>Input:</strong> grid = [[0,0,0,0,0,1], |
| 38 | + [1,1,0,0,1,0], |
| 39 | + [0,0,0,0,1,1], |
| 40 | + [0,0,1,0,1,0], |
| 41 | + [0,1,1,0,0,0], |
| 42 | + [0,1,1,0,0,0]] |
| 43 | +<strong>Output:</strong> 11 |
| 44 | +<strong>Explanation: |
| 45 | +</strong>One possible solution is [right, right, rotate clockwise, right, down, down, down, down, rotate counterclockwise, right, down]. |
| 46 | +</pre> |
| 47 | + |
| 48 | +<p><strong>Example 2:</strong></p> |
| 49 | + |
| 50 | +<pre> |
| 51 | +<strong>Input:</strong> grid = [[0,0,1,1,1,1], |
| 52 | + [0,0,0,0,1,1], |
| 53 | + [1,1,0,0,0,1], |
| 54 | + [1,1,1,0,0,1], |
| 55 | + [1,1,1,0,0,1], |
| 56 | + [1,1,1,0,0,0]] |
| 57 | +<strong>Output:</strong> 9 |
| 58 | +</pre> |
| 59 | + |
| 60 | +<p> </p> |
| 61 | +<p><strong>Constraints:</strong></p> |
| 62 | + |
| 63 | +<ul> |
| 64 | + <li><code>2 <= n <= 100</code></li> |
| 65 | + <li><code>0 <= grid[i][j] <= 1</code></li> |
| 66 | + <li>It is guaranteed that the snake starts at empty cells.</li> |
| 67 | +</ul> |
| 68 | + |
| 69 | +### Hints |
| 70 | +<details> |
| 71 | +<summary>Hint 1</summary> |
| 72 | +Use BFS to find the answer. |
| 73 | +</details> |
| 74 | + |
| 75 | +<details> |
| 76 | +<summary>Hint 2</summary> |
| 77 | +The state of the BFS is the position (x, y) along with a binary value that specifies if the position is horizontal or vertical. |
| 78 | +</details> |
0 commit comments