Imagine a rolling ball trapped in a maze with a twist! This ball can't stop on a dime - once it starts rolling in a direction, it keeps going until it hits a wall. Your mission? Guide this ball through the maze to drop into a hole using the shortest path possible.

The maze is represented as an m x n grid where:

  • 0 represents empty spaces
  • 1 represents walls

The ball starts at position ball = [ballrow, ballcol] and needs to reach the hole at hole = [holerow, holecol]. Here's the catch:

  • ๐ŸŽฏ The ball rolls continuously until it hits a wall or falls into the hole
  • ๐Ÿ“ Distance is measured by the number of empty spaces traveled
  • ๐Ÿ”ค If multiple shortest paths exist, return the lexicographically smallest instruction sequence
  • โšก Valid moves: 'u' (up), 'd' (down), 'l' (left), 'r' (right)

Return the instruction string for the shortest path, or "impossible" if no path exists.

Input & Output

example_1.py โ€” Basic maze with single path
$ 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]], ball = [4,3], hole = [0,1]
โ€บ Output: "lul"
๐Ÿ’ก Note: Ball rolls left until hitting wall at (4,0), then up until hitting wall at (0,0), then left to reach hole at (0,1). Total distance: 3+4+1=8 steps with path "lul".
example_2.py โ€” Multiple paths with lexicographical choice
$ 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]], ball = [4,3], hole = [3,0]
โ€บ Output: "ul"
๐Ÿ’ก Note: Two possible paths: "ul" (up 1 step, left 3 steps = 4 total) and "lu" (left 3 steps, up 1 step = 4 total). Since both have same distance, return lexicographically smaller "ul".
example_3.py โ€” Impossible maze
$ 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]], ball = [4,3], hole = [4,0]
โ€บ Output: "impossible"
๐Ÿ’ก Note: Ball can reach position (4,0) but cannot stop there because it would roll past the hole, making it impossible to drop into the hole at that exact position.

Constraints

  • m == maze.length
  • n == maze[i].length
  • 1 โ‰ค m, n โ‰ค 100
  • maze[i][j] is 0 or 1
  • ball.length == 2
  • hole.length == 2
  • 0 โ‰ค ballrow, holerow < m
  • 0 โ‰ค ballcol, holecol < n
  • Both the ball and hole exist in an empty space
  • The maze borders are all walls

Visualization

Tap to expand
Ball Rolling Through MazeSH"r" (right)"d" (down)Algorithm Steps1Initialize priority queue2Process min distance3Roll in each direction4Update distances5Found optimal path!Result: "rd"Distance: 6 steps
Understanding the Visualization
1
Start Position
Ball begins at starting position, ready to roll in any direction
2
Roll Until Obstacle
When ball rolls in a direction, it continues until hitting a wall or falling into hole
3
Calculate Distance
Count the number of empty spaces traveled during each rolling movement
4
Track Best Path
Keep track of shortest total distance and lexicographically smallest instruction sequence
5
Priority Queue Processing
Use Dijkstra's algorithm to efficiently explore paths in order of increasing distance
Key Takeaway
๐ŸŽฏ Key Insight: This problem combines shortest path algorithms with string comparison. Using Dijkstra's algorithm with a custom comparator ensures we find the optimal solution efficiently while handling lexicographical ordering requirements.
Asked in
Google 42 Amazon 38 Meta 31 Microsoft 25
52.4K Views
Medium-High Frequency
~25 min Avg. Time
1.9K 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