FastPrepRolling-Ball Maze Reachability

Rolling-Ball Maze Reachability

Amazon logoAmazon● MediumFULLTIMEPHONE SCREEN
Learn

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[]) → boolean

Examples

Example 1

maze = [[0,0,0],[1,1,0],[0,0,0]]start = [0,0]destination = [2,0]return = true

Roll 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 = false

The 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 = true

The 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 0 or 1.
  • start and destination are two-element, zero-based [row,column] coordinates inside the maze, and both name open cells.

More Amazon problems

See Amazon hiring insights
public boolean hasPath(int[][] maze, int[] start, int[] destination) {
    // Write your code here.
}
maze[[0,0,0],[1,1,0],[0,0,0]]
start[0,0]
destination[2,0]
expectedtrue
Checking account…