Skip to content

Commit 55488a3

Browse files
refactor 505
1 parent 57bb623 commit 55488a3

File tree

2 files changed

+57
-52
lines changed

2 files changed

+57
-52
lines changed

src/main/java/com/fishercoder/solutions/_505.java

+53-48
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
import java.util.Queue;
55

66
/**
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.
1213
1314
Example 1
1415
@@ -51,55 +52,59 @@ The given maze does not contain border (like the red rectangle in the example pi
5152
*/
5253
public class _505 {
5354

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
7270
}
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));
8392
}
84-
x -= directions[i];
85-
y -= directions[i + 1];
86-
distance--;
87-
queue.offer(new Point(x, y, distance));
8893
}
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]];
9195

92-
}
96+
}
9397

94-
class Point {
95-
int x;
96-
int y;
97-
int distance;
98+
class Point {
99+
int x;
100+
int y;
101+
int distance;
98102

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+
}
103108
}
104109
}
105110
}

src/test/java/com/fishercoder/_505Test.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import static junit.framework.Assert.assertEquals;
99

1010
public class _505Test {
11-
private static _505 test;
11+
private static _505.Solution1 solution1;
1212
private static int expected;
1313
private static int actual;
1414
private static int[][] maze;
@@ -17,7 +17,7 @@ public class _505Test {
1717

1818
@BeforeClass
1919
public static void setup() {
20-
test = new _505();
20+
solution1 = new _505.Solution1();
2121
}
2222

2323
@Before
@@ -35,7 +35,7 @@ public void test1() {
3535
};
3636
start = new int[]{4, 3};
3737
destination = new int[]{0, 1};
38-
actual = test.shortestDistance(maze, start, destination);
38+
actual = solution1.shortestDistance(maze, start, destination);
3939
expected = -1;
4040
assertEquals(expected, actual);
4141

@@ -52,7 +52,7 @@ public void test2() {
5252
};
5353
start = new int[]{0, 4};
5454
destination = new int[]{4, 4};
55-
actual = test.shortestDistance(maze, start, destination);
55+
actual = solution1.shortestDistance(maze, start, destination);
5656
expected = 12;
5757
assertEquals(expected, actual);
5858
}

0 commit comments

Comments
 (0)