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 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 impossible

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

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
🎮 Pinball Machine Analogy🏀🎯Why Dijkstra's Algorithm?🎯 Different rolls = different distances📊 This creates a weighted graph problem🏃‍♂️ BFS won't work (assumes equal weights)🎪 Dijkstra guarantees shortest path!Example: Rolling right 3 spaces ≠ rolling up 1 spaceWe need to consider actual distances, not just steps!Algorithm Steps:1. Use priority queue (min-heap) for positions2. Always process position with minimum distance3. Roll ball in all 4 directions until hitting walls4. Add new positions to queue with total distance5. First time reaching destination = optimal!
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!
Asked in
Google 45 Amazon 38 Facebook 32 Microsoft 28 Apple 22 Uber 18
67.8K Views
High 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