Tap here to switch tabs
Problem
Submissions

Problem: Minimum Moves to Reach Target with Rotations

Try to solve the Minimum Moves to Reach Target with Rotations problem.

Statement

You are given an n×nn \times n matrix grid, where each cell contains either 00 indicating an empty cell or 11 indicating an obstacle.

A snake of length 22 initially occupies the cells (0,0)(0, 0) and (0,1)(0, 1) in a horizontal orientation. The target is for the snake to reach the bottom right position, occupying the cells (n−1,n−2)(n - 1, n - 2) and (n−1,n−1)(n - 1, n - 1) horizontally.

In each move, the snake can perform one of the following actions:

  • Move one cell to the right:

    • If the snake is horizontal, both cells must be shifted right by 11, requiring the cell directly to the right of its head to be empty.

    • If the snake is vertical, both cells must be shifted right by 11, requiring both cells immediately to the right of its body to be empty.

  • Move one cell down:

    • If the snake is horizontal, both cells must be shifted down by 11, requiring both cells immediately below its body to be empty.

    • If the snake is vertical, both cells must be shifted down by 11, requiring the cell directly below its head to be empty.

  • Rotate clockwise:

    • If the snake is horizontal, it can rotate clockwise around its tail to become vertical, occupying (r,c)(r, c) and (r+1,c)(r + 1, c), provided that the cells (r+1,c)(r + 1, c) and (r+1,c+1)(r + 1, c + 1) are both empty.

  • Rotate counterclockwise:

    • If the snake is vertical, it can rotate counterclockwise around its tail to become horizontal, occupying (r,c)(r, c) and (r,c+1)(r, c + 1), provided that the cells (r,c+1)(r, c + 1) and (r+1,c+1)(r + 1, c + 1) are both empty.

Return the minimum number of moves required to reach the target position, or −1-1 if it is impossible.

Constraints:

  • 2≤n≤1002 \leq n \leq 100

  • grid.length ==n== n

  • grid[i].length ==n== n

  • 0≤0 \leq grid[i][j] ≤1\leq 1

  • It is guaranteed that the snake starts at empty cells.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimum Moves to Reach Target with Rotations

Try to solve the Minimum Moves to Reach Target with Rotations problem.

Statement

You are given an n×nn \times n matrix grid, where each cell contains either 00 indicating an empty cell or 11 indicating an obstacle.

A snake of length 22 initially occupies the cells (0,0)(0, 0) and (0,1)(0, 1) in a horizontal orientation. The target is for the snake to reach the bottom right position, occupying the cells (n−1,n−2)(n - 1, n - 2) and (n−1,n−1)(n - 1, n - 1) horizontally.

In each move, the snake can perform one of the following actions:

  • Move one cell to the right:

    • If the snake is horizontal, both cells must be shifted right by 11, requiring the cell directly to the right of its head to be empty.

    • If the snake is vertical, both cells must be shifted right by 11, requiring both cells immediately to the right of its body to be empty.

  • Move one cell down:

    • If the snake is horizontal, both cells must be shifted down by 11, requiring both cells immediately below its body to be empty.

    • If the snake is vertical, both cells must be shifted down by 11, requiring the cell directly below its head to be empty.

  • Rotate clockwise:

    • If the snake is horizontal, it can rotate clockwise around its tail to become vertical, occupying (r,c)(r, c) and (r+1,c)(r + 1, c), provided that the cells (r+1,c)(r + 1, c) and (r+1,c+1)(r + 1, c + 1) are both empty.

  • Rotate counterclockwise:

    • If the snake is vertical, it can rotate counterclockwise around its tail to become horizontal, occupying (r,c)(r, c) and (r,c+1)(r, c + 1), provided that the cells (r,c+1)(r, c + 1) and (r+1,c+1)(r + 1, c + 1) are both empty.

Return the minimum number of moves required to reach the target position, or −1-1 if it is impossible.

Constraints:

  • 2≤n≤1002 \leq n \leq 100

  • grid.length ==n== n

  • grid[i].length ==n== n

  • 0≤0 \leq grid[i][j] ≤1\leq 1

  • It is guaranteed that the snake starts at empty cells.