diff --git a/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java b/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java index 8247172e7ee0..aaefc8a28735 100644 --- a/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java +++ b/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java @@ -6,7 +6,8 @@ * ends are marked with different integers. * * The goal is to find a path from a starting position to the target position - * (map[6][5]) while navigating through the maze. + * (the second-to-last row and column of the map) while navigating through + * the maze. */ public final class MazeRecursion { @@ -21,6 +22,9 @@ private MazeRecursion() { * @return The solved maze with paths marked, or null if no solution exists. */ public static int[][] solveMazeUsingFirstStrategy(int[][] map) { + if (map == null || map.length < 3 || map[0].length < 3) { + return null; + } if (setWay(map, 1, 1)) { return map; } @@ -35,6 +39,9 @@ public static int[][] solveMazeUsingFirstStrategy(int[][] map) { * @return The solved maze with paths marked, or null if no solution exists. */ public static int[][] solveMazeUsingSecondStrategy(int[][] map) { + if (map == null || map.length < 3 || map[0].length < 3) { + return null; + } if (setWay2(map, 1, 1)) { return map; } @@ -48,10 +55,10 @@ public static int[][] solveMazeUsingSecondStrategy(int[][] map) { * @param map The 2D array representing the maze (walls, paths, etc.) * @param i The current x-coordinate of the ball (row index) * @param j The current y-coordinate of the ball (column index) - * @return True if a path is found to (6,5), otherwise false + * @return True if a path is found to the target position, otherwise false */ private static boolean setWay(int[][] map, int i, int j) { - if (map[6][5] == 2) { + if (map[map.length - 2][map[0].length - 2] == 2) { return true; } @@ -85,15 +92,16 @@ else if (setWay(map, i, j - 1)) { /** * Attempts to find a path through the maze using an alternative movement - * strategy "up -> right -> down -> left". + * strategy "up -> right -> down -> left". The path is marked with '2' for + * valid paths and '3' for dead ends. * * @param map The 2D array representing the maze (walls, paths, etc.) * @param i The current x-coordinate of the ball (row index) * @param j The current y-coordinate of the ball (column index) - * @return True if a path is found to (6,5), otherwise false + * @return True if a path is found to the target position, otherwise false */ private static boolean setWay2(int[][] map, int i, int j) { - if (map[6][5] == 2) { + if (map[map.length - 2][map[0].length - 2] == 2) { return true; } diff --git a/src/test/java/com/thealgorithms/backtracking/MazeRecursionTest.java b/src/test/java/com/thealgorithms/backtracking/MazeRecursionTest.java index b8e77fb38bad..5fe27d4f4c06 100644 --- a/src/test/java/com/thealgorithms/backtracking/MazeRecursionTest.java +++ b/src/test/java/com/thealgorithms/backtracking/MazeRecursionTest.java @@ -1,6 +1,7 @@ package com.thealgorithms.backtracking; import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertNull; import org.junit.jupiter.api.Test; @@ -64,4 +65,59 @@ public void testSolveMazeUsingFirstAndSecondStrategy() { assertArrayEquals(expectedMap1, solvedMap1); assertArrayEquals(expectedMap2, solvedMap2); } + + @Test + public void testSolveMazeWithSmallMaze() { + int[][] map = new int[][] { + {1, 1, 1, 1, 1}, + {1, 0, 0, 0, 1}, + {1, 0, 1, 0, 1}, + {1, 0, 0, 0, 1}, + {1, 1, 1, 1, 1}, + }; + int[][] map2 = new int[5][5]; + for (int i = 0; i < map.length; i++) { + System.arraycopy(map[i], 0, map2[i], 0, map[i].length); + } + + int[][] solvedMap1 = MazeRecursion.solveMazeUsingFirstStrategy(map); + int[][] solvedMap2 = MazeRecursion.solveMazeUsingSecondStrategy(map2); + + int[][] expectedMap1 = new int[][] { + {1, 1, 1, 1, 1}, + {1, 2, 0, 0, 1}, + {1, 2, 1, 0, 1}, + {1, 2, 2, 2, 1}, + {1, 1, 1, 1, 1}, + }; + int[][] expectedMap2 = new int[][] { + {1, 1, 1, 1, 1}, + {1, 2, 2, 2, 1}, + {1, 0, 1, 2, 1}, + {1, 0, 0, 2, 1}, + {1, 1, 1, 1, 1}, + }; + + assertArrayEquals(expectedMap1, solvedMap1); + assertArrayEquals(expectedMap2, solvedMap2); + } + + @Test + public void testSolveMazeWithNoSolution() { + // The target cell is walled off, so no path exists + int[][] map = new int[][] { + {1, 1, 1, 1, 1}, + {1, 0, 1, 0, 1}, + {1, 0, 1, 0, 1}, + {1, 0, 1, 0, 1}, + {1, 1, 1, 1, 1}, + }; + int[][] map2 = new int[5][5]; + for (int i = 0; i < map.length; i++) { + System.arraycopy(map[i], 0, map2[i], 0, map[i].length); + } + + assertNull(MazeRecursion.solveMazeUsingFirstStrategy(map)); + assertNull(MazeRecursion.solveMazeUsingSecondStrategy(map2)); + } }