There is a ball in a maze with empty spaces and walls. The ball can go through 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.
Given the ball’s start position, the destination and the maze, find the shortest distance for the ball to stop at the destination. The distance is defined by the number of empty spaces traveled by the ball from the start position (excluded) to the destination (included). If the ball cannot stop at the destination, return -1.
The maze is represented by a binary 2D array. 1 means the wall and 0 means the empty space. You may assume that the borders of the maze are all walls. The start and destination coordinates are represented by row and column indexes.
1.There is only one ball and one destination in the maze.
2.Both the ball and the destination exist on an empty space, and they will not be at the same position initially.
3.The given maze does not contain border (like the red rectangle in the example pictures), but you could assume the border of the maze are all walls.
4.The maze contains at least 2 empty spaces, and both the width and height of the maze won’t exceed 100.
The key idea is to use Dijkstra’s algorithm to always expand the path with the minimum distance so far. For each position, roll the ball in all four directions until it hits a wall, and if the new distance is less than the previously recorded distance, update and push to the heap.
classSolution {
public:int shortestDistance(vector<vector<int>>& maze, vector<int>& start, vector<int>& dest) {
int m = maze.size(), n = maze[0].size();
vector<vector<int>> dist(m, vector<int>(n, INT_MAX));
dist[start[0]][start[1]] =0;
priority_queue<tuple<int,int,int>, vector<tuple<int,int,int>>, greater<>> pq;
pq.emplace(0, start[0], start[1]);
int dirs[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};
while (!pq.empty()) {
auto [d, i, j] = pq.top(); pq.pop();
if (i == dest[0] && j == dest[1]) return d;
for (auto& dir : dirs) {
int x = i, y = j, cnt =0;
while (x+dir[0] >=0&& x+dir[0] < m && y+dir[1] >=0&& y+dir[1] < n && maze[x+dir[0]][y+dir[1]] ==0) {
x += dir[0]; y += dir[1]; cnt++;
}
if (dist[x][y] > d + cnt) {
dist[x][y] = d + cnt;
pq.emplace(dist[x][y], x, y);
}
}
}
return-1;
}
};
classSolution {
funshortestDistance(maze: Array<IntArray>, start: IntArray, dest: IntArray): Int {
val m = maze.size
val n = maze[0].size
val dist = Array(m) { IntArray(n) { Int.MAX_VALUE } }
dist[start[0]][start[1]] = 0val pq = java.util.PriorityQueue(compareBy<IntArray> { it[0] })
pq.add(intArrayOf(0, start[0], start[1]))
val dirs = arrayOf(0 to 1, 1 to 0, 0 to -1, -1 to 0)
while (pq.isNotEmpty()) {
val(d, i, j) = pq.remove()
if (i == dest[0] && j == dest[1]) return d
for ((dx, dy) in dirs) {
var x = i; var y = j; var cnt = 0while (x+dx in0 until m && y+dy in0 until n && maze[x+dx][y+dy] ==0) {
x += dx; y += dy; cnt++ }
if (dist[x][y] > d + cnt) {
dist[x][y] = d + cnt
pq.add(intArrayOf(dist[x][y], x, y))
}
}
}
return -1 }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution:
defshortestDistance(self, maze: list[list[int]], start: list[int], dest: list[int]) -> int:
import heapq
m, n = len(maze), len(maze[0])
dist = [[float('inf')] * n for _ in range(m)]
dist[start[0]][start[1]] =0 h = [(0, start[0], start[1])]
while h:
d, i, j = heapq.heappop(h)
if [i, j] == dest:
return d
for dx, dy in ((0,1),(1,0),(0,-1),(-1,0)):
x, y, cnt = i, j, 0while0<= x+dx < m and0<= y+dy < n and maze[x+dx][y+dy] ==0:
x += dx; y += dy; cnt +=1if dist[x][y] > d + cnt:
dist[x][y] = d + cnt
heapq.heappush(h, (dist[x][y], x, y))
return-1