The Maze - A Rolling Ball Adventure! ๐ŸŽณ

Imagine a ball rolling through a maze with a unique twist: the ball cannot stop until it hits a wall! This creates an interesting pathfinding challenge where you must think several steps ahead.

Problem: Given an m x n maze where 0 represents empty spaces and 1 represents walls, determine if a ball can reach its destination. The ball starts at position [startrow, startcol] and needs to reach [destinationrow, destinationcol].

Key Rules:
โ€ข The ball rolls in one direction until it hits a wall
โ€ข Only when stopped can it choose a new direction
โ€ข The maze borders are surrounded by walls
โ€ข Return true if the destination is reachable, false otherwise

Example: If the ball rolls right, it continues rolling right through all empty spaces until it hits a wall, then stops at the last empty space before the wall.

Input & Output

example_1.py โ€” Basic Maze
$ 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
๐Ÿ’ก Note: The ball can roll from start [0,4] down to [1,4], then left to [1,0], then down to [4,0], and finally right to reach destination [4,4].
example_2.py โ€” Blocked Path
$ 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
๐Ÿ’ก Note: The destination [3,2] is unreachable because there's no way for the ball to stop at that position - it would always roll past it or be blocked by walls.
example_3.py โ€” Same Start and Destination
$ Input: maze = [[0,0,0,0,0],[1,1,0,0,1],[0,0,0,0,0],[0,1,0,0,1],[0,1,0,0,0]] start = [0,0] destination = [0,0]
โ€บ Output: true
๐Ÿ’ก Note: When start and destination are the same position, the ball is already at the target, so the answer is true.

Constraints

  • m == maze.length
  • n == maze[i].length
  • 1 โ‰ค m, n โ‰ค 100
  • maze[i][j] is 0 or 1
  • start.length == 2
  • destination.length == 2
  • 0 โ‰ค startrow, destinationrow < m
  • 0 โ‰ค startcol, destinationcol < n
  • Both the start and destination positions are empty spaces
  • The borders of the maze are all walls

Visualization

Tap to expand
Ball Rolling Mechanics๐Ÿ”ด Start Position - Ball begins here๐ŸŸข Stopping Point - Where ball stops after rolling๐Ÿ”ต Destination - Target to reachโฌ› Wall - Blocks movementKey Algorithm Steps:1. Use DFS to explore all possible paths2. For each direction, roll until hitting wall3. Track visited stopping positions4. Return true if destination is reachable
Understanding the Visualization
1
Start Rolling
Ball begins at starting position and can roll in 4 directions
2
Continuous Motion
Once rolling, ball continues until hitting a wall - cannot stop mid-roll
3
Mark Stopping Points
Track where ball stops to avoid revisiting same positions
4
Explore All Paths
From each stopping point, try rolling in all four directions
5
Find Destination
Success when ball stops exactly at destination coordinates
Key Takeaway
๐ŸŽฏ Key Insight: The ball can only stop when it hits a wall, so we only need to track stopping positions, not every cell the ball rolls through. This dramatically reduces the search space and makes the problem tractable with simple DFS + visited set tracking.
Asked in
Google 45 Amazon 32 Meta 28 Microsoft 15
42.5K Views
Medium Frequency
~25 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen