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.
Example 1
Input 1: a maze represented by a 2D array
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
Input 2: start coordinate (rowStart, colStart) = (0, 4)
Input 3: destination coordinate (rowDest, colDest) = (4, 4)
Output: 12
Explanation: One shortest way is : left -> down -> left -> down -> right -> down -> right. The total distance is 1 + 1 + 3 + 1 + 2 + 2 + 2 = 12.

Example 2
Input 1: a maze represented by a 2D array
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
Input 2: start coordinate (rowStart, colStart) = (0, 4)
Input 3: destination coordinate (rowDest, colDest) = (3, 2)
Output: -1
Explanation: There is no way for the ball to stop at the destination.

Note:
There is only one ball and one destination in the maze.
Both the ball and the destination exist on an empty space, and they will not be at the same position initially.
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.
The maze contains at least 2 empty spaces, and both the width and height of the maze won't exceed 100.
一刷
題解:
同LC490, 但是這里不僅要碰到壁才能停下,還要找出最短的路徑。
最值都可以用動(dòng)態(tài)規(guī)劃(cache的思路,把已經(jīng)存在的最小值保存下來(lái)),也就是構(gòu)造length[m][n]的array, 保存經(jīng)過(guò)點(diǎn)(i, j)的當(dāng)前最小值,如果新的路徑經(jīng)過(guò)改點(diǎn),比最小值大,直接跳過(guò),否則update最小值。由于是求最值,并且要撞墻才會(huì)停,于是要遍歷到圖中每個(gè)點(diǎn)。
class Solution {
class Point {
int x,y,l;
public Point(int x, int y, int l) {
this.x=x;
this.y=y;
this.l=l;
}
}
public int shortestDistance(int[][] maze, int[] start, int[] destination) {
int m = maze.length, n = maze[0].length;
int[][] length = new int[m][n];
for(int[] len : length){
Arrays.fill(len, Integer.MAX_VALUE);
}
int[][] dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
PriorityQueue<Point> list = new PriorityQueue<>((o1, o2)->o1.l - o2.l);
list.offer(new Point(start[0], start[1], 0));
while(!list.isEmpty()){
Point p = list.poll();
if(length[p.x][p.y]<=p.l) continue;//already have found a shorter route
length[p.x][p.y] = p.l;
for(int[] dir: dirs){
int xx = p.x;
int yy = p.y;
int l = p.l;
while(xx>=0 && xx<m && yy>=0 && yy<n && maze[xx][yy] == 0){
xx+=dir[0];
yy+=dir[1];
l++;
}
xx-=dir[0];
yy-=dir[1];
l--;
list.offer(new Point(xx, yy, l));
}
}
return length[destination[0]][destination[1]]==Integer.MAX_VALUE?-1:length[destination[0]][destination[1]];
}
}
二刷
同上
class Solution {
class Point implements Comparable<Point>{
int x,y,l;
public Point(int _x, int _y, int _l) {x=_x;y=_y;l=_l;}
@Override
public int compareTo(Point o){
return this.l - o.l;
}
}
public int shortestDistance(int[][] maze, int[] start, int[] destination) {
int m=maze.length, n=maze[0].length;
int[][] length = new int[m][n];
int[][] dirs = {{-1,0},{0,1},{1,0},{0,-1}};
for(int[] len : length) Arrays.fill(len, Integer.MAX_VALUE);
PriorityQueue<Point> pq = new PriorityQueue<>();
pq.offer(new Point(start[0], start[1], 0));
while(!pq.isEmpty()){
Point cur = pq.poll();
for(int[] dir : dirs){
int x = cur.x;
int y = cur.y;
int l = cur.l;
while(x>=0 && x<m && y>=0 && y<n && maze[x][y] == 0){
x += dir[0];
y += dir[1];
l++;
}
l--;
x -= dir[0];
y -= dir[1];
if(length[x][y]<=l) continue;
length[x][y] = l;
//System.out.println(x+"/"+y+"/"+l);
pq.offer(new Point(x, y, l));
}
}
return length[destination[0]][destination[1]]==Integer.MAX_VALUE?-1:length[destination[0]][destination[1]];
}
}