The Maze - Problem
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
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
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.
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 otherwiseExample: 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
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.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code