The Maze II - Problem
The Maze II - Find the Shortest Path for a Rolling Ball
Imagine a ball trapped in a maze that can't stop rolling until it hits a wall! This is a fascinating pathfinding problem where you need to find the shortest distance for the ball to reach its destination.
The Challenge:
Given an
Input: A 2D matrix maze, start position
Output: The shortest distance (number of empty spaces traveled), or
Key Rules:
• Ball rolls continuously until hitting a wall
• Distance counts empty spaces from start (excluded) to destination (included)
• All maze borders are walls
• Ball can choose direction only when it stops
Imagine a ball trapped in a maze that can't stop rolling until it hits a wall! This is a fascinating pathfinding problem where you need to find the shortest distance for the ball to reach its destination.
The Challenge:
Given an
m × n maze where 0 represents empty spaces and 1 represents walls, help the ball find the shortest path from start to destination. The twist? The ball keeps rolling in the chosen direction until it hits a wall!Input: A 2D matrix maze, start position
[startRow, startCol], and destination [destRow, destCol]Output: The shortest distance (number of empty spaces traveled), or
-1 if impossibleKey Rules:
• Ball rolls continuously until hitting a wall
• Distance counts empty spaces from start (excluded) to destination (included)
• All maze borders are walls
• Ball can choose direction only when it stops
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:
12
💡 Note:
One possible path is: left -> down -> left -> down -> right -> down -> right. The total distance is 1 + 1 + 3 + 1 + 2 + 2 + 2 = 12.
example_2.py — Impossible 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:
-1
💡 Note:
There is no way for the ball to stop at the destination. The ball will roll past the destination due to walls blocking it from stopping there.
example_3.py — Same Position
$
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 = [4,3], destination = [4,3]
›
Output:
0
💡 Note:
The start and destination are the same, so the distance is 0.
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 coordinates represent empty spaces
- The borders of the maze are all walls
Visualization
Tap to expand
Understanding the Visualization
1
Understand Rolling
Ball rolls continuously in chosen direction until hitting a wall
2
Model as Graph
Each stopping position is a node, each roll is a weighted edge
3
Use Dijkstra's
Since edges have different weights (distances), use shortest path algorithm
4
Find Optimal Path
Priority queue ensures we find the minimum distance to destination
Key Takeaway
🎯 Key Insight: The maze ball problem is actually a shortest path problem in a weighted graph, where each 'roll' has a different cost (distance). Dijkstra's algorithm guarantees we find the optimal solution by always exploring the shortest known path first!
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code