Skip to content

Commit ea34cab

Browse files
style: apply clang-format fixes and add newline at end of files
1 parent e44f6c4 commit ea34cab

2 files changed

Lines changed: 16 additions & 63 deletions

File tree

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

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,7 @@ public static List<String> findPaths(final int[][] maze) {
7171
* @param visited tracks visited cells for the current path
7272
* @param results accumulates complete paths
7373
*/
74-
private static void solve(
75-
final int[][] maze,
76-
final int row,
77-
final int col,
78-
final int n,
79-
final String path,
80-
final boolean[][] visited,
81-
final List<String> results) {
82-
74+
private static void solve(final int[][] maze, final int row, final int col, final int n, final String path, final boolean[][] visited, final List<String> results) {
8375
// Base case: reached destination
8476
if (row == n - 1 && col == n - 1) {
8577
results.add(path);
@@ -121,17 +113,7 @@ private static void solve(
121113
* @param visited tracks visited cells for the current path
122114
* @return {@code true} if the cell is within bounds, open, and not yet visited
123115
*/
124-
private static boolean isSafe(
125-
final int[][] maze,
126-
final int row,
127-
final int col,
128-
final int n,
129-
final boolean[][] visited) {
130-
return row >= 0
131-
&& row < n
132-
&& col >= 0
133-
&& col < n
134-
&& maze[row][col] == 1
135-
&& !visited[row][col];
116+
private static boolean isSafe(final int[][] maze, final int row, final int col, final int n, final boolean[][] visited) {
117+
return row >= 0 && row < n && col >= 0 && col < n && maze[row][col] == 1 && !visited[row][col];
136118
}
137-
}
119+
}

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

Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,9 @@ class RatInAMazeTest {
1111

1212
@Test
1313
void testMultiplePathsExist() {
14-
// This maze has exactly 2 paths: DDRDRR and DRDDRR
15-
int[][] maze = {
16-
{1, 0, 0, 0},
17-
{1, 1, 0, 1},
18-
{0, 1, 0, 0},
19-
{0, 1, 1, 1}
20-
};
14+
int[][] maze = {{1, 0, 0, 0}, {1, 1, 0, 1}, {0, 1, 0, 0}, {0, 1, 1, 1}};
15+
2116
List<String> paths = RatInAMaze.findPaths(maze);
22-
// Verify at least one valid path exists and all paths are valid
2317
assertTrue(paths.size() >= 1);
2418
for (String path : paths) {
2519
assertTrue(path.chars().allMatch(c -> "DLRU".indexOf(c) >= 0));
@@ -28,43 +22,29 @@ void testMultiplePathsExist() {
2822

2923
@Test
3024
void testSinglePath() {
31-
int[][] maze = {
32-
{1, 0, 0},
33-
{1, 1, 0},
34-
{0, 1, 1}
35-
};
25+
int[][] maze = {{1, 0, 0}, {1, 1, 0}, {0, 1, 1}};
3626
List<String> paths = RatInAMaze.findPaths(maze);
3727
assertEquals(1, paths.size());
3828
assertEquals("DRDR", paths.get(0));
3929
}
4030

4131
@Test
4232
void testNoPathExists() {
43-
int[][] maze = {
44-
{1, 0, 0},
45-
{0, 0, 0},
46-
{0, 0, 1}
47-
};
33+
int[][] maze = {{1, 0, 0}, {0, 0, 0}, {0, 0, 1}};
4834
List<String> paths = RatInAMaze.findPaths(maze);
4935
assertTrue(paths.isEmpty());
5036
}
5137

5238
@Test
5339
void testSourceBlocked() {
54-
int[][] maze = {
55-
{0, 1},
56-
{1, 1}
57-
};
40+
int[][] maze = {{0, 1}, {1, 1}};
5841
List<String> paths = RatInAMaze.findPaths(maze);
5942
assertTrue(paths.isEmpty());
6043
}
6144

6245
@Test
6346
void testDestinationBlocked() {
64-
int[][] maze = {
65-
{1, 1},
66-
{1, 0}
67-
};
47+
int[][] maze = {{1, 1}, {1, 0}};
6848
List<String> paths = RatInAMaze.findPaths(maze);
6949
assertTrue(paths.isEmpty());
7050
}
@@ -91,7 +71,7 @@ void testNullMazeThrowsException() {
9171

9272
@Test
9373
void testEmptyMazeThrowsException() {
94-
assertThrows(IllegalArgumentException.class, () -> RatInAMaze.findPaths(new int[][]{}));
74+
assertThrows(IllegalArgumentException.class, () -> RatInAMaze.findPaths(new int[][] {}));
9575
}
9676

9777
@Test
@@ -102,28 +82,19 @@ void testNonSquareMazeThrowsException() {
10282

10383
@Test
10484
void testAllCellsOpen() {
105-
int[][] maze = {
106-
{1, 1, 1},
107-
{1, 1, 1},
108-
{1, 1, 1}
109-
};
85+
int[][] maze = {{1, 1, 1}, {1, 1, 1}, {1, 1, 1}};
11086
List<String> paths = RatInAMaze.findPaths(maze);
11187
assertTrue(paths.size() > 1);
11288
}
11389

11490
@Test
11591
void testLargerMazeWithPath() {
116-
int[][] maze = {
117-
{1, 1, 1, 1},
118-
{0, 1, 0, 1},
119-
{0, 1, 0, 1},
120-
{0, 1, 1, 1}
121-
};
92+
int[][] maze = {{1, 1, 1, 1}, {0, 1, 0, 1}, {0, 1, 0, 1}, {0, 1, 1, 1}};
12293
List<String> paths = RatInAMaze.findPaths(maze);
12394
assertTrue(paths.size() >= 1);
12495
for (String path : paths) {
125-
assertTrue(path.chars().allMatch(c -> "DLRU".indexOf(c) >= 0),
126-
"Path contains invalid characters: " + path);
96+
assertTrue(path.chars().allMatch(c -> "DLRU".indexOf(c) >= 0), "Path contains invalid characters: " + path);
12797
}
98+
12899
}
129-
}
100+
}

0 commit comments

Comments
 (0)