[LintCode] Problem 789 - The Maze III

There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolling up (u), down (d), left (l) or right (r), but it won’t stop rolling until hitting a wall. When the ball stops, it could choose the next direction. There is also a hole in this maze. The ball will drop into the hole if it rolls on to the hole.

Given the ball position, the hole position and the maze, find out how the ball could drop into the hole by moving the shortest 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 directions by 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.

Note

  1. There is only one ball and one hole in the maze.
  2. Both the ball and hole 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 the width and the height of the maze won’t exceed 30.

Example

No.1

Input:
[[0,0,0,0,0],[1,1,0,0,1],[0,0,0,0,0],[0,1,0,0,1],[0,1,0,0,0]]
[4,3]
[0,1]

Output:
“lul”

No.2

Input:
[[0,0,0,0,0],[1,1,0,0,1],[0,0,0,0,0],[0,1,0,0,1],[0,1,0,0,0]]
[0,0]
[1,1]
[2,2]
[3,3]

Output:
“impossible”

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
public class Point implements Comparable<Point> {
private int x;
private int y;
private int distance;
private String path;

public Point(int x, int y, int distance, String path) {
this.x = x;
this.y = y;
this.distance = distance;
this.path = path;
}

@Override
public int compareTo(Point o) {
return this.distance == o.distance ? this.path.compareTo(o.path)
: this.distance - o.distance;
}
}

public String findShortestWay(int[][] maze, int[] ball, int[] hole) {
int n = maze.length;
int m = maze[0].length;
int[][] directions = {{1, 0}, {0, -1}, {0, 1}, {-1, 0}};
String[] dirs = {"d", "l", "r", "u"};
boolean[][] visit = new boolean[n][m];
PriorityQueue<Point> pq = new PriorityQueue<>();
pq.offer(new Point(ball[0], ball[1], 0, ""));

while (!pq.isEmpty()) {
Point point = pq.poll();

if (point.x == hole[0] && point.y == hole[1])
return point.path;
else if (visit[point.x][point.y])
continue;

visit[point.x][point.y] = true;

for (int i = 0; i < directions.length; i++) {
int x = point.x;
int y = point.y;
int distance = point.distance;
String path = point.path;

while (x >= 0 && y >= 0 && x < n && y < m
&& maze[x][y] == 0 && !(x == hole[0] && y == hole[1])) {
x += directions[i][0];
y += directions[i][1];
distance++;
}

if (!(x == hole[0] && y == hole[1])) {
x -= directions[i][0];
y -= directions[i][1];
distance--;
}

pq.offer(new Point(x, y, distance, path + dirs[i]));
}
}

return "impossible";
}