505 The Maze II

505. The Maze II

1. Question

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
1
Input 1: a maze represented by a 2D array
2
3
0 0 1 0 0
4
0 0 0 0 0
5
0 0 0 1 0
6
1 1 0 1 1
7
0 0 0 0 0
8
9
Input 2: start coordinate (rowStart, colStart) = (0, 4)
10
11
Input 3: destination coordinate (rowDest, colDest) = (4, 4)
12
13
Output: 12
14
15
Explanation: One shortest way is : left -> down -> left -> down ->right ->down ->right.
16
The total distance is 1 + 1 + 3 + 1 + 2 + 2 + 2 = 12.
Copied!
Note:
  1. 1.
    There is only one ball and one destination in the maze.
  2. 2.
    Both the ball and the destination exist on an empty space, and they will not be at the same position initially.
  3. 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. 4.
    The maze contains at least 2 empty spaces, and both the width and height of the maze won't exceed 100.

2. Implementation

(1) BFS
1
class Solution {
2
class Point {
3
int row, col, dist;
4
5
public Point(int row, int col, int dist) {
6
this.row = row;
7
this.col = col;
8
this.dist = dist;
9
}
10
}
11
12
public int shortestDistance(int[][] maze, int[] start, int[] destination) {
13
if (maze == null || maze.length == 0) {
14
return 0;
15
}
16
17
int m = maze.length, n = maze[0].length;
18
int[][] distance = new int[m][n];
19
20
for (int[] dist : distance) {
21
Arrays.fill(dist, Integer.MAX_VALUE);
22
}
23
24
distance[start[0]][start[1]] = 0;
25
Queue<Point> queue = new LinkedList<>();
26
queue.add(new Point(start[0], start[1], 0));
27
28
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
29
30
while (!queue.isEmpty()) {
31
Point curCell = queue.remove();
32
int curRow = curCell.row;
33
int curCol = curCell.col;
34
int curDist = curCell.dist;
35
36
for (int[] direction : directions) {
37
int nextRow = curRow + direction[0];
38
int nextCol = curCol + direction[1];
39
int dist = curDist + 1;
40
41
42
while (isValid(maze, nextRow, nextCol)) {
43
nextRow += direction[0];
44
nextCol += direction[1];
45
dist += 1;
46
}
47
48
nextRow -= direction[0];
49
nextCol -= direction[1];
50
--dist;
51
52
if (dist > distance[destination[0]][destination[1]]) {
53
continue;
54
}
55
56
if (dist < distance[nextRow][nextCol]) {
57
distance[nextRow][nextCol] = dist;
58
queue.add(new Point(nextRow, nextCol, dist));
59
}
60
}
61
}
62
return distance[destination[0]][destination[1]] == Integer.MAX_VALUE ? -1 : distance[destination[0]][destination[1]];
63
}
64
65
public boolean isValid(int[][] maze, int row, int col) {
66
return row >= 0 && row < maze.length && col >= 0 && col < maze[0].length && maze[row][col] == 0;
67
}
68
}
Copied!
(2) DFS
1
class Solution {
2
public int shortestDistance(int[][] maze, int[] start, int[] destination) {
3
if (maze == null || maze.length == 0 || maze[0].length == 0) {
4
return -1;
5
}
6
7
int m = maze.length, n = maze[0].length;
8
int[][] distance = new int[m][n];
9
10
for (int i = 0; i < m; i++) {
11
Arrays.fill(distance[i], Integer.MAX_VALUE);
12
}
13
14
distance[start[0]][start[1]] = 0;
15
16
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
17
18
getDistanceByDFS(start, destination, distance, maze, directions);
19
return distance[destination[0]][destination[1]] == Integer.MAX_VALUE ? -1 : distance[destination[0]][destination[1]];
20
}
21
22
public void getDistanceByDFS(int[] start, int[] destination, int[][] distance, int[][] maze, int[][] directions) {
23
int curRow = start[0], curCol = start[1];
24
25
if (curRow == destination[0] && curCol == destination[1]) {
26
return;
27
}
28
29
30
for (int[] direction : directions) {
31
int nextRow = curRow + direction[0];
32
int nextCol = curCol + direction[1];
33
int dist = distance[curRow][curCol] + 1;
34
35
while (isValid(nextRow, nextCol, maze)) {
36
nextRow += direction[0];
37
nextCol += direction[1];
38
dist += 1;
39
}
40
41
nextRow -= direction[0];
42
nextCol -= direction[1];
43
dist -= 1;
44
45
if (dist < distance[nextRow][nextCol]) {
46
distance[nextRow][nextCol] = dist;
47
getDistanceByDFS(new int[] {nextRow, nextCol}, destination, distance, maze, directions);
48
}
49
}
50
}
51
52
public boolean isValid(int row, int col, int[][] maze) {
53
return row >= 0 && row < maze.length && col >= 0 && col < maze[0].length && maze[row][col] == 0;
54
}
55
}
Copied!

3. Time & Space Complexity

BFS: 时间复杂度O(m * n * Max(m,n)),空间复杂度O(mn)
DFS: 时间复杂度O(m * n * Max(m,n)),空间复杂度O(mn)