Problem · Matrix
The Maze
Learn this problemProblem statement
A ball is placed in a rectangular maze represented by a binary matrix. Empty cells contain 0 and walls contain 1. The ball can move up, down, left, or right, but it keeps rolling in the chosen direction until a wall stops it.
Given the ball's start and destination cells, return true if the ball can stop at the destination and false otherwise.
Function
hasPath(maze: int[][], start: int[], destination: int[]) → booleanExamples
Example 1
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]return = trueA sequence of rolls can stop the ball at the destination.
Example 2
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 = [3,2]return = falseThe ball can pass through the destination cell but cannot stop there.
Constraints
1 <= maze.length, maze[0].length <= 100maze[i][j]is either0or1.startanddestinationeach contain two coordinates.- The start and destination cells are empty.
- The maze is surrounded by walls outside its boundary.