Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/main/java/com/thealgorithms/backtracking/MazeRecursion.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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));
}
}
Loading