FastPrepBinary Maze Reachability

Binary Maze Reachability

Amazon logoAmazon● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

You are given a nonempty rectangular binary matrix maze. A cell containing 1 is passable, and a cell containing 0 is blocked.

The two arrays start and destination contain zero-based coordinates in the form [row, column]. From a passable cell, each move goes exactly one cell up, down, left or right to another passable cell inside the matrix.

Return true if a route from start to destination exists; otherwise return false. If either endpoint is blocked, return false. A passable cell can reach itself using zero moves.

Function

canReachMaze(maze: int[][], start: int[], destination: int[]) → boolean

Examples

Example 1

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

One route is (0,0) -> (0,1) -> (1,1) -> (1,2) -> (2,2). Every visited cell contains 1, and each move joins orthogonally adjacent cells.

Example 2

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

The start has no passable orthogonal neighbor. The diagonal passable cells do not connect it to the destination.

Example 3

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

A route goes down to (1,0), right through (1,1) and (1,2), then up to (0,2). Moving only right and down would miss this valid route.

Constraints

  • 1 <= maze.length <= 40.
  • 1 <= maze[row].length <= 40; every row has the same length.
  • Every cell is 0 or 1.
  • start.length == destination.length == 2.
  • Both coordinates name cells inside the matrix: 0 <= row < maze.length and 0 <= column < maze[0].length.

More Amazon problems

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