3
3
/**
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
- * ends
7
- * are marked with different integers.
6
+ * ends are marked with different integers.
8
7
*
9
8
* The goal is to find a path from a starting position to the target position
10
9
* (map[6][5]) while navigating through the maze.
@@ -15,163 +14,96 @@ private MazeRecursion() {
15
14
}
16
15
17
16
/**
18
- * This method sets up a maze as a 2D array, where '1' represents walls and '0'
19
- * represents open paths. It then calls recursive functions to find paths using
20
- * two different movement strategies. The results are printed to the console.
17
+ * This method solves the maze using the "down -> right -> up -> left"
18
+ * movement strategy.
19
+ *
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.
21
22
*/
22
- public static void mazeRecursion () {
23
-
24
- int [][] map = new int [8 ][7 ]; // Create an 8x7 maze map (2D array)
25
- int [][] map2 = new int [8 ][7 ]; // Copy of the maze for an alternative pathfinding method
26
-
27
- // Initialize the maze with boundaries (set walls as '1')
28
- // Set the top and bottom rows as walls
29
- for (int i = 0 ; i < 7 ; i ++) {
30
- map [0 ][i ] = 1 ;
31
- map [7 ][i ] = 1 ;
32
- }
33
-
34
- // Set the left and right columns as walls
35
- for (int i = 0 ; i < 8 ; i ++) {
36
- map [i ][0 ] = 1 ;
37
- map [i ][6 ] = 1 ;
38
- }
39
-
40
- // Place internal obstacles in the maze
41
- map [3 ][1 ] = 1 ; // Wall block at position (3,1)
42
- map [3 ][2 ] = 1 ; // Wall block at position (3,2)
43
-
44
- System .out .println ("Initial maze layout:" );
45
- for (int i = 0 ; i < 8 ; i ++) {
46
- for (int j = 0 ; j < 7 ; j ++) {
47
- System .out .print (map [i ][j ] + " " );
48
- }
49
- System .out .println ();
50
- }
51
-
52
- // Clone the maze into map2 for the second pathfinding method
53
- for (int i = 0 ; i < map .length ; i ++) {
54
- System .arraycopy (map [i ], 0 , map2 [i ], 0 , map [i ].length );
55
- }
56
-
57
- // Use the first pathfinding method with a "down -> right -> up -> left" strategy
58
- setWay (map , 1 , 1 );
59
-
60
- // Use the second pathfinding method with a "up -> right -> down -> left" strategy
61
- setWay2 (map2 , 1 , 1 );
62
-
63
- // Print the maze after pathfinding using the first method
64
- System .out .println ("Maze after pathfinding using first strategy:" );
65
- for (int i = 0 ; i < 8 ; i ++) {
66
- for (int j = 0 ; j < 7 ; j ++) {
67
- System .out .print (map [i ][j ] + " " );
68
- }
69
- System .out .println ();
23
+ public static int [][] solveMazeUsingFirstStrategy (int [][] map ) {
24
+ if (setWay (map , 1 , 1 )) {
25
+ return map ;
70
26
}
27
+ return null ;
28
+ }
71
29
72
- // Print the maze after pathfinding using the second method
73
- System .out .println ("Maze after pathfinding using second strategy:" );
74
- for (int i = 0 ; i < 8 ; i ++) {
75
- for (int j = 0 ; j < 7 ; j ++) {
76
- System .out .print (map2 [i ][j ] + " " );
77
- }
78
- System .out .println ();
30
+ /**
31
+ * This method solves the maze using the "up -> right -> down -> left"
32
+ * movement strategy.
33
+ *
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.
36
+ */
37
+ public static int [][] solveMazeUsingSecondStrategy (int [][] map ) {
38
+ if (setWay2 (map , 1 , 1 )) {
39
+ return map ;
79
40
}
41
+ return null ;
80
42
}
81
43
82
44
/**
83
- * Attempts to find a path through the maze using a "down -> right -> up ->
84
- * left" movement strategy. The ball tries to reach position (6,5), and the path is
85
- * marked with '2' for valid paths and '3' for dead ends.
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.
86
47
*
87
48
* @param map The 2D array representing the maze (walls, paths, etc.)
88
49
* @param i The current x-coordinate of the ball (row index)
89
50
* @param j The current y-coordinate of the ball (column index)
90
51
* @return True if a path is found to (6,5), otherwise false
91
52
*/
92
- public static boolean setWay (int [][] map , int i , int j ) {
53
+ private static boolean setWay (int [][] map , int i , int j ) {
93
54
if (map [6 ][5 ] == 2 ) {
94
55
return true ;
95
56
}
96
57
97
58
// If the current position is unvisited (0), explore it
98
59
if (map [i ][j ] == 0 ) {
99
- // Assume the path is feasible, mark the current position as '2'
60
+ // Mark the current position as '2'
100
61
map [i ][j ] = 2 ;
101
62
102
63
// Move down
103
- if (setWay (map , i + 1 , j )) {
104
- return true ;
105
- }
64
+ if (setWay (map , i + 1 , j )) return true ;
106
65
// Move right
107
- else if (setWay (map , i , j + 1 )) {
108
- return true ;
109
- }
66
+ else if (setWay (map , i , j + 1 )) return true ;
110
67
// Move up
111
- else if (setWay (map , i - 1 , j )) {
112
- return true ;
113
- }
68
+ else if (setWay (map , i - 1 , j )) return true ;
114
69
// Move left
115
- else if (setWay (map , i , j - 1 )) {
116
- return true ;
117
- } else {
118
- // Mark the current position as a dead end (3) and backtrack
119
- map [i ][j ] = 3 ;
120
- return false ;
121
- }
122
- } else {
123
- // If the position is not unvisited (either a wall, dead end, or already part of
124
- // the path), return false
70
+ else if (setWay (map , i , j - 1 )) return true ;
71
+
72
+ map [i ][j ] = 3 ; // Mark as dead end (3) if no direction worked
125
73
return false ;
126
74
}
75
+ return false ;
127
76
}
128
77
129
78
/**
130
79
* Attempts to find a path through the maze using an alternative movement
131
80
* strategy "up -> right -> down -> left".
132
- * This method explores a different order of
133
- * movement compared to setWay().
134
81
*
135
82
* @param map The 2D array representing the maze (walls, paths, etc.)
136
83
* @param i The current x-coordinate of the ball (row index)
137
84
* @param j The current y-coordinate of the ball (column index)
138
85
* @return True if a path is found to (6,5), otherwise false
139
86
*/
140
- public static boolean setWay2 (int [][] map , int i , int j ) {
141
- // Check if the ball has reached the target at map[6][5]
87
+ private static boolean setWay2 (int [][] map , int i , int j ) {
142
88
if (map [6 ][5 ] == 2 ) {
143
- return true ; // Path found
89
+ return true ;
144
90
}
145
91
146
- // If the current position is unvisited (0), explore it
147
92
if (map [i ][j ] == 0 ) {
148
- // Assume the path is feasible, mark the current position as '2'
149
93
map [i ][j ] = 2 ;
150
94
151
95
// Move up
152
- if (setWay2 (map , i - 1 , j )) {
153
- return true ;
154
- }
96
+ if (setWay2 (map , i - 1 , j )) return true ;
155
97
// Move right
156
- else if (setWay2 (map , i , j + 1 )) {
157
- return true ;
158
- }
98
+ else if (setWay2 (map , i , j + 1 )) return true ;
159
99
// Move down
160
- else if (setWay2 (map , i + 1 , j )) {
161
- return true ;
162
- }
100
+ else if (setWay2 (map , i + 1 , j )) return true ;
163
101
// Move left
164
- else if (setWay2 (map , i , j - 1 )) {
165
- return true ;
166
- } else {
167
- // Mark the current position as a dead end (3) and backtrack
168
- map [i ][j ] = 3 ;
169
- return false ;
170
- }
171
- } else {
172
- // If the position is not unvisited (either a wall, dead end, or already part of
173
- // the path), return false
102
+ else if (setWay2 (map , i , j - 1 )) return true ;
103
+
104
+ map [i ][j ] = 3 ; // Mark as dead end (3) if no direction worked
174
105
return false ;
175
106
}
107
+ return false ;
176
108
}
177
109
}
0 commit comments