499 The Maze III

499. The Maze III

1. Question

There is aballin a maze with empty spaces and walls. The ball can go through empty spaces by rollingup(u),down(d),left(l) orright(r), but it won't stop rolling until hitting a wall. When the ball stops, it could choose the next direction. There is also aholein this maze. The ball will drop into the hole if it rolls on to the hole.
Given the ball position, the hole positionand the maze, find out how the ball could drop into the hole by moving theshortest distance. The distance is defined by the number of empty spaces traveled by the ball from the start position (excluded) to the hole (included). Output the moving directionsby using 'u', 'd', 'l' and 'r'. Since there could be several different shortest ways, you should output the lexicographically smallest way. If the ball cannot reach the hole, output "impossible".
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 ball and the hole coordinates are represented by row and column indexes.
Example 1
1
Input 1: a maze represented by a 2D array
2
3
0 0 0 0 0
4
1 1 0 0 1
5
0 0 0 0 0
6
0 1 0 0 1
7
0 1 0 0 0
8
9
10
Input 2:
11
ball coordinate (rowBall, colBall) = (4, 3)
12
13
Input 3:
14
hole coordinate (rowHole, colHole) = (0, 1)
15
16
17
Output:
18
"lul"
19
20
Explanation:
21
There are two shortest ways for the ball to drop into the hole.
22
The first way is left ->up -> left, represented by "lul".
23
The second way is up ->left, represented by 'ul'.
24
Both ways have shortest distance 6, but the first way is lexicographically smaller because 'l' <'u'. So the output is "lul".
Copied!
Example 2
1
Input 1: a maze represented by a 2D array
2
3
0 0 0 0 0
4
1 1 0 0 1
5
0 0 0 0 0
6
0 1 0 0 1
7
0 1 0 0 0
8
9
10
Input 2: ball coordinate (rowBall, colBall) = (4, 3)
11
12
Input 3: hole coordinate (rowHole, colHole) = (3, 0)
13
14
Output: "impossible"
15
16
Explanation:
17
The ball cannot reach the hole.
Copied!
Note:
  1. 1.
    There is only one ball and one hole in the maze.
  2. 2.
    Both the ball and hole 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 the width and the height of the maze won't exceed 30.

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 String findShortestWay(int[][] maze, int[] ball, int[] hole) {
13
if (maze == null || maze.length == 0) {
14
return "";
15
}
16
17
int m = maze.length, n = maze[0].length;
18
int[][] distance = new int[m][n];
19
20
for (int i = 0; i < m; i++) {
21
Arrays.fill(distance[i], Integer.MAX_VALUE);
22
}
23
24
distance[ball[0]][ball[1]] = 0;
25
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
26
String[] ways = {"u", "d", "l", "r"};
27
28
Map<Integer, String> map = new HashMap<>();
29
30
Queue<Point> queue = new LinkedList<>();
31
queue.add(new Point(ball[0], ball[1], 0));
32
33
findShortestWayByBFS(maze, queue, map, distance, hole, ways, directions);
34
35
return map.containsKey(hole[0] * n + hole[1]) ? map.get(hole[0] * n + hole[1]) : "impossible";
36
}
37
38
public void findShortestWayByBFS(int[][] maze, Queue<Point> queue, Map<Integer, String> map, int[][] distance, int[] hole, String[] ways, int[][] directions) {
39
int n = maze[0].length;
40
41
while (!queue.isEmpty()) {
42
Point curPoint = queue.remove();
43
44
for (int i = 0; i < 4; i++) {
45
int row = curPoint.row, col = curPoint.col, dist = curPoint.dist;
46
String path = map.getOrDefault(row * n + col, "");
47
48
while (isValid(row, col, maze, hole)) {
49
row += directions[i][0];
50
col += directions[i][1];
51
++dist;
52
}
53
54
if (row != hole[0] || col != hole[1]) {
55
row -= directions[i][0];
56
col -= directions[i][1];
57
--dist;
58
}
59
60
path += ways[i];
61
62
if (dist < distance[row][col]) {
63
distance[row][col] = dist;
64
map.put(row * n + col, path);
65
queue.add(new Point(row, col, dist));
66
}
67
else if (dist == distance[row][col] && path.compareTo(map.getOrDefault(row * n + col, "")) < 0) {
68
map.put(row * n + col, path);
69
queue.add(new Point(row, col, dist));
70
}
71
}
72
}
73
}
74
75
public boolean isValid(int row, int col, int[][] maze, int[] hole) {
76
return row >= 0 && row < maze.length && col >= 0 && col < maze[0].length && maze[row][col] == 0 && (row != hole[0] || col != hole[1]);
77
}
78
}
Copied!

3. Time & Space Complexity

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