From ce9b917d7ff9fa90f880079bc9052471d3f33c0b Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sat, 5 Oct 2024 10:40:35 +0530 Subject: [PATCH 1/7] Add class documentation, improve comments in `MazeRecursion.java` --- .../backtracking/MazeRecursion.java | 169 ++++++++++-------- 1 file changed, 97 insertions(+), 72 deletions(-) diff --git a/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java b/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java index f7eae01e449a..5e66915f7599 100644 --- a/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java +++ b/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java @@ -1,35 +1,47 @@ package com.thealgorithms.backtracking; +/** + * This class contains methods to solve a maze using recursive backtracking. + * The maze is represented as a 2D array where walls, paths, and visited/dead + * 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. + */ public final class MazeRecursion { + private MazeRecursion() { } + /** + * This method sets up a maze as a 2D array, where '1' represents walls and '0' + * represents open paths. It then calls recursive functions to find paths using + * two different movement strategies. The results are printed to the console. + */ public static void mazeRecursion() { - // First create a 2 dimensions array to mimic a maze map - int[][] map = new int[8][7]; - int[][] map2 = new int[8][7]; - // We use 1 to indicate wall - // Set the ceiling and floor to 1 + int[][] map = new int[8][7]; // Create an 8x7 maze map (2D array) + int[][] map2 = new int[8][7]; // Copy of the maze for an alternative pathfinding method + + // Initialize the maze with boundaries (set walls as '1') + // Set the top and bottom rows as walls for (int i = 0; i < 7; i++) { map[0][i] = 1; map[7][i] = 1; } - // Then we set the left and right wall to 1 + // Set the left and right columns as walls for (int i = 0; i < 8; i++) { map[i][0] = 1; map[i][6] = 1; } - // Now we have created a maze with its wall initialized - - // Here we set the obstacle - map[3][1] = 1; - map[3][2] = 1; + // Place internal obstacles in the maze + map[3][1] = 1; // Wall block at position (3,1) + map[3][2] = 1; // Wall block at position (3,2) - // Print the current map - System.out.println("The condition of the map: "); + System.out.println("Initial maze layout:"); for (int i = 0; i < 8; i++) { for (int j = 0; j < 7; j++) { System.out.print(map[i][j] + " "); @@ -37,21 +49,19 @@ public static void mazeRecursion() { System.out.println(); } - // clone another map for setWay2 method + // Clone the maze into map2 for the second pathfinding method for (int i = 0; i < map.length; i++) { System.arraycopy(map[i], 0, map2[i], 0, map[i].length); } - // By using recursive backtracking to let your ball(target) find its way in the - // maze - // The first parameter is the map - // Second parameter is x coordinate of your target - // Third parameter is the y coordinate of your target + // Use the first pathfinding method with a "down -> right -> up -> left" strategy setWay(map, 1, 1); + + // Use the second pathfinding method with a "up -> right -> down -> left" strategy setWay2(map2, 1, 1); - // Print out the new map1, with the ball footprint - System.out.println("After the ball goes through the map1,show the current map1 condition"); + // Print the maze after pathfinding using the first method + System.out.println("Maze after pathfinding using first strategy:"); for (int i = 0; i < 8; i++) { for (int j = 0; j < 7; j++) { System.out.print(map[i][j] + " "); @@ -59,8 +69,8 @@ public static void mazeRecursion() { System.out.println(); } - // Print out the new map2, with the ball footprint - System.out.println("After the ball goes through the map2,show the current map2 condition"); + // Print the maze after pathfinding using the second method + System.out.println("Maze after pathfinding using second strategy:"); for (int i = 0; i < 8; i++) { for (int j = 0; j < 7; j++) { System.out.print(map2[i][j] + " "); @@ -70,82 +80,97 @@ public static void mazeRecursion() { } /** - * Using recursive path finding to help the ball find its way in the maze - * Description: - * 1. map (means the maze) - * 2. i, j (means the initial coordinate of the ball in the maze) - * 3. if the ball can reach the end of maze, that is position of map[6][5], - * means the we have found a path for the ball - * 4. Additional Information: 0 in the map[i][j] means the ball has not gone - * through this position, 1 means the wall, 2 means the path is feasible, 3 - * means the ball has gone through the path but this path is dead end - * 5. We will need strategy for the ball to pass through the maze for example: - * Down -> Right -> Up -> Left, if the path doesn't work, then backtrack + * Attempts to find a path through the maze using a "down -> right -> up -> + * left" movement strategy. The ball tries to reach position (6,5), and the path is + * marked with '2' for valid paths and '3' for dead ends. * - * @author OngLipWei - * @version Jun 23, 2021 11:36:14 AM - * @param map The maze - * @param i x coordinate of your ball(target) - * @param j y coordinate of your ball(target) - * @return If we did find a path for the ball,return true,else false + * @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 */ public static boolean setWay(int[][] map, int i, int j) { - if (map[6][5] == 2) { // means the ball find its path, ending condition + if (map[6][5] == 2) { return true; } - if (map[i][j] == 0) { // if the ball haven't gone through this point - // then the ball follows the move strategy : down -> right -> up -> left - map[i][j] = 2; // we assume that this path is feasible first, set the current point to 2 - // first。 - if (setWay(map, i + 1, j)) { // go down + + // If the current position is unvisited (0), explore it + if (map[i][j] == 0) { + // Assume the path is feasible, mark the current position as '2' + map[i][j] = 2; + + // Try moving down + if (setWay(map, i + 1, j)) { return true; - } else if (setWay(map, i, j + 1)) { // go right + } + // Try moving right + else if (setWay(map, i, j + 1)) { return true; - } else if (setWay(map, i - 1, j)) { // go up + } + // Try moving up + else if (setWay(map, i - 1, j)) { return true; - } else if (setWay(map, i, j - 1)) { // go left + } + // Try moving left + else if (setWay(map, i, j - 1)) { return true; } else { - // means that the current point is the dead end, the ball cannot proceed, set - // the current point to 3 and return false, the backtracking will start, it will - // go to the previous step and check for feasible path again + // Mark the current position as a dead end (3) and backtrack map[i][j] = 3; return false; } - } else { // if the map[i][j] != 0 , it will probably be 1,2,3, return false because the - // ball cannot hit the wall, cannot go to the path that has gone though before, - // and cannot head to deadened. + } else { + // If the position is not unvisited (either a wall, dead end, or already part of + // the path), return false return false; } } - // Here is another move strategy for the ball: up->right->down->left + /** + * Attempts to find a path through the maze using an alternative movement + * strategy "up -> right -> down -> left". + * This method explores a different order of + * movement compared to setWay(). + * + * @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 + */ public static boolean setWay2(int[][] map, int i, int j) { - if (map[6][5] == 2) { // means the ball find its path, ending condition - return true; + // Check if the ball has reached the target at map[6][5] + if (map[6][5] == 2) { + return true; // Path found } - if (map[i][j] == 0) { // if the ball haven't gone through this point - // then the ball follows the move strategy : up->right->down->left - map[i][j] = 2; // we assume that this path is feasible first, set the current point to 2 - // first。 - if (setWay2(map, i - 1, j)) { // go up + + // If the current position is unvisited (0), explore it + if (map[i][j] == 0) { + // Assume the path is feasible, mark the current position as '2' + map[i][j] = 2; + + // Try moving up + if (setWay2(map, i - 1, j)) { return true; - } else if (setWay2(map, i, j + 1)) { // go right + } + // Try moving right + else if (setWay2(map, i, j + 1)) { return true; - } else if (setWay2(map, i + 1, j)) { // go down + } + // Try moving down + else if (setWay2(map, i + 1, j)) { return true; - } else if (setWay2(map, i, j - 1)) { // go left + } + // Try moving left + else if (setWay2(map, i, j - 1)) { return true; } else { - // means that the current point is the dead end, the ball cannot proceed, set - // the current point to 3 and return false, the backtracking will start, it will - // go to the previous step and check for feasible path again + // Mark the current position as a dead end (3) and backtrack map[i][j] = 3; return false; } - } else { // if the map[i][j] != 0 , it will probably be 1,2,3, return false because the - // ball cannot hit the wall, cannot go to the path that has gone through before, - // and cannot head to deadend. + } else { + // If the position is not unvisited (either a wall, dead end, or already part of + // the path), return false return false; } } From 87839e36d9a8aa38aa05d56f346509f4f92e62b4 Mon Sep 17 00:00:00 2001 From: Hardvan Date: Sat, 5 Oct 2024 05:18:14 +0000 Subject: [PATCH 2/7] Update directory --- DIRECTORY.md | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index c272d3865b58..a349c1944adf 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -22,7 +22,7 @@ * [WordSearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/WordSearch.java) * bitmanipulation * [BitSwap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java) - * [CountSetBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/CountSetBits.java) + * [CountSetBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/CountSetBits.java) * [HighestSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/HighestSetBit.java) * [IndexOfRightMostSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/IndexOfRightMostSetBit.java) * [IsEven](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/IsEven.java) @@ -69,7 +69,7 @@ * [HexaDecimalToBinary](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/HexaDecimalToBinary.java) * [HexaDecimalToDecimal](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/HexaDecimalToDecimal.java) * [HexToOct](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/HexToOct.java) - * [IntegerToEnglish] (https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/IntegerToEnglish.java) + * [IntegerToEnglish](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/IntegerToEnglish.java) * [IntegerToRoman](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/IntegerToRoman.java) * [OctalToBinary](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/OctalToBinary.java) * [OctalToDecimal](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/OctalToDecimal.java) @@ -410,7 +410,7 @@ * [WordBoggle](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/misc/WordBoggle.java) * others * [ArrayLeftRotation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/ArrayLeftRotation.java) - * [ArrayRightRotation](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/ArrayRightRotation.java) + * [ArrayRightRotation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/ArrayRightRotation.java) * [BankersAlgorithm](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/BankersAlgorithm.java) * [BFPRT](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/BFPRT.java) * [BoyerMoore](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/BoyerMoore.java) @@ -559,8 +559,8 @@ * [CharactersSame](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/CharactersSame.java) * [CheckAnagrams](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/CheckAnagrams.java) * [CheckVowels](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/CheckVowels.java) - * [CountChar](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/CountChar.java) - * [CountWords](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/CountWords.java) + * [CountChar](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/CountChar.java) + * [CountWords](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/CountWords.java) * [HammingDistance](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/HammingDistance.java) * [HorspoolSearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/HorspoolSearch.java) * [Isomorphic](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/Isomorphic.java) @@ -575,13 +575,13 @@ * [Pangram](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/Pangram.java) * [PermuteString](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/PermuteString.java) * [RabinKarp](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/RabinKarp.java) - * [ReturnSubsequence](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/ReturnSubsequence.java) + * [ReturnSubsequence](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/ReturnSubsequence.java) * [ReverseString](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/ReverseString.java) * [ReverseStringRecursive](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/ReverseStringRecursive.java) * [ReverseWordsInString](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/ReverseWordsInString.java) * [Rotation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/Rotation.java) * [StringCompression](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/StringCompression.java) - * [StringMatchFiniteAutomata](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/StringMatchFiniteAutomata.java) + * [StringMatchFiniteAutomata](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/StringMatchFiniteAutomata.java) * [Upper](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/Upper.java) * [ValidParentheses](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/ValidParentheses.java) * [WordLadder](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/WordLadder.java) @@ -606,7 +606,7 @@ * [WordSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/backtracking/WordSearchTest.java) * bitmanipulation * [BitSwapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/BitSwapTest.java) - * [CountSetBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/CountSetBitsTest.java) + * [CountSetBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/CountSetBitsTest.java) * [HighestSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/HighestSetBitTest.java) * [IndexOfRightMostSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/IndexOfRightMostSetBitTest.java) * [IsEvenTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/IsEvenTest.java) @@ -621,6 +621,7 @@ * [BlowfishTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/BlowfishTest.java) * [CaesarTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/CaesarTest.java) * [DESTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/DESTest.java) + * [HillCipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/HillCipherTest.java) * [PlayfairTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/PlayfairTest.java) * [PolybiusTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/PolybiusTest.java) * [RSATest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/RSATest.java) @@ -639,6 +640,7 @@ * [HexaDecimalToBinaryTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/HexaDecimalToBinaryTest.java) * [HexaDecimalToDecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/HexaDecimalToDecimalTest.java) * [HexToOctTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/HexToOctTest.java) + * [IntegerToEnglishTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/IntegerToEnglishTest.java) * [IntegerToRomanTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/IntegerToRomanTest.java) * [OctalToBinaryTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/OctalToBinaryTest.java) * [OctalToDecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/OctalToDecimalTest.java) @@ -991,8 +993,8 @@ * [CharacterSameTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/CharacterSameTest.java) * [CheckAnagramsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/CheckAnagramsTest.java) * [CheckVowelsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/CheckVowelsTest.java) - * [CountCharTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/CountCharTest.java) - * [CountWordsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/CountWordsTest.java) + * [CountCharTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/CountCharTest.java) + * [CountWordsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/CountWordsTest.java) * [HammingDistanceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/HammingDistanceTest.java) * [HorspoolSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/HorspoolSearchTest.java) * [IsomorphicTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/IsomorphicTest.java) @@ -1005,13 +1007,13 @@ * [PalindromeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/PalindromeTest.java) * [PangramTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/PangramTest.java) * [PermuteStringTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/PermuteStringTest.java) - * [ReturnSubsequenceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/ReturnSubsequenceTest.java) + * [ReturnSubsequenceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/ReturnSubsequenceTest.java) * [ReverseStringRecursiveTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/ReverseStringRecursiveTest.java) * [ReverseStringTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/ReverseStringTest.java) * [ReverseWordsInStringTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/ReverseWordsInStringTest.java) * [RotationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/RotationTest.java) * [StringCompressionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/StringCompressionTest.java) - * [StringMatchFiniteAutomataTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/StringMatchFiniteAutomataTest.java) + * [StringMatchFiniteAutomataTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/StringMatchFiniteAutomataTest.java) * [UpperTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/UpperTest.java) * [ValidParenthesesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/ValidParenthesesTest.java) * [WordLadderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/WordLadderTest.java) From 066f97052ceda3fee97f8ad006033758c722ab3e Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sat, 5 Oct 2024 10:49:19 +0530 Subject: [PATCH 3/7] Fine tune comments --- .../com/thealgorithms/backtracking/MazeRecursion.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java b/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java index 5e66915f7599..bac5536683ff 100644 --- a/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java +++ b/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java @@ -148,19 +148,19 @@ public static boolean setWay2(int[][] map, int i, int j) { // Assume the path is feasible, mark the current position as '2' map[i][j] = 2; - // Try moving up + // Move up if (setWay2(map, i - 1, j)) { return true; } - // Try moving right + // Move right else if (setWay2(map, i, j + 1)) { return true; } - // Try moving down + // Move down else if (setWay2(map, i + 1, j)) { return true; } - // Try moving left + // Move left else if (setWay2(map, i, j - 1)) { return true; } else { From 5fb842e7c13eba7b0f8b12ff9f3099540e5673a4 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sat, 5 Oct 2024 10:49:39 +0530 Subject: [PATCH 4/7] Fix comments --- .../com/thealgorithms/backtracking/MazeRecursion.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java b/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java index bac5536683ff..a53c8934793c 100644 --- a/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java +++ b/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java @@ -99,19 +99,19 @@ public static boolean setWay(int[][] map, int i, int j) { // Assume the path is feasible, mark the current position as '2' map[i][j] = 2; - // Try moving down + // Move down if (setWay(map, i + 1, j)) { return true; } - // Try moving right + // Move right else if (setWay(map, i, j + 1)) { return true; } - // Try moving up + // Move up else if (setWay(map, i - 1, j)) { return true; } - // Try moving left + // Move left else if (setWay(map, i, j - 1)) { return true; } else { From 779429c66c1252731659bd68bf8cf0a35452dbe5 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 6 Oct 2024 13:25:16 +0530 Subject: [PATCH 5/7] Modify MazeRecursion --- .../backtracking/MazeRecursion.java | 152 +++++------------- .../backtracking/MazeRecursionTest.java | 66 ++++---- 2 files changed, 72 insertions(+), 146 deletions(-) diff --git a/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java b/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java index a53c8934793c..e578132528d6 100644 --- a/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java +++ b/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java @@ -3,8 +3,7 @@ /** * This class contains methods to solve a maze using recursive backtracking. * The maze is represented as a 2D array where walls, paths, and visited/dead - * ends - * are marked with different integers. + * 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. @@ -15,163 +14,96 @@ private MazeRecursion() { } /** - * This method sets up a maze as a 2D array, where '1' represents walls and '0' - * represents open paths. It then calls recursive functions to find paths using - * two different movement strategies. The results are printed to the console. + * This method solves the maze using the "down -> right -> up -> left" + * movement strategy. + * + * @param map The 2D array representing the maze (walls, paths, etc.) + * @return The solved maze with paths marked, or null if no solution exists. */ - public static void mazeRecursion() { - - int[][] map = new int[8][7]; // Create an 8x7 maze map (2D array) - int[][] map2 = new int[8][7]; // Copy of the maze for an alternative pathfinding method - - // Initialize the maze with boundaries (set walls as '1') - // Set the top and bottom rows as walls - for (int i = 0; i < 7; i++) { - map[0][i] = 1; - map[7][i] = 1; - } - - // Set the left and right columns as walls - for (int i = 0; i < 8; i++) { - map[i][0] = 1; - map[i][6] = 1; - } - - // Place internal obstacles in the maze - map[3][1] = 1; // Wall block at position (3,1) - map[3][2] = 1; // Wall block at position (3,2) - - System.out.println("Initial maze layout:"); - for (int i = 0; i < 8; i++) { - for (int j = 0; j < 7; j++) { - System.out.print(map[i][j] + " "); - } - System.out.println(); - } - - // Clone the maze into map2 for the second pathfinding method - for (int i = 0; i < map.length; i++) { - System.arraycopy(map[i], 0, map2[i], 0, map[i].length); - } - - // Use the first pathfinding method with a "down -> right -> up -> left" strategy - setWay(map, 1, 1); - - // Use the second pathfinding method with a "up -> right -> down -> left" strategy - setWay2(map2, 1, 1); - - // Print the maze after pathfinding using the first method - System.out.println("Maze after pathfinding using first strategy:"); - for (int i = 0; i < 8; i++) { - for (int j = 0; j < 7; j++) { - System.out.print(map[i][j] + " "); - } - System.out.println(); + public static int[][] solveMazeUsingFirstStrategy(int[][] map) { + if (setWay(map, 1, 1)) { + return map; } + return null; + } - // Print the maze after pathfinding using the second method - System.out.println("Maze after pathfinding using second strategy:"); - for (int i = 0; i < 8; i++) { - for (int j = 0; j < 7; j++) { - System.out.print(map2[i][j] + " "); - } - System.out.println(); + /** + * This method solves the maze using the "up -> right -> down -> left" + * movement strategy. + * + * @param map The 2D array representing the maze (walls, paths, etc.) + * @return The solved maze with paths marked, or null if no solution exists. + */ + public static int[][] solveMazeUsingSecondStrategy(int[][] map) { + if (setWay2(map, 1, 1)) { + return map; } + return null; } /** - * Attempts to find a path through the maze using a "down -> right -> up -> - * left" movement strategy. The ball tries to reach position (6,5), and the path is - * marked with '2' for valid paths and '3' for dead ends. + * Attempts to find a path through the maze using a "down -> right -> up -> left" + * movement strategy. 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 */ - public static boolean setWay(int[][] map, int i, int j) { + private static boolean setWay(int[][] map, int i, int j) { if (map[6][5] == 2) { return true; } // If the current position is unvisited (0), explore it if (map[i][j] == 0) { - // Assume the path is feasible, mark the current position as '2' + // Mark the current position as '2' map[i][j] = 2; // Move down - if (setWay(map, i + 1, j)) { - return true; - } + if (setWay(map, i + 1, j)) return true; // Move right - else if (setWay(map, i, j + 1)) { - return true; - } + else if (setWay(map, i, j + 1)) return true; // Move up - else if (setWay(map, i - 1, j)) { - return true; - } + else if (setWay(map, i - 1, j)) return true; // Move left - else if (setWay(map, i, j - 1)) { - return true; - } else { - // Mark the current position as a dead end (3) and backtrack - map[i][j] = 3; - return false; - } - } else { - // If the position is not unvisited (either a wall, dead end, or already part of - // the path), return false + else if (setWay(map, i, j - 1)) return true; + + map[i][j] = 3; // Mark as dead end (3) if no direction worked return false; } + return false; } /** * Attempts to find a path through the maze using an alternative movement * strategy "up -> right -> down -> left". - * This method explores a different order of - * movement compared to setWay(). * * @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 */ - public static boolean setWay2(int[][] map, int i, int j) { - // Check if the ball has reached the target at map[6][5] + private static boolean setWay2(int[][] map, int i, int j) { if (map[6][5] == 2) { - return true; // Path found + return true; } - // If the current position is unvisited (0), explore it if (map[i][j] == 0) { - // Assume the path is feasible, mark the current position as '2' map[i][j] = 2; // Move up - if (setWay2(map, i - 1, j)) { - return true; - } + if (setWay2(map, i - 1, j)) return true; // Move right - else if (setWay2(map, i, j + 1)) { - return true; - } + else if (setWay2(map, i, j + 1)) return true; // Move down - else if (setWay2(map, i + 1, j)) { - return true; - } + else if (setWay2(map, i + 1, j)) return true; // Move left - else if (setWay2(map, i, j - 1)) { - return true; - } else { - // Mark the current position as a dead end (3) and backtrack - map[i][j] = 3; - return false; - } - } else { - // If the position is not unvisited (either a wall, dead end, or already part of - // the path), return false + else if (setWay2(map, i, j - 1)) return true; + + map[i][j] = 3; // Mark as dead end (3) if no direction worked return false; } + return false; } } diff --git a/src/test/java/com/thealgorithms/backtracking/MazeRecursionTest.java b/src/test/java/com/thealgorithms/backtracking/MazeRecursionTest.java index edaca14af067..9b18cafbbea7 100644 --- a/src/test/java/com/thealgorithms/backtracking/MazeRecursionTest.java +++ b/src/test/java/com/thealgorithms/backtracking/MazeRecursionTest.java @@ -11,63 +11,57 @@ public class MazeRecursionTest { @Test - public void testMaze() { - // First create a 2 dimensions array to mimic a maze map + public void testSolveMazeUsingFirstAndSecondStrategy() { int[][] map = new int[8][7]; int[][] map2 = new int[8][7]; - // We use 1 to indicate wall + // We use 1 to indicate walls // Set the ceiling and floor to 1 for (int i = 0; i < 7; i++) { map[0][i] = 1; map[7][i] = 1; } - - // Then we set the left and right wall to 1 + // Set the left and right wall to 1 for (int i = 0; i < 8; i++) { map[i][0] = 1; map[i][6] = 1; } - - // Now we have created a maze with its wall initialized - - // Here we set the obstacle + // Set obstacles map[3][1] = 1; map[3][2] = 1; - // clone another map for setWay2 method + // Clone the original map for the second pathfinding strategy for (int i = 0; i < map.length; i++) { - for (int j = 0; j < map[i].length; j++) { - map2[i][j] = map[i][j]; - } + System.arraycopy(map[i], 0, map2[i], 0, map[i].length); } - MazeRecursion.setWay(map, 1, 1); - MazeRecursion.setWay2(map2, 1, 1); - - int[][] expectedMap = new int[][] { - {1, 1, 1, 1, 1, 1, 1}, - {1, 2, 0, 0, 0, 0, 1}, - {1, 2, 2, 2, 0, 0, 1}, - {1, 1, 1, 2, 0, 0, 1}, - {1, 0, 0, 2, 0, 0, 1}, - {1, 0, 0, 2, 0, 0, 1}, - {1, 0, 0, 2, 2, 2, 1}, - {1, 1, 1, 1, 1, 1, 1}, + // Solve the maze using the first strategy + int[][] solvedMap1 = MazeRecursion.solveMazeUsingFirstStrategy(map); + // Solve the maze using the second strategy + int[][] solvedMap2 = MazeRecursion.solveMazeUsingSecondStrategy(map2); + int[][] expectedMap1 = new int[][] { + {1, 1, 1, 1, 1, 1, 1}, + {1, 2, 0, 0, 0, 0, 1}, + {1, 2, 2, 2, 0, 0, 1}, + {1, 1, 1, 2, 0, 0, 1}, + {1, 0, 0, 2, 0, 0, 1}, + {1, 0, 0, 2, 0, 0, 1}, + {1, 0, 0, 2, 2, 2, 1}, + {1, 1, 1, 1, 1, 1, 1}, }; - int[][] expectedMap2 = new int[][] { - {1, 1, 1, 1, 1, 1, 1}, - {1, 2, 2, 2, 2, 2, 1}, - {1, 0, 0, 0, 0, 2, 1}, - {1, 1, 1, 0, 0, 2, 1}, - {1, 0, 0, 0, 0, 2, 1}, - {1, 0, 0, 0, 0, 2, 1}, - {1, 0, 0, 0, 0, 2, 1}, - {1, 1, 1, 1, 1, 1, 1}, + {1, 1, 1, 1, 1, 1, 1}, + {1, 2, 2, 2, 2, 2, 1}, + {1, 0, 0, 0, 0, 2, 1}, + {1, 1, 1, 0, 0, 2, 1}, + {1, 0, 0, 0, 0, 2, 1}, + {1, 0, 0, 0, 0, 2, 1}, + {1, 0, 0, 0, 0, 2, 1}, + {1, 1, 1, 1, 1, 1, 1}, }; - assertArrayEquals(map, expectedMap); - assertArrayEquals(map2, expectedMap2); + // Assert the results + assertArrayEquals(expectedMap1, solvedMap1); + assertArrayEquals(expectedMap2, solvedMap2); } } From 94ffc80f5a8368d691d2f8fa3597de0725642f58 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 6 Oct 2024 13:26:50 +0530 Subject: [PATCH 6/7] Fix --- .../backtracking/MazeRecursion.java | 18 +++++++---- .../backtracking/MazeRecursionTest.java | 32 +++++++++---------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java b/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java index e578132528d6..5dfb0517e40b 100644 --- a/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java +++ b/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java @@ -63,11 +63,14 @@ private static boolean setWay(int[][] map, int i, int j) { // Move down if (setWay(map, i + 1, j)) return true; // Move right - else if (setWay(map, i, j + 1)) return true; + else if (setWay(map, i, j + 1)) + return true; // Move up - else if (setWay(map, i - 1, j)) return true; + else if (setWay(map, i - 1, j)) + return true; // Move left - else if (setWay(map, i, j - 1)) return true; + else if (setWay(map, i, j - 1)) + return true; map[i][j] = 3; // Mark as dead end (3) if no direction worked return false; @@ -95,11 +98,14 @@ private static boolean setWay2(int[][] map, int i, int j) { // Move up if (setWay2(map, i - 1, j)) return true; // Move right - else if (setWay2(map, i, j + 1)) return true; + else if (setWay2(map, i, j + 1)) + return true; // Move down - else if (setWay2(map, i + 1, j)) return true; + else if (setWay2(map, i + 1, j)) + return true; // Move left - else if (setWay2(map, i, j - 1)) return true; + else if (setWay2(map, i, j - 1)) + return true; map[i][j] = 3; // Mark as dead end (3) if no direction worked return false; diff --git a/src/test/java/com/thealgorithms/backtracking/MazeRecursionTest.java b/src/test/java/com/thealgorithms/backtracking/MazeRecursionTest.java index 9b18cafbbea7..b8e77fb38bad 100644 --- a/src/test/java/com/thealgorithms/backtracking/MazeRecursionTest.java +++ b/src/test/java/com/thealgorithms/backtracking/MazeRecursionTest.java @@ -40,24 +40,24 @@ public void testSolveMazeUsingFirstAndSecondStrategy() { // Solve the maze using the second strategy int[][] solvedMap2 = MazeRecursion.solveMazeUsingSecondStrategy(map2); int[][] expectedMap1 = new int[][] { - {1, 1, 1, 1, 1, 1, 1}, - {1, 2, 0, 0, 0, 0, 1}, - {1, 2, 2, 2, 0, 0, 1}, - {1, 1, 1, 2, 0, 0, 1}, - {1, 0, 0, 2, 0, 0, 1}, - {1, 0, 0, 2, 0, 0, 1}, - {1, 0, 0, 2, 2, 2, 1}, - {1, 1, 1, 1, 1, 1, 1}, + {1, 1, 1, 1, 1, 1, 1}, + {1, 2, 0, 0, 0, 0, 1}, + {1, 2, 2, 2, 0, 0, 1}, + {1, 1, 1, 2, 0, 0, 1}, + {1, 0, 0, 2, 0, 0, 1}, + {1, 0, 0, 2, 0, 0, 1}, + {1, 0, 0, 2, 2, 2, 1}, + {1, 1, 1, 1, 1, 1, 1}, }; int[][] expectedMap2 = new int[][] { - {1, 1, 1, 1, 1, 1, 1}, - {1, 2, 2, 2, 2, 2, 1}, - {1, 0, 0, 0, 0, 2, 1}, - {1, 1, 1, 0, 0, 2, 1}, - {1, 0, 0, 0, 0, 2, 1}, - {1, 0, 0, 0, 0, 2, 1}, - {1, 0, 0, 0, 0, 2, 1}, - {1, 1, 1, 1, 1, 1, 1}, + {1, 1, 1, 1, 1, 1, 1}, + {1, 2, 2, 2, 2, 2, 1}, + {1, 0, 0, 0, 0, 2, 1}, + {1, 1, 1, 0, 0, 2, 1}, + {1, 0, 0, 0, 0, 2, 1}, + {1, 0, 0, 0, 0, 2, 1}, + {1, 0, 0, 0, 0, 2, 1}, + {1, 1, 1, 1, 1, 1, 1}, }; // Assert the results From 6d0d1ffe19990a3692df5c7688c001bc2fec9b4b Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 6 Oct 2024 13:29:24 +0530 Subject: [PATCH 7/7] Fix --- .../backtracking/MazeRecursion.java | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java b/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java index 5dfb0517e40b..8247172e7ee0 100644 --- a/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java +++ b/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java @@ -61,16 +61,21 @@ private static boolean setWay(int[][] map, int i, int j) { map[i][j] = 2; // Move down - if (setWay(map, i + 1, j)) return true; + if (setWay(map, i + 1, j)) { + return true; + } // Move right - else if (setWay(map, i, j + 1)) + else if (setWay(map, i, j + 1)) { return true; + } // Move up - else if (setWay(map, i - 1, j)) + else if (setWay(map, i - 1, j)) { return true; + } // Move left - else if (setWay(map, i, j - 1)) + else if (setWay(map, i, j - 1)) { return true; + } map[i][j] = 3; // Mark as dead end (3) if no direction worked return false; @@ -96,16 +101,21 @@ private static boolean setWay2(int[][] map, int i, int j) { map[i][j] = 2; // Move up - if (setWay2(map, i - 1, j)) return true; + if (setWay2(map, i - 1, j)) { + return true; + } // Move right - else if (setWay2(map, i, j + 1)) + else if (setWay2(map, i, j + 1)) { return true; + } // Move down - else if (setWay2(map, i + 1, j)) + else if (setWay2(map, i + 1, j)) { return true; + } // Move left - else if (setWay2(map, i, j - 1)) + else if (setWay2(map, i, j - 1)) { return true; + } map[i][j] = 3; // Mark as dead end (3) if no direction worked return false;