-
Notifications
You must be signed in to change notification settings - Fork 19.7k
/
Copy pathMazeRecursionTest.java
67 lines (59 loc) · 2.01 KB
/
MazeRecursionTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.thealgorithms.backtracking;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import org.junit.jupiter.api.Test;
/**
* @author onglipwei
* @create 2022-08-03 5:17 AM
*/
public class MazeRecursionTest {
@Test
public void testSolveMazeUsingFirstAndSecondStrategy() {
int[][] map = new int[8][7];
int[][] map2 = new int[8][7];
// We use 1 to indicate walls
// Set the ceiling and floor to 1
for (int i = 0; i < 7; i++) {
map[0][i] = 1;
map[7][i] = 1;
}
// Set the left and right wall to 1
for (int i = 0; i < 8; i++) {
map[i][0] = 1;
map[i][6] = 1;
}
// Set obstacles
map[3][1] = 1;
map[3][2] = 1;
// Clone the original map for the second pathfinding strategy
for (int i = 0; i < map.length; i++) {
System.arraycopy(map[i], 0, map2[i], 0, map[i].length);
}
// Solve the maze using the first strategy
int[][] solvedMap1 = MazeRecursion.solveMazeUsingFirstStrategy(map);
// Solve the maze using the second strategy
int[][] solvedMap2 = MazeRecursion.solveMazeUsingSecondStrategy(map2);
int[][] expectedMap1 = new int[][] {
{1, 1, 1, 1, 1, 1, 1},
{1, 2, 0, 0, 0, 0, 1},
{1, 2, 2, 2, 0, 0, 1},
{1, 1, 1, 2, 0, 0, 1},
{1, 0, 0, 2, 0, 0, 1},
{1, 0, 0, 2, 0, 0, 1},
{1, 0, 0, 2, 2, 2, 1},
{1, 1, 1, 1, 1, 1, 1},
};
int[][] expectedMap2 = new int[][] {
{1, 1, 1, 1, 1, 1, 1},
{1, 2, 2, 2, 2, 2, 1},
{1, 0, 0, 0, 0, 2, 1},
{1, 1, 1, 0, 0, 2, 1},
{1, 0, 0, 0, 0, 2, 1},
{1, 0, 0, 0, 0, 2, 1},
{1, 0, 0, 0, 0, 2, 1},
{1, 1, 1, 1, 1, 1, 1},
};
// Assert the results
assertArrayEquals(expectedMap1, solvedMap1);
assertArrayEquals(expectedMap2, solvedMap2);
}
}