499. 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.

 

Example 1:

Input 1:

 a maze represented by a 2D array
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
Input 2:

 ball coordinate (rowBall, colBall) = (4, 3)
Input 3:

 hole coordinate (rowHole, colHole) = (0, 1)
Output:

 "lul"
Explanation:

 There are two shortest ways for the ball to drop into the hole.
The first way is left -> up -> left, represented by "lul".
The second way is up -> left, represented by 'ul'.
Both ways have shortest distance 6, but the first way is lexicographically smaller because 'l' < 'u'. So the output is "lul".

Example 2:

Input 1:

 a maze represented by a 2D array
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
Input 2:

 ball coordinate (rowBall, colBall) = (4, 3)
Input 3:

 hole coordinate (rowHole, colHole) = (3, 0)
Output:

 "impossible"
Explanation:

 The ball cannot reach the hole.

 

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.

在迷宫中有一个球,里面有空的空间和墙壁。球可以通过滚up (u)down (d)left (l)或右right (r)来穿过空的空间,但它不会停止滚动直到撞到墙上。当球停止时,它可以选择下一个方向。在这个迷宫里还有一个洞。如果球滚到洞里,球就会掉进洞里。
给定球的位置、洞的位置和迷宫,找出球如何通过移动最短距离落入洞内。距离是由球从起始位置(被排除)到洞(包括)所走过的空空间的数量来定义的。用“u”、“d”、“l”和“r”来输出移动的方向。由于可能有几种不同的最短路径,所以你应该输出字母顺序中(移动顺序中)最短的方法。如果球打不进洞,输出“impossible”。
迷宫由二维数组表示。1表示墙和0表示空的空间。你可以假设迷宫的边界都是墙。球和孔坐标用行和列的索引表示。

样例

样例 1:

输入:
[[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]

输出:
"lul"

样例 2:

输入:
[[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]
输出:
"impossible"

注意事项

1.迷宫中只有一个球和一个洞。
2.球和洞都存在于一个空的空间中,它们最初不会处于相同的位置。
3.给定的迷宫不包含边框(比如图片中的红色矩形),但是你可以假设迷宫的边界都是墙。
4.迷宫中至少有2个空的空间,迷宫的宽度和高度不会超过30。

Difficulty:

Hard

Lock:

Prime

Company:

Google