|
4 | 4 | import java.util.Queue;
|
5 | 5 |
|
6 | 6 | /**
|
7 |
| - * 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. |
8 |
| -
|
9 |
| - 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. |
10 |
| -
|
11 |
| - 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. |
| 7 | + * 505. The Maze II |
| 8 | + * |
| 9 | + * 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, |
| 10 | + * but it won't stop rolling until hitting a wall. When the ball stops, it could choose the next direction. |
| 11 | + * 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. |
| 12 | + * 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. |
12 | 13 |
|
13 | 14 | Example 1
|
14 | 15 |
|
@@ -51,55 +52,59 @@ The given maze does not contain border (like the red rectangle in the example pi
|
51 | 52 | */
|
52 | 53 | public class _505 {
|
53 | 54 |
|
54 |
| - /**The difference between II and I of this problem: |
55 |
| - * the extra array is not boolean type any more, but int type, and it's recording the length of each point to start point.*/ |
56 |
| - public int shortestDistance(int[][] maze, int[] start, int[] destination) { |
57 |
| - |
58 |
| - final int[] directions = new int[]{-1, 0, 1, 0, -1}; |
59 |
| - Queue<Point> queue = new LinkedList<>(); |
60 |
| - queue.offer(new Point(start[0], start[1], 0)); |
61 |
| - int m = maze.length; |
62 |
| - int n = maze[0].length; |
63 |
| - int[][] length = new int[m][n]; |
64 |
| - for (int i = 0; i < m * n; i++) { |
65 |
| - length[i / n][i % n] = Integer.MAX_VALUE;//initialize the length array |
66 |
| - } |
67 |
| - |
68 |
| - while (!queue.isEmpty()) { |
69 |
| - Point curr = queue.poll(); |
70 |
| - if (length[curr.x][curr.y] <= curr.distance) { |
71 |
| - continue; |
| 55 | + public static class Solution1 { |
| 56 | + /** |
| 57 | + * The difference between II and I of this problem: |
| 58 | + * the extra array is not boolean type any more, but int type, and it's recording the length of each point to start point. |
| 59 | + */ |
| 60 | + public int shortestDistance(int[][] maze, int[] start, int[] destination) { |
| 61 | + |
| 62 | + final int[] directions = new int[]{-1, 0, 1, 0, -1}; |
| 63 | + Queue<Point> queue = new LinkedList<>(); |
| 64 | + queue.offer(new Point(start[0], start[1], 0)); |
| 65 | + int m = maze.length; |
| 66 | + int n = maze[0].length; |
| 67 | + int[][] length = new int[m][n]; |
| 68 | + for (int i = 0; i < m * n; i++) { |
| 69 | + length[i / n][i % n] = Integer.MAX_VALUE;//initialize the length array |
72 | 70 | }
|
73 |
| - length[curr.x][curr.y] = curr.distance; |
74 |
| - for (int i = 0; i < directions.length - 1; i++) { |
75 |
| - int x = curr.x; |
76 |
| - int y = curr.y; |
77 |
| - int distance = curr.distance;//use temp variables to move |
78 |
| - //we need below while loop to find only "stop" points that could be put into the queue |
79 |
| - while (x >= 0 && y >= 0 && x < m && y < n && maze[x][y] == 0) { |
80 |
| - x += directions[i]; |
81 |
| - y += directions[i + 1]; |
82 |
| - distance++; |
| 71 | + |
| 72 | + while (!queue.isEmpty()) { |
| 73 | + Point curr = queue.poll(); |
| 74 | + if (length[curr.x][curr.y] <= curr.distance) { |
| 75 | + continue; |
| 76 | + } |
| 77 | + length[curr.x][curr.y] = curr.distance; |
| 78 | + for (int i = 0; i < directions.length - 1; i++) { |
| 79 | + int x = curr.x; |
| 80 | + int y = curr.y; |
| 81 | + int distance = curr.distance;//use temp variables to move |
| 82 | + //we need below while loop to find only "stop" points that could be put into the queue |
| 83 | + while (x >= 0 && y >= 0 && x < m && y < n && maze[x][y] == 0) { |
| 84 | + x += directions[i]; |
| 85 | + y += directions[i + 1]; |
| 86 | + distance++; |
| 87 | + } |
| 88 | + x -= directions[i]; |
| 89 | + y -= directions[i + 1]; |
| 90 | + distance--; |
| 91 | + queue.offer(new Point(x, y, distance)); |
83 | 92 | }
|
84 |
| - x -= directions[i]; |
85 |
| - y -= directions[i + 1]; |
86 |
| - distance--; |
87 |
| - queue.offer(new Point(x, y, distance)); |
88 | 93 | }
|
89 |
| - } |
90 |
| - return length[destination[0]][destination[1]] == Integer.MAX_VALUE ? -1 : length[destination[0]][destination[1]]; |
| 94 | + return length[destination[0]][destination[1]] == Integer.MAX_VALUE ? -1 : length[destination[0]][destination[1]]; |
91 | 95 |
|
92 |
| - } |
| 96 | + } |
93 | 97 |
|
94 |
| - class Point { |
95 |
| - int x; |
96 |
| - int y; |
97 |
| - int distance; |
| 98 | + class Point { |
| 99 | + int x; |
| 100 | + int y; |
| 101 | + int distance; |
98 | 102 |
|
99 |
| - public Point(int x, int y, int distance) { |
100 |
| - this.x = x; |
101 |
| - this.y = y; |
102 |
| - this.distance = distance; |
| 103 | + public Point(int x, int y, int distance) { |
| 104 | + this.x = x; |
| 105 | + this.y = y; |
| 106 | + this.distance = distance; |
| 107 | + } |
103 | 108 | }
|
104 | 109 | }
|
105 | 110 | }
|
0 commit comments