|
4 | 4 | * This class contains methods to solve a maze using recursive backtracking. |
5 | 5 | * The maze is represented as a 2D array where walls, paths, and visited/dead |
6 | 6 | * 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. |
10 | 7 | */ |
11 | 8 | 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}}; |
12 | 11 |
|
13 | 12 | private MazeRecursion() { |
14 | 13 | } |
15 | 14 |
|
16 | 15 | /** |
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). |
19 | 18 | * |
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 |
22 | 22 | */ |
23 | 23 | 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) { |
25 | 29 | return map; |
26 | 30 | } |
27 | | - return null; |
| 31 | + return solve(map, 1, 1, 6, 5, FIRST_STRATEGY_DIRECTIONS) ? map : null; |
28 | 32 | } |
29 | 33 |
|
30 | 34 | /** |
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). |
33 | 37 | * |
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 |
36 | 41 | */ |
37 | 42 | 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) { |
39 | 48 | return map; |
40 | 49 | } |
41 | | - return null; |
| 50 | + return solve(map, 1, 1, 6, 5, SECOND_STRATEGY_DIRECTIONS) ? map : null; |
42 | 51 | } |
43 | 52 |
|
44 | 53 | /** |
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. |
47 | 55 | * |
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 |
52 | 64 | */ |
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; |
56 | 90 | } |
57 | 91 |
|
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 | + } |
62 | 96 |
|
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)) { |
77 | 99 | return true; |
78 | 100 | } |
79 | | - |
80 | | - map[i][j] = 3; // Mark as dead end (3) if no direction worked |
81 | | - return false; |
82 | 101 | } |
| 102 | + |
| 103 | + map[row][column] = 3; |
83 | 104 | return false; |
84 | 105 | } |
85 | 106 |
|
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."); |
98 | 110 | } |
99 | 111 |
|
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."); |
118 | 116 | } |
| 117 | + } |
| 118 | + return columnCount; |
| 119 | + } |
119 | 120 |
|
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."); |
122 | 125 | } |
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; |
124 | 130 | } |
125 | 131 | } |
0 commit comments