Problem
There is a ball in a maze
with empty spaces (represented as 0
) and walls (represented as 1
). The ball can go through the empty spaces by rolling up, down, left or right, but it won’t stop rolling until hitting a wall. When the ball stops, it could choose the next direction (must be different from last chosen direction). There is also a hole in this maze. The ball will drop into the hole if it rolls onto the hole.
Given the m x n
maze
, the ball’s position ball
and the hole’s position hole
, where ball = [ballrow, ballcol]
and hole = [holerow, holecol]
, return a string instructions
of all the instructions that the ball should follow to drop in the hole with the shortest distance possible. If there are multiple valid instructions, return the lexicographically minimum one. If the ball can’t drop in the hole, return "impossible"
.
If there is a way for the ball to drop in the hole, the answer instructions
should contain the characters 'u'
(i.e., up), 'd'
(i.e., down), 'l'
(i.e., left), and 'r'
(i.e., right).
The distance is the number of empty spaces traveled by the ball from the start position (excluded) to the destination (included).
You may assume that the borders of the maze are all walls (see examples).
Examples
Example 1
$$ \Huge \begin{array}{|c|c|c|c|c|c|c|} \hline 🟨 & 🟨 & 🟨 & 🟨 & 🟨 & 🟨 & 🟨 \\ \hline 🟨 & & 📍 & & & & 🟨 \\ \hline 🟨 & 🟨 & 🟨 & & & 🟨 & 🟨 \\ \hline 🟨 & & & & & & 🟨 \\ \hline 🟨 & & 🟨 & & & 🟨 & 🟨 \\ \hline 🟨 & & 🟨 & & ⚽️ & & 🟨 \\ \hline 🟨 & 🟨 & 🟨 & 🟨 & 🟨 & 🟨 & 🟨 \\ \hline \end{array} $$
|
|
Example 2
$$ \Huge \begin{array}{|c|c|c|c|c|c|c|} \hline 🟨 & 🟨 & 🟨 & 🟨 & 🟨 & 🟨 & 🟨 \\ \hline 🟨 & & & & & & 🟨 \\ \hline 🟨 & 🟨 & 🟨 & & & 🟨 & 🟨 \\ \hline 🟨 & & & & & & 🟨 \\ \hline 🟨 & 📍 & 🟨 & & & 🟨 & 🟨 \\ \hline 🟨 & & 🟨 & & ⚽️ & & 🟨 \\ \hline 🟨 & 🟨 & 🟨 & 🟨 & 🟨 & 🟨 & 🟨 \\ \hline \end{array} $$
|
|
Example 3
$$ \Huge \begin{array}{|c|c|c|c|c|c|c|c|c|} \hline 🟨 & 🟨 & 🟨 & 🟨 & 🟨 & 🟨 & 🟨 & 🟨 & 🟨 \\ \hline 🟨 & & & & & ⚽️ & & & 🟨 \\ \hline 🟨 & & & 🟨 & & & 🟨 & & 🟨 \\ \hline 🟨 & & & & & 🟨 & & & 🟨 \\ \hline 🟨 & & & & & & 📍 & 🟨 & 🟨 \\ \hline 🟨 & 🟨 & 🟨 & 🟨 & 🟨 & 🟨 & 🟨 & 🟨 & 🟨 \\ \hline \end{array} $$
|
|
Solution
Method 1 – Dijkstra’s Algorithm with Lexicographical Path Tracking
Intuition
The key idea is to use Dijkstra’s algorithm to find the shortest path (minimum distance) to the hole, and for equal distances, keep the lexicographically smallest path. For each position, roll the ball in all four directions until it hits a wall or the hole, and track both distance and path string.
Approach
- Use a min-heap to always process the cell with the smallest current distance and, for ties, the lexicographically smallest path.
- For each cell, roll the ball in all four directions until it hits a wall or the hole, counting the distance and building the path string.
- If the new position has a smaller distance or a lexicographically smaller path for the same distance, update and push to the heap.
- When the hole is reached, return the path string.
- If the hole is unreachable, return “impossible”.
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(mn log(mn))
, where m and n are the maze dimensions; Dijkstra’s algorithm dominates. - 🧺 Space complexity:
O(mn)
, for the distance and path matrices and heap.