Practice/Meta/Leetcode 490. The Maze
CodingMust
You are given a rectangular maze represented as a 2D grid where:
0 represents an empty space1 represents a wallA ball starts at a given position and can roll in four directions: up, down, left, or right. The key mechanic is that once the ball starts rolling in a direction, it cannot stop until it hits a wall or the maze boundary. The ball will stop in the empty cell just before the wall.
Your task is to determine whether the ball can reach a specific destination position and stop exactly there.
true if the ball can reach and stop at the destination, false otherwise1 <= maze.length, maze[0].length <= 100maze[i][j] is 0 or 1start and destination are guaranteed to be empty cells (value 0)Example 1:
` 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: true
Explanation: The ball starts at (0,4) and can roll down. It stops at (4,4) which is the destination. `
Example 2:
` 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 = [3,2]
Output: false
Explanation: The ball can reach (3,2) but cannot stop there. When rolling toward (3,2), it would continue past that position. `
Example 3:
` Input: maze = [[0,0,0], [0,1,0], [0,0,0]] start = [1,0] destination = [1,0]
Output: true
Explanation: The ball is already at the destination. `