|
3 | 3 | import java.util.PriorityQueue;
|
4 | 4 | import java.util.Queue;
|
5 | 5 |
|
6 |
| -/** |
7 |
| - * 499. The Maze III |
8 |
| - * |
9 |
| - * There is a ball in a maze with empty spaces and walls. |
10 |
| - * The ball can go through empty spaces by rolling up (u), down (d), left (l) or right (r), |
11 |
| - * but it won't stop rolling until hitting a wall. |
12 |
| - * |
13 |
| - * When the ball stops, it could choose the next direction. |
14 |
| - * There is also a hole in this maze. The ball will drop into the hole if it rolls on to the hole. |
15 |
| - * |
16 |
| - * Given the ball position, the hole position and the maze, |
17 |
| - * find out how the ball could drop into the hole by moving the shortest distance. |
18 |
| - * |
19 |
| - * The distance is defined by the number of empty spaces traveled by the ball from the start |
20 |
| - * position (excluded) to the hole (included). |
21 |
| - * |
22 |
| - * Output the moving directions by using 'u', 'd', 'l' and 'r'. |
23 |
| - * Since there could be several different shortest ways, you should output the lexicographically smallest way. |
24 |
| - * |
25 |
| - * If the ball cannot reach the hole, output "impossible". |
26 |
| - * The maze is represented by a binary 2D array. |
27 |
| - * 1 means the wall and 0 means the empty space. |
28 |
| - * You may assume that the borders of the maze are all walls. |
29 |
| - * The ball and the hole coordinates are represented by row and column indexes. |
30 |
| -
|
31 |
| - Example 1 |
32 |
| -
|
33 |
| - Input 1: a maze represented by a 2D array |
34 |
| -
|
35 |
| - 0 0 0 0 0 |
36 |
| - 1 1 0 0 1 |
37 |
| - 0 0 0 0 0 |
38 |
| - 0 1 0 0 1 |
39 |
| - 0 1 0 0 0 |
40 |
| -
|
41 |
| - Input 2: ball coordinate (rowBall, colBall) = (4, 3) |
42 |
| - Input 3: hole coordinate (rowHole, colHole) = (0, 1) |
43 |
| -
|
44 |
| - Output: "lul" |
45 |
| - Explanation: There are two shortest ways for the ball to drop into the hole. |
46 |
| - The first way is left -> up -> left, represented by "lul". |
47 |
| - The second way is up -> left, represented by 'ul'. |
48 |
| - Both ways have shortest distance 6, but the first way is lexicographically smaller because 'l' < 'u'. So the output is "lul". |
49 |
| -
|
50 |
| - Example 2 |
51 |
| -
|
52 |
| - Input 1: a maze represented by a 2D array |
53 |
| -
|
54 |
| - 0 0 0 0 0 |
55 |
| - 1 1 0 0 1 |
56 |
| - 0 0 0 0 0 |
57 |
| - 0 1 0 0 1 |
58 |
| - 0 1 0 0 0 |
59 |
| -
|
60 |
| - Input 2: ball coordinate (rowBall, colBall) = (4, 3) |
61 |
| - Input 3: hole coordinate (rowHole, colHole) = (3, 0) |
62 |
| - Output: "impossible" |
63 |
| - Explanation: The ball cannot reach the hole. |
64 |
| -
|
65 |
| - Note: |
66 |
| - There is only one ball and one hole in the maze. |
67 |
| - Both the ball and hole exist on an empty space, and they will not be at the same position initially. |
68 |
| - 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. |
69 |
| - The maze contains at least 2 empty spaces, and the width and the height of the maze won't exceed 30.*/ |
70 | 6 | public class _499 {
|
71 | 7 | public static class Solutoin1 {
|
72 |
| - /**credit: https://discuss.leetcode.com/topic/77474/similar-to-the-maze-ii-easy-understanding-java-bfs-solution*/ |
| 8 | + /** |
| 9 | + * credit: https://discuss.leetcode.com/topic/77474/similar-to-the-maze-ii-easy-understanding-java-bfs-solution |
| 10 | + */ |
73 | 11 |
|
74 | 12 | public String findShortestWay(int[][] maze, int[] ball, int[] hole) {
|
75 | 13 |
|
|
0 commit comments