Skip to content

Commit 2def68a

Browse files
[LEET-499] add 499
1 parent b279ba8 commit 2def68a

File tree

3 files changed

+170
-0
lines changed

3 files changed

+170
-0
lines changed

leetcode-algorithms/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
|504|[Base 7](https://leetcode.com/problems/base-7/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/Base7.java) | O(1) |O(1) | Easy|
1212
|501|[Find Mode in Binary Tree](https://leetcode.com/problems/find-mode-in-binary-tree/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/FindModeinBinaryTree.java) | O(n) |O(k) | Easy| Binary Tree
1313
|500|[Keyboard Row](https://leetcode.com/problems/keyboard-row/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/KeyboardRow.java) | O(n) |O(1) | Easy|
14+
|499|[The Maze III](https://leetcode.com/problems/the-maze-iii/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/TheMazeIII.java) | O(m*n) |O(m*n) | Hard| BFS
1415
|494|[Target Sum](https://leetcode.com/problems/target-sum/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/TargetSum.java) | O(2^n) |O(1) | Medium|
1516
|492|[Construct the Rectangle](https://leetcode.com/problems/construct-the-rectangle/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ConstructTheRectangle.java) | O(n) |O(1) | Easy| Array
1617
|490|[The Maze](https://leetcode.com/problems/the-maze/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/TheMaze.java) | O(m*n) |O(m*n) | Medium| BFS
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.stevesun.solutions;
2+
3+
import java.util.PriorityQueue;
4+
import java.util.Queue;
5+
6+
/**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.
7+
8+
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".
9+
10+
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.
11+
12+
Example 1
13+
14+
Input 1: a maze represented by a 2D array
15+
16+
0 0 0 0 0
17+
1 1 0 0 1
18+
0 0 0 0 0
19+
0 1 0 0 1
20+
0 1 0 0 0
21+
22+
Input 2: ball coordinate (rowBall, colBall) = (4, 3)
23+
Input 3: hole coordinate (rowHole, colHole) = (0, 1)
24+
25+
Output: "lul"
26+
Explanation: There are two shortest ways for the ball to drop into the hole.
27+
The first way is left -> up -> left, represented by "lul".
28+
The second way is up -> left, represented by 'ul'.
29+
Both ways have shortest distance 6, but the first way is lexicographically smaller because 'l' < 'u'. So the output is "lul".
30+
31+
Example 2
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) = (3, 0)
43+
Output: "impossible"
44+
Explanation: The ball cannot reach the hole.
45+
46+
Note:
47+
There is only one ball and one hole in the maze.
48+
Both the ball and hole exist on an empty space, and they will not be at the same position initially.
49+
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.
50+
The maze contains at least 2 empty spaces, and the width and the height of the maze won't exceed 30.*/
51+
public class TheMazeIII {
52+
//credit: https://discuss.leetcode.com/topic/77474/similar-to-the-maze-ii-easy-understanding-java-bfs-solution
53+
54+
public String findShortestWay(int[][] maze, int[] ball, int[] hole) {
55+
56+
final int[] directions = new int[]{-1, 0, 1, 0, -1};
57+
Queue<Point> heap = new PriorityQueue<>();
58+
heap.offer(new Point(ball[0], ball[1], 0, ""));
59+
int m = maze.length, n = maze[0].length;
60+
Point[][] points = new Point[m][n];
61+
for (int i = 0; i < m*n; i++) points[i/n][i%n] = new Point(i/n, i%n);//initialize the length array
62+
String[] ds = new String[]{"u", "r", "d", "l"};
63+
while (!heap.isEmpty()) {
64+
Point curr = heap.poll();
65+
if (points[curr.x][curr.y].compareTo(curr) <= 0) continue;//if we have already found a shorter route
66+
points[curr.x][curr.y] = curr;
67+
for (int i = 0; i < directions.length - 1; i++) {
68+
int x = curr.x, y = curr.y, distance = curr.distance;//use temp variables to move
69+
//we need below while loop to find only "stop" points that could be put into the queue
70+
while (x >= 0 && y >= 0 && x < m && y < n && maze[x][y] == 0 && (x != hole[0] || y != hole[1])) {
71+
x += directions[i];
72+
y += directions[i + 1];
73+
distance++;
74+
}
75+
if (x != hole[0] || y != hole[1]) {
76+
x -= directions[i];
77+
y -= directions[i + 1];
78+
distance--;
79+
}
80+
heap.offer(new Point(x, y, distance, curr.path+ds[i]));
81+
}
82+
}
83+
return points[hole[0]][hole[1]].distance == Integer.MAX_VALUE ? "impossible" : points[hole[0]][hole[1]].path;
84+
}
85+
86+
class Point implements Comparable<Point>{
87+
int x;
88+
int y;
89+
int distance;
90+
String path;
91+
92+
public Point(int x, int y) {
93+
this.x = x;
94+
this.y = y;
95+
this.distance = Integer.MAX_VALUE;
96+
this.path = "";
97+
}
98+
99+
public Point(int x, int y, int distance, String path) {
100+
this.x = x;
101+
this.y = y;
102+
this.distance = distance;
103+
this.path = path;
104+
}
105+
106+
@Override
107+
public int compareTo(Point o) {
108+
return (this.distance == o.distance) ? this.path.compareTo(o.path) : this.distance - o.distance;
109+
}
110+
}
111+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.stevesun;
2+
3+
import com.stevesun.solutions.TheMazeIII;
4+
import org.junit.Before;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static junit.framework.Assert.assertEquals;
9+
10+
public class TheMazeIIITest {
11+
private static TheMazeIII test;
12+
private static String expected;
13+
private static String actual;
14+
private static int[][] maze;
15+
private static int[] ball;
16+
private static int[] hole;
17+
18+
@BeforeClass
19+
public static void setup(){
20+
test = new TheMazeIII();
21+
}
22+
23+
@Before
24+
public void setupForEachTest(){}
25+
26+
@Test
27+
public void test1(){
28+
maze = new int[][]{
29+
{0,0,0,0,0},
30+
{1,1,0,0,1},
31+
{0,0,0,0,0},
32+
{0,1,0,0,1},
33+
{0,1,0,0,0}
34+
};
35+
ball = new int[]{4,3};
36+
hole = new int[]{0,1};
37+
actual = test.findShortestWay(maze, ball, hole);
38+
expected = "lul";
39+
assertEquals(expected, actual);
40+
41+
}
42+
43+
@Test
44+
public void test2(){
45+
maze = new int[][]{
46+
{0,0,1,0,0},
47+
{0,0,0,0,0},
48+
{0,0,0,1,0},
49+
{1,1,0,1,1},
50+
{0,0,0,0,0}
51+
};
52+
ball = new int[]{0,4};
53+
hole = new int[]{4,4};
54+
actual = test.findShortestWay(maze, ball, hole);
55+
expected = "ldldrdr";
56+
assertEquals(expected, actual);
57+
}
58+
}

0 commit comments

Comments
 (0)