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