Rolling-Ball Maze Reachability
Problem statement
A ball starts at start in a rectangular maze. Cells marked 0 are open and cells marked 1 are walls. Choose one of the four cardinal directions. The ball continues rolling through open cells in that direction until the next step would hit a wall or leave the maze; only then can it choose another direction.
Return true if some sequence of rolls lets the ball stop at destination, and false otherwise. Passing through the destination without stopping does not count. The starting cell is already a stopping position, so equal start and destination coordinates return true.
Function
hasPath(maze: int[][], start: int[], destination: int[]) → booleanExamples
Example 1
maze = [[0,0,0],[1,1,0],[0,0,0]]start = [0,0]destination = [2,0]return = trueRoll right to (0,2), down to (2,2), then left to (2,0). Each turn happens at a stopping cell.
Example 2
maze = [[0,0,0]]start = [0,0]destination = [0,1]return = falseThe only horizontal roll from (0,0) stops at (0,2). The ball passes (0,1) but cannot stop there.
Example 3
maze = [[0]]start = [0,0]destination = [0,0]return = trueThe ball is already stopped at the destination before making any roll.
Constraints
1 <= maze.length, maze[0].length <= 100; the maze is rectangular.- Every cell is
0or1. startanddestinationare two-element, zero-based[row,column]coordinates inside the maze, and both name open cells.