Tap here to switch tabs
Problem
Submissions

Problem: The Maze II

Try to solve the The Maze II problem.

Statement

There is a ball in an m ×\times n maze consisting of empty spaces represented by 00 and walls represented by 11. The ball can travel through empty spaces by rolling up, down, left, or right. It continues rolling in the chosen direction and does not stop until it hits a wall. Once the ball stops, it can choose a new direction.

Given the m ×\times n grid maze, the initial position of the ball start =[= [ startrow_{\text{row}}, startcol]_{\text{col}} ], and the target position destination =[= [ destinationrow_{\text{row}}, destinationcol]_{\text{col}} ], return the shortest distance for the ball to stop at the destination. If it is impossible for the ball to stop at the destination, return −1-1.

The distance is measured by the total number of empty spaces traveled from the start position to the destination.

Constraints:

  • m ==== maze.length

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

  • 1≤1 \leq m, n ≤100\leq 100

  • maze[i][j] is 00 or 11.

  • start.length ==2== 2

  • destination.length ==2== 2

  • 0≤0 \leq startrow_{\text{row}}, destinationrow<_{\text{row}} < m

  • 0≤0 \leq startcol_{\text{col}}, destinationcol<_{\text{col}} < n

  • Both the ball and the destination exist in an empty space, and they will not be in the same position initially.

  • The maze contains at least 22 empty spaces.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: The Maze II

Try to solve the The Maze II problem.

Statement

There is a ball in an m ×\times n maze consisting of empty spaces represented by 00 and walls represented by 11. The ball can travel through empty spaces by rolling up, down, left, or right. It continues rolling in the chosen direction and does not stop until it hits a wall. Once the ball stops, it can choose a new direction.

Given the m ×\times n grid maze, the initial position of the ball start =[= [ startrow_{\text{row}}, startcol]_{\text{col}} ], and the target position destination =[= [ destinationrow_{\text{row}}, destinationcol]_{\text{col}} ], return the shortest distance for the ball to stop at the destination. If it is impossible for the ball to stop at the destination, return −1-1.

The distance is measured by the total number of empty spaces traveled from the start position to the destination.

Constraints:

  • m ==== maze.length

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

  • 1≤1 \leq m, n ≤100\leq 100

  • maze[i][j] is 00 or 11.

  • start.length ==2== 2

  • destination.length ==2== 2

  • 0≤0 \leq startrow_{\text{row}}, destinationrow<_{\text{row}} < m

  • 0≤0 \leq startcol_{\text{col}}, destinationcol<_{\text{col}} < n

  • Both the ball and the destination exist in an empty space, and they will not be in the same position initially.

  • The maze contains at least 22 empty spaces.