1210. Minimum Moves to Reach Target with Rotations Hard
1/**
2 * [1210] Minimum Moves to Reach Target with Rotations
3 *
4 * In an n*n grid, there is a snake that spans 2 cells and starts moving from the top left corner at (0, 0) and (0, 1). The grid has empty cells represented by zeros and blocked cells represented by ones. The snake wants to reach the lower right corner at (n-1, n-2) and (n-1, n-1).
5 * In one move the snake can:
6 *
7 * 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.
8 * Move down one cell if there are no blocked cells there. This move keeps the horizontal/vertical position of the snake as it is.
9 * 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 (r, c) and (r, c+1) to (r, c) and (r+1, c).<br />
10 * <img alt="" src="https://assets.leetcode.com/uploads/2019/09/24/image-2.png" style="width: 300px; height: 134px;" />
11 * 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 (r, c) and (r+1, c) to (r, c) and (r, c+1).<br />
12 * <img alt="" src="https://assets.leetcode.com/uploads/2019/09/24/image-1.png" style="width: 300px; height: 121px;" />
13 *
14 * Return the minimum number of moves to reach the target.
15 * If there is no way to reach the target, return -1.
16 *
17 * Example 1:
18 * <img alt="" src="https://assets.leetcode.com/uploads/2019/09/24/image.png" style="width: 400px; height: 439px;" />
19 *
20 * Input: grid = [[0,0,0,0,0,1],
21 * [1,1,0,0,1,0],
22 * [0,0,0,0,1,1],
23 * [0,0,1,0,1,0],
24 * [0,1,1,0,0,0],
25 * [0,1,1,0,0,0]]
26 * Output: 11
27 * Explanation:
28 * One possible solution is [right, right, rotate clockwise, right, down, down, down, down, rotate counterclockwise, right, down].
29 *
30 * Example 2:
31 *
32 * Input: grid = [[0,0,1,1,1,1],
33 * [0,0,0,0,1,1],
34 * [1,1,0,0,0,1],
35 * [1,1,1,0,0,1],
36 * [1,1,1,0,0,1],
37 * [1,1,1,0,0,0]]
38 * Output: 9
39 *
40 *
41 * Constraints:
42 *
43 * 2 <= n <= 100
44 * 0 <= grid[i][j] <= 1
45 * It is guaranteed that the snake starts at empty cells.
46 *
47 */
48pub struct Solution {}
49
50// problem: https://leetcode.com/problems/minimum-moves-to-reach-target-with-rotations/
51// discuss: https://leetcode.com/problems/minimum-moves-to-reach-target-with-rotations/discuss/?currentPage=1&orderBy=most_votes&query=
52
53// submission codes start here
54
55impl Solution {
56 pub fn minimum_moves(grid: Vec<Vec<i32>>) -> i32 {
57 0
58 }
59}
60
61// submission codes end
62
63#[cfg(test)]
64mod tests {
65 use super::*;
66
67 #[test]
68 fn test_1210() {
69 }
70}
71
Back
© 2025 bowen.ge All Rights Reserved.