Skip to content

Commit 779429c

Browse files
committed
Modify MazeRecursion
1 parent d5c12f9 commit 779429c

File tree

2 files changed

+72
-146
lines changed

2 files changed

+72
-146
lines changed

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

Lines changed: 42 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
/**
44
* This class contains methods to solve a maze using recursive backtracking.
55
* The maze is represented as a 2D array where walls, paths, and visited/dead
6-
* ends
7-
* are marked with different integers.
6+
* ends are marked with different integers.
87
*
98
* The goal is to find a path from a starting position to the target position
109
* (map[6][5]) while navigating through the maze.
@@ -15,163 +14,96 @@ private MazeRecursion() {
1514
}
1615

1716
/**
18-
* This method sets up a maze as a 2D array, where '1' represents walls and '0'
19-
* represents open paths. It then calls recursive functions to find paths using
20-
* two different movement strategies. The results are printed to the console.
17+
* This method solves the maze using the "down -> right -> up -> left"
18+
* movement strategy.
19+
*
20+
* @param map The 2D array representing the maze (walls, paths, etc.)
21+
* @return The solved maze with paths marked, or null if no solution exists.
2122
*/
22-
public static void mazeRecursion() {
23-
24-
int[][] map = new int[8][7]; // Create an 8x7 maze map (2D array)
25-
int[][] map2 = new int[8][7]; // Copy of the maze for an alternative pathfinding method
26-
27-
// Initialize the maze with boundaries (set walls as '1')
28-
// Set the top and bottom rows as walls
29-
for (int i = 0; i < 7; i++) {
30-
map[0][i] = 1;
31-
map[7][i] = 1;
32-
}
33-
34-
// Set the left and right columns as walls
35-
for (int i = 0; i < 8; i++) {
36-
map[i][0] = 1;
37-
map[i][6] = 1;
38-
}
39-
40-
// Place internal obstacles in the maze
41-
map[3][1] = 1; // Wall block at position (3,1)
42-
map[3][2] = 1; // Wall block at position (3,2)
43-
44-
System.out.println("Initial maze layout:");
45-
for (int i = 0; i < 8; i++) {
46-
for (int j = 0; j < 7; j++) {
47-
System.out.print(map[i][j] + " ");
48-
}
49-
System.out.println();
50-
}
51-
52-
// Clone the maze into map2 for the second pathfinding method
53-
for (int i = 0; i < map.length; i++) {
54-
System.arraycopy(map[i], 0, map2[i], 0, map[i].length);
55-
}
56-
57-
// Use the first pathfinding method with a "down -> right -> up -> left" strategy
58-
setWay(map, 1, 1);
59-
60-
// Use the second pathfinding method with a "up -> right -> down -> left" strategy
61-
setWay2(map2, 1, 1);
62-
63-
// Print the maze after pathfinding using the first method
64-
System.out.println("Maze after pathfinding using first strategy:");
65-
for (int i = 0; i < 8; i++) {
66-
for (int j = 0; j < 7; j++) {
67-
System.out.print(map[i][j] + " ");
68-
}
69-
System.out.println();
23+
public static int[][] solveMazeUsingFirstStrategy(int[][] map) {
24+
if (setWay(map, 1, 1)) {
25+
return map;
7026
}
27+
return null;
28+
}
7129

72-
// Print the maze after pathfinding using the second method
73-
System.out.println("Maze after pathfinding using second strategy:");
74-
for (int i = 0; i < 8; i++) {
75-
for (int j = 0; j < 7; j++) {
76-
System.out.print(map2[i][j] + " ");
77-
}
78-
System.out.println();
30+
/**
31+
* This method solves the maze using the "up -> right -> down -> left"
32+
* movement strategy.
33+
*
34+
* @param map The 2D array representing the maze (walls, paths, etc.)
35+
* @return The solved maze with paths marked, or null if no solution exists.
36+
*/
37+
public static int[][] solveMazeUsingSecondStrategy(int[][] map) {
38+
if (setWay2(map, 1, 1)) {
39+
return map;
7940
}
41+
return null;
8042
}
8143

8244
/**
83-
* Attempts to find a path through the maze using a "down -> right -> up ->
84-
* left" movement strategy. The ball tries to reach position (6,5), and the path is
85-
* marked with '2' for valid paths and '3' for dead ends.
45+
* Attempts to find a path through the maze using a "down -> right -> up -> left"
46+
* movement strategy. The path is marked with '2' for valid paths and '3' for dead ends.
8647
*
8748
* @param map The 2D array representing the maze (walls, paths, etc.)
8849
* @param i The current x-coordinate of the ball (row index)
8950
* @param j The current y-coordinate of the ball (column index)
9051
* @return True if a path is found to (6,5), otherwise false
9152
*/
92-
public static boolean setWay(int[][] map, int i, int j) {
53+
private static boolean setWay(int[][] map, int i, int j) {
9354
if (map[6][5] == 2) {
9455
return true;
9556
}
9657

9758
// If the current position is unvisited (0), explore it
9859
if (map[i][j] == 0) {
99-
// Assume the path is feasible, mark the current position as '2'
60+
// Mark the current position as '2'
10061
map[i][j] = 2;
10162

10263
// Move down
103-
if (setWay(map, i + 1, j)) {
104-
return true;
105-
}
64+
if (setWay(map, i + 1, j)) return true;
10665
// Move right
107-
else if (setWay(map, i, j + 1)) {
108-
return true;
109-
}
66+
else if (setWay(map, i, j + 1)) return true;
11067
// Move up
111-
else if (setWay(map, i - 1, j)) {
112-
return true;
113-
}
68+
else if (setWay(map, i - 1, j)) return true;
11469
// Move left
115-
else if (setWay(map, i, j - 1)) {
116-
return true;
117-
} else {
118-
// Mark the current position as a dead end (3) and backtrack
119-
map[i][j] = 3;
120-
return false;
121-
}
122-
} else {
123-
// If the position is not unvisited (either a wall, dead end, or already part of
124-
// the path), return false
70+
else if (setWay(map, i, j - 1)) return true;
71+
72+
map[i][j] = 3; // Mark as dead end (3) if no direction worked
12573
return false;
12674
}
75+
return false;
12776
}
12877

12978
/**
13079
* Attempts to find a path through the maze using an alternative movement
13180
* strategy "up -> right -> down -> left".
132-
* This method explores a different order of
133-
* movement compared to setWay().
13481
*
13582
* @param map The 2D array representing the maze (walls, paths, etc.)
13683
* @param i The current x-coordinate of the ball (row index)
13784
* @param j The current y-coordinate of the ball (column index)
13885
* @return True if a path is found to (6,5), otherwise false
13986
*/
140-
public static boolean setWay2(int[][] map, int i, int j) {
141-
// Check if the ball has reached the target at map[6][5]
87+
private static boolean setWay2(int[][] map, int i, int j) {
14288
if (map[6][5] == 2) {
143-
return true; // Path found
89+
return true;
14490
}
14591

146-
// If the current position is unvisited (0), explore it
14792
if (map[i][j] == 0) {
148-
// Assume the path is feasible, mark the current position as '2'
14993
map[i][j] = 2;
15094

15195
// Move up
152-
if (setWay2(map, i - 1, j)) {
153-
return true;
154-
}
96+
if (setWay2(map, i - 1, j)) return true;
15597
// Move right
156-
else if (setWay2(map, i, j + 1)) {
157-
return true;
158-
}
98+
else if (setWay2(map, i, j + 1)) return true;
15999
// Move down
160-
else if (setWay2(map, i + 1, j)) {
161-
return true;
162-
}
100+
else if (setWay2(map, i + 1, j)) return true;
163101
// Move left
164-
else if (setWay2(map, i, j - 1)) {
165-
return true;
166-
} else {
167-
// Mark the current position as a dead end (3) and backtrack
168-
map[i][j] = 3;
169-
return false;
170-
}
171-
} else {
172-
// If the position is not unvisited (either a wall, dead end, or already part of
173-
// the path), return false
102+
else if (setWay2(map, i, j - 1)) return true;
103+
104+
map[i][j] = 3; // Mark as dead end (3) if no direction worked
174105
return false;
175106
}
107+
return false;
176108
}
177109
}

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

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,63 +11,57 @@
1111
public class MazeRecursionTest {
1212

1313
@Test
14-
public void testMaze() {
15-
// First create a 2 dimensions array to mimic a maze map
14+
public void testSolveMazeUsingFirstAndSecondStrategy() {
1615
int[][] map = new int[8][7];
1716
int[][] map2 = new int[8][7];
1817

19-
// We use 1 to indicate wall
18+
// We use 1 to indicate walls
2019
// Set the ceiling and floor to 1
2120
for (int i = 0; i < 7; i++) {
2221
map[0][i] = 1;
2322
map[7][i] = 1;
2423
}
25-
26-
// Then we set the left and right wall to 1
24+
// Set the left and right wall to 1
2725
for (int i = 0; i < 8; i++) {
2826
map[i][0] = 1;
2927
map[i][6] = 1;
3028
}
31-
32-
// Now we have created a maze with its wall initialized
33-
34-
// Here we set the obstacle
29+
// Set obstacles
3530
map[3][1] = 1;
3631
map[3][2] = 1;
3732

38-
// clone another map for setWay2 method
33+
// Clone the original map for the second pathfinding strategy
3934
for (int i = 0; i < map.length; i++) {
40-
for (int j = 0; j < map[i].length; j++) {
41-
map2[i][j] = map[i][j];
42-
}
35+
System.arraycopy(map[i], 0, map2[i], 0, map[i].length);
4336
}
4437

45-
MazeRecursion.setWay(map, 1, 1);
46-
MazeRecursion.setWay2(map2, 1, 1);
47-
48-
int[][] expectedMap = new int[][] {
49-
{1, 1, 1, 1, 1, 1, 1},
50-
{1, 2, 0, 0, 0, 0, 1},
51-
{1, 2, 2, 2, 0, 0, 1},
52-
{1, 1, 1, 2, 0, 0, 1},
53-
{1, 0, 0, 2, 0, 0, 1},
54-
{1, 0, 0, 2, 0, 0, 1},
55-
{1, 0, 0, 2, 2, 2, 1},
56-
{1, 1, 1, 1, 1, 1, 1},
38+
// Solve the maze using the first strategy
39+
int[][] solvedMap1 = MazeRecursion.solveMazeUsingFirstStrategy(map);
40+
// Solve the maze using the second strategy
41+
int[][] solvedMap2 = MazeRecursion.solveMazeUsingSecondStrategy(map2);
42+
int[][] expectedMap1 = new int[][] {
43+
{1, 1, 1, 1, 1, 1, 1},
44+
{1, 2, 0, 0, 0, 0, 1},
45+
{1, 2, 2, 2, 0, 0, 1},
46+
{1, 1, 1, 2, 0, 0, 1},
47+
{1, 0, 0, 2, 0, 0, 1},
48+
{1, 0, 0, 2, 0, 0, 1},
49+
{1, 0, 0, 2, 2, 2, 1},
50+
{1, 1, 1, 1, 1, 1, 1},
5751
};
58-
5952
int[][] expectedMap2 = new int[][] {
60-
{1, 1, 1, 1, 1, 1, 1},
61-
{1, 2, 2, 2, 2, 2, 1},
62-
{1, 0, 0, 0, 0, 2, 1},
63-
{1, 1, 1, 0, 0, 2, 1},
64-
{1, 0, 0, 0, 0, 2, 1},
65-
{1, 0, 0, 0, 0, 2, 1},
66-
{1, 0, 0, 0, 0, 2, 1},
67-
{1, 1, 1, 1, 1, 1, 1},
53+
{1, 1, 1, 1, 1, 1, 1},
54+
{1, 2, 2, 2, 2, 2, 1},
55+
{1, 0, 0, 0, 0, 2, 1},
56+
{1, 1, 1, 0, 0, 2, 1},
57+
{1, 0, 0, 0, 0, 2, 1},
58+
{1, 0, 0, 0, 0, 2, 1},
59+
{1, 0, 0, 0, 0, 2, 1},
60+
{1, 1, 1, 1, 1, 1, 1},
6861
};
6962

70-
assertArrayEquals(map, expectedMap);
71-
assertArrayEquals(map2, expectedMap2);
63+
// Assert the results
64+
assertArrayEquals(expectedMap1, solvedMap1);
65+
assertArrayEquals(expectedMap2, solvedMap2);
7266
}
7367
}

0 commit comments

Comments
 (0)