The Maze II
A ball is in a maze of empty spaces (0) and walls (1). The ball can roll up, down, left, or right, but it does not stop rolling until it hits a wall. Given the maze, the ball's start position, and a destination, return the shortest distance (number of empty cells traveled) for the ball to stop at the destination, or -1 if it cannot stop there.
Open official problem prompt ↗Compute the fewest empty cells the ball must travel over to come to rest exactly on the destination.
Like sliding a hockey puck on ice: once you push it, it glides until it slams into the boards; you pay for every tile it slides across.
- Input
- maze = [[0,0,1,0,0],[0,0,0,0,0],[0,0,0,1,0],[1,1,0,1,1],[0,0,0,0,0]], start = [0,4], destination = [4,4]
- Output
- 12
- Why
- The shortest sequence of rolls that stops exactly at (4,4) covers 12 traveled cells.
m == maze.length, n == maze[i].length1 <= m, n <= 100maze[i][j] is 0 or 1start and destination are empty cellsstart != destinationThe borders are all walls