Skip to content

Commit fe94188

Browse files
Merge branch 'master' into fix/saddleback-search-bounds-and-null-safety
2 parents c2d1dda + 402d3b0 commit fe94188

29 files changed

Lines changed: 840 additions & 113 deletions

‎.github/workflows/build.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
steps:
1111
- uses: actions/checkout@v7
1212
- name: Set up JDK
13-
uses: actions/setup-java@v6.1.0
13+
uses: actions/setup-java@v6.2.0
1414
with:
1515
java-version: 21
1616
distribution: 'temurin'

‎.github/workflows/codeql.yml‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,21 @@ jobs:
2424
uses: actions/checkout@v7
2525

2626
- name: Set up JDK
27-
uses: actions/setup-java@v6.1.0
27+
uses: actions/setup-java@v6.2.0
2828
with:
2929
java-version: 21
3030
distribution: 'temurin'
3131

3232
- name: Initialize CodeQL
33-
uses: github/codeql-action/init@v4.39.1
33+
uses: github/codeql-action/init@v4.39.3
3434
with:
3535
languages: 'java-kotlin'
3636

3737
- name: Build
3838
run: mvn --batch-mode --update-snapshots verify
3939

4040
- name: Perform CodeQL Analysis
41-
uses: github/codeql-action/analyze@v4.39.1
41+
uses: github/codeql-action/analyze@v4.39.3
4242
with:
4343
category: "/language:java-kotlin"
4444

@@ -55,12 +55,12 @@ jobs:
5555
uses: actions/checkout@v7
5656

5757
- name: Initialize CodeQL
58-
uses: github/codeql-action/init@v4.39.1
58+
uses: github/codeql-action/init@v4.39.3
5959
with:
6060
languages: 'actions'
6161

6262
- name: Perform CodeQL Analysis
63-
uses: github/codeql-action/analyze@v4.39.1
63+
uses: github/codeql-action/analyze@v4.39.3
6464
with:
6565
category: "/language:actions"
6666
...

‎.github/workflows/infer.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
- uses: actions/checkout@v7
1919

2020
- name: Set up JDK
21-
uses: actions/setup-java@v6.1.0
21+
uses: actions/setup-java@v6.2.0
2222
with:
2323
java-version: 21
2424
distribution: 'temurin'

‎pom.xml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
<plugins>
6262
<plugin>
6363
<artifactId>maven-surefire-plugin</artifactId>
64-
<version>3.6.0</version>
64+
<version>3.6.1</version>
6565
<configuration>
6666
<forkNode implementation="org.apache.maven.plugin.surefire.extensions.SurefireForkNodeFactory"/>
6767
</configuration>
@@ -82,7 +82,7 @@
8282
<plugin>
8383
<groupId>org.jacoco</groupId>
8484
<artifactId>jacoco-maven-plugin</artifactId>
85-
<version>0.8.15</version>
85+
<version>0.8.16</version>
8686
<executions>
8787
<execution>
8888
<goals>
@@ -112,7 +112,7 @@
112112
<dependency>
113113
<groupId>com.puppycrawl.tools</groupId>
114114
<artifactId>checkstyle</artifactId>
115-
<version>14.3.0</version>
115+
<version>14.4.0</version>
116116
</dependency>
117117
</dependencies>
118118
</plugin>

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

Lines changed: 84 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -4,122 +4,128 @@
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
66
* ends are marked with different integers.
7-
*
8-
* The goal is to find a path from a starting position to the target position
9-
* (map[6][5]) while navigating through the maze.
107
*/
118
public final class MazeRecursion {
9+
private static final int[][] FIRST_STRATEGY_DIRECTIONS = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
10+
private static final int[][] SECOND_STRATEGY_DIRECTIONS = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
1211

1312
private MazeRecursion() {
1413
}
1514

1615
/**
17-
* This method solves the maze using the "down -> right -> up -> left"
18-
* movement strategy.
16+
* Solves the maze using the "down -> right -> up -> left" movement strategy,
17+
* starting at (1, 1) and targeting (6, 5).
1918
*
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.
19+
* @param map the 2D array representing the maze (walls, paths, etc.)
20+
* @return the solved maze with paths marked, or null if no solution exists or a fixed endpoint is outside the maze
21+
* @throws IllegalArgumentException if the maze is null, empty, or not rectangular
2222
*/
2323
public static int[][] solveMazeUsingFirstStrategy(int[][] map) {
24-
if (setWay(map, 1, 1)) {
24+
validateMaze(map);
25+
if (!isInBounds(map, 1, 1) || !isInBounds(map, 6, 5)) {
26+
return null;
27+
}
28+
if (map[6][5] == 2) {
2529
return map;
2630
}
27-
return null;
31+
return solve(map, 1, 1, 6, 5, FIRST_STRATEGY_DIRECTIONS) ? map : null;
2832
}
2933

3034
/**
31-
* This method solves the maze using the "up -> right -> down -> left"
32-
* movement strategy.
35+
* Solves the maze using the "up -> right -> down -> left" movement strategy,
36+
* starting at (1, 1) and targeting (6, 5).
3337
*
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.
38+
* @param map the 2D array representing the maze (walls, paths, etc.)
39+
* @return the solved maze with paths marked, or null if no solution exists or a fixed endpoint is outside the maze
40+
* @throws IllegalArgumentException if the maze is null, empty, or not rectangular
3641
*/
3742
public static int[][] solveMazeUsingSecondStrategy(int[][] map) {
38-
if (setWay2(map, 1, 1)) {
43+
validateMaze(map);
44+
if (!isInBounds(map, 1, 1) || !isInBounds(map, 6, 5)) {
45+
return null;
46+
}
47+
if (map[6][5] == 2) {
3948
return map;
4049
}
41-
return null;
50+
return solve(map, 1, 1, 6, 5, SECOND_STRATEGY_DIRECTIONS) ? map : null;
4251
}
4352

4453
/**
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.
54+
* Solves the maze using the "down -> right -> up -> left" movement strategy.
4755
*
48-
* @param map The 2D array representing the maze (walls, paths, etc.)
49-
* @param i The current x-coordinate of the ball (row index)
50-
* @param j The current y-coordinate of the ball (column index)
51-
* @return True if a path is found to (6,5), otherwise false
56+
* @param map the 2D array representing the maze (0 = open, non-zero = blocked)
57+
* @param startRow the starting row
58+
* @param startColumn the starting column
59+
* @param goalRow the goal row
60+
* @param goalColumn the goal column
61+
* @return the solved maze with paths marked, or null if no solution exists
62+
* @throws IllegalArgumentException if the maze is null, empty, not rectangular,
63+
* or a coordinate is outside the maze
5264
*/
53-
private static boolean setWay(int[][] map, int i, int j) {
54-
if (map[6][5] == 2) {
55-
return true;
65+
public static int[][] solveMazeUsingFirstStrategy(int[][] map, int startRow, int startColumn, int goalRow, int goalColumn) {
66+
validateCoordinates(map, startRow, startColumn, goalRow, goalColumn);
67+
return solve(map, startRow, startColumn, goalRow, goalColumn, FIRST_STRATEGY_DIRECTIONS) ? map : null;
68+
}
69+
70+
/**
71+
* Solves the maze using the "up -> right -> down -> left" movement strategy.
72+
*
73+
* @param map the 2D array representing the maze (0 = open, non-zero = blocked)
74+
* @param startRow the starting row
75+
* @param startColumn the starting column
76+
* @param goalRow the goal row
77+
* @param goalColumn the goal column
78+
* @return the solved maze with paths marked, or null if no solution exists
79+
* @throws IllegalArgumentException if the maze is null, empty, not rectangular,
80+
* or a coordinate is outside the maze
81+
*/
82+
public static int[][] solveMazeUsingSecondStrategy(int[][] map, int startRow, int startColumn, int goalRow, int goalColumn) {
83+
validateCoordinates(map, startRow, startColumn, goalRow, goalColumn);
84+
return solve(map, startRow, startColumn, goalRow, goalColumn, SECOND_STRATEGY_DIRECTIONS) ? map : null;
85+
}
86+
87+
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) {
89+
return false;
5690
}
5791

58-
// If the current position is unvisited (0), explore it
59-
if (map[i][j] == 0) {
60-
// Mark the current position as '2'
61-
map[i][j] = 2;
92+
map[row][column] = 2;
93+
if (row == goalRow && column == goalColumn) {
94+
return true;
95+
}
6296

63-
// Move down
64-
if (setWay(map, i + 1, j)) {
65-
return true;
66-
}
67-
// Move right
68-
else if (setWay(map, i, j + 1)) {
69-
return true;
70-
}
71-
// Move up
72-
else if (setWay(map, i - 1, j)) {
73-
return true;
74-
}
75-
// Move left
76-
else if (setWay(map, i, j - 1)) {
97+
for (int[] direction : directions) {
98+
if (solve(map, row + direction[0], column + direction[1], goalRow, goalColumn, directions)) {
7799
return true;
78100
}
79-
80-
map[i][j] = 3; // Mark as dead end (3) if no direction worked
81-
return false;
82101
}
102+
103+
map[row][column] = 3;
83104
return false;
84105
}
85106

86-
/**
87-
* Attempts to find a path through the maze using an alternative movement
88-
* strategy "up -> right -> down -> left".
89-
*
90-
* @param map The 2D array representing the maze (walls, paths, etc.)
91-
* @param i The current x-coordinate of the ball (row index)
92-
* @param j The current y-coordinate of the ball (column index)
93-
* @return True if a path is found to (6,5), otherwise false
94-
*/
95-
private static boolean setWay2(int[][] map, int i, int j) {
96-
if (map[6][5] == 2) {
97-
return true;
107+
private static int validateMaze(int[][] map) {
108+
if (map == null || map.length == 0 || map[0] == null || map[0].length == 0) {
109+
throw new IllegalArgumentException("Maze must not be null or empty.");
98110
}
99111

100-
if (map[i][j] == 0) {
101-
map[i][j] = 2;
102-
103-
// Move up
104-
if (setWay2(map, i - 1, j)) {
105-
return true;
106-
}
107-
// Move right
108-
else if (setWay2(map, i, j + 1)) {
109-
return true;
110-
}
111-
// Move down
112-
else if (setWay2(map, i + 1, j)) {
113-
return true;
114-
}
115-
// Move left
116-
else if (setWay2(map, i, j - 1)) {
117-
return true;
112+
int columnCount = map[0].length;
113+
for (int[] row : map) {
114+
if (row == null || row.length != columnCount) {
115+
throw new IllegalArgumentException("Maze must be a non-empty rectangular matrix.");
118116
}
117+
}
118+
return columnCount;
119+
}
119120

120-
map[i][j] = 3; // Mark as dead end (3) if no direction worked
121-
return false;
121+
private static void validateCoordinates(int[][] map, int startRow, int startColumn, int goalRow, int goalColumn) {
122+
int columnCount = validateMaze(map);
123+
if (startRow < 0 || startRow >= map.length || startColumn < 0 || startColumn >= columnCount || goalRow < 0 || goalRow >= map.length || goalColumn < 0 || goalColumn >= columnCount) {
124+
throw new IllegalArgumentException("Start and goal coordinates must be inside the maze.");
122125
}
123-
return false;
126+
}
127+
128+
private static boolean isInBounds(int[][] map, int row, int column) {
129+
return row >= 0 && row < map.length && column >= 0 && column < map[row].length;
124130
}
125131
}

0 commit comments

Comments
 (0)