Skip to content

Commit fa2b743

Browse files
### FIxes<Mazerecursion> (#7705)
* fix: handle bounds, validation and custom start/target in MazeRecursion * style: apply clang-format * test: fix formatting and use fresh maze per strategy * test: expect null for default target outside small maze * style: remove trailing whitespace in MazeRecursion * style: remove trailing whitespace
1 parent 599703c commit fa2b743

2 files changed

Lines changed: 74 additions & 19 deletions

File tree

‎src/main/java/com/thealgorithms/backtracking/MazeRecursion.java‎

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,19 @@
66
* ends are marked with different integers.
77
*/
88
public final class MazeRecursion {
9+
private static final int OPEN = 0;
10+
private static final int PATH = 2;
11+
private static final int DEAD_END = 3;
12+
13+
private static final int DEFAULT_START_ROW = 1;
14+
private static final int DEFAULT_START_COLUMN = 1;
15+
private static final int DEFAULT_GOAL_ROW = 6;
16+
private static final int DEFAULT_GOAL_COLUMN = 5;
17+
18+
// Each entry is {rowDelta, colDelta}
19+
// down -> right -> up -> left
920
private static final int[][] FIRST_STRATEGY_DIRECTIONS = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
21+
// up -> right -> down -> left
1022
private static final int[][] SECOND_STRATEGY_DIRECTIONS = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
1123

1224
private MazeRecursion() {
@@ -21,14 +33,7 @@ private MazeRecursion() {
2133
* @throws IllegalArgumentException if the maze is null, empty, or not rectangular
2234
*/
2335
public static int[][] solveMazeUsingFirstStrategy(int[][] map) {
24-
validateMaze(map);
25-
if (!isInBounds(map, 1, 1) || !isInBounds(map, 6, 5)) {
26-
return null;
27-
}
28-
if (map[6][5] == 2) {
29-
return map;
30-
}
31-
return solve(map, 1, 1, 6, 5, FIRST_STRATEGY_DIRECTIONS) ? map : null;
36+
return solveWithDefaultEndpoints(map, FIRST_STRATEGY_DIRECTIONS);
3237
}
3338

3439
/**
@@ -40,14 +45,7 @@ public static int[][] solveMazeUsingFirstStrategy(int[][] map) {
4045
* @throws IllegalArgumentException if the maze is null, empty, or not rectangular
4146
*/
4247
public static int[][] solveMazeUsingSecondStrategy(int[][] map) {
43-
validateMaze(map);
44-
if (!isInBounds(map, 1, 1) || !isInBounds(map, 6, 5)) {
45-
return null;
46-
}
47-
if (map[6][5] == 2) {
48-
return map;
49-
}
50-
return solve(map, 1, 1, 6, 5, SECOND_STRATEGY_DIRECTIONS) ? map : null;
48+
return solveWithDefaultEndpoints(map, SECOND_STRATEGY_DIRECTIONS);
5149
}
5250

5351
/**
@@ -84,12 +82,23 @@ public static int[][] solveMazeUsingSecondStrategy(int[][] map, int startRow, in
8482
return solve(map, startRow, startColumn, goalRow, goalColumn, SECOND_STRATEGY_DIRECTIONS) ? map : null;
8583
}
8684

85+
private static int[][] solveWithDefaultEndpoints(int[][] map, int[][] directions) {
86+
validateMaze(map);
87+
if (!isInBounds(map, DEFAULT_START_ROW, DEFAULT_START_COLUMN) || !isInBounds(map, DEFAULT_GOAL_ROW, DEFAULT_GOAL_COLUMN)) {
88+
return null;
89+
}
90+
if (map[DEFAULT_GOAL_ROW][DEFAULT_GOAL_COLUMN] == PATH) {
91+
return map;
92+
}
93+
return solve(map, DEFAULT_START_ROW, DEFAULT_START_COLUMN, DEFAULT_GOAL_ROW, DEFAULT_GOAL_COLUMN, directions) ? map : null;
94+
}
95+
8796
private static boolean solve(int[][] map, int row, int column, int goalRow, int goalColumn, int[][] directions) {
88-
if (!isInBounds(map, row, column) || map[row][column] != 0) {
97+
if (!isInBounds(map, row, column) || map[row][column] != OPEN) {
8998
return false;
9099
}
91100

92-
map[row][column] = 2;
101+
map[row][column] = PATH;
93102
if (row == goalRow && column == goalColumn) {
94103
return true;
95104
}
@@ -100,7 +109,7 @@ private static boolean solve(int[][] map, int row, int column, int goalRow, int
100109
}
101110
}
102111

103-
map[row][column] = 3;
112+
map[row][column] = DEAD_END;
104113
return false;
105114
}
106115

‎src/test/java/com/thealgorithms/backtracking/MazeRecursionTest.java‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,50 @@ public void testSolveMazeUsingFirstAndSecondStrategy() {
163163
assertArrayEquals(expectedMap1, solvedMap1);
164164
assertArrayEquals(expectedMap2, solvedMap2);
165165
}
166+
167+
@Test
168+
void testSmallMazeWithCustomStartAndTarget() {
169+
int[][] map = {{0, 0}, {0, 0}};
170+
int[][] result = MazeRecursion.solveMazeUsingFirstStrategy(map, 0, 0, 1, 1);
171+
assertArrayEquals(new int[][] {{2, 0}, {2, 2}}, result); // down -> right
172+
}
173+
174+
@Test
175+
void testSecondStrategyKeepsItsTraversalOrder() {
176+
int[][] map = {{0, 0}, {0, 0}};
177+
int[][] result = MazeRecursion.solveMazeUsingSecondStrategy(map, 0, 0, 1, 1);
178+
assertArrayEquals(new int[][] {{2, 2}, {0, 2}}, result); // right -> down
179+
}
180+
181+
@Test
182+
void testDefaultTargetOutsideSmallMazeReturnsNull() {
183+
assertNull(MazeRecursion.solveMazeUsingFirstStrategy(new int[][] {{0, 0}, {0, 0}}));
184+
assertNull(MazeRecursion.solveMazeUsingSecondStrategy(new int[][] {{0, 0}, {0, 0}}));
185+
}
186+
187+
@Test
188+
void testNullAndEmptyMaze() {
189+
assertThrows(IllegalArgumentException.class, () -> MazeRecursion.solveMazeUsingFirstStrategy(null));
190+
assertThrows(IllegalArgumentException.class, () -> MazeRecursion.solveMazeUsingSecondStrategy(new int[][] {}));
191+
assertThrows(IllegalArgumentException.class, () -> MazeRecursion.solveMazeUsingFirstStrategy(new int[][] {{}}));
192+
}
193+
194+
@Test
195+
void testJaggedMazeThrows() {
196+
assertThrows(IllegalArgumentException.class, () -> MazeRecursion.solveMazeUsingFirstStrategy(new int[][] {{0, 0}, {0}}, 0, 0, 0, 1));
197+
assertThrows(IllegalArgumentException.class, () -> MazeRecursion.solveMazeUsingSecondStrategy(new int[][] {{0}, {0, 0}}, 0, 0, 1, 1));
198+
}
199+
200+
@Test
201+
void testStartOrTargetOutOfBoundsThrows() {
202+
assertThrows(IllegalArgumentException.class, () -> MazeRecursion.solveMazeUsingFirstStrategy(new int[][] {{0, 0}, {0, 0}}, -1, 0, 1, 1));
203+
assertThrows(IllegalArgumentException.class, () -> MazeRecursion.solveMazeUsingSecondStrategy(new int[][] {{0, 0}, {0, 0}}, 0, 0, 2, 2));
204+
}
205+
206+
@Test
207+
void testUnsolvableMazeReturnsNull() {
208+
int[][] map = {{0, 1}, {1, 0}};
209+
assertNull(MazeRecursion.solveMazeUsingFirstStrategy(map, 0, 0, 1, 1));
210+
assertNull(MazeRecursion.solveMazeUsingSecondStrategy(map, 0, 0, 1, 1));
211+
}
166212
}

0 commit comments

Comments
 (0)