-
Notifications
You must be signed in to change notification settings - Fork 21k
Expand file tree
/
Copy pathMazeRecursion.java
More file actions
137 lines (123 loc) · 4.1 KB
/
MazeRecursion.java
File metadata and controls
137 lines (123 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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 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 int[][] solveMazeUsingFirstStrategy(int[][] map) {
if (setWay(map, 1, 1)) {
return map;
}
return null;
}
/**
* 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 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
*/
/**
* Complexity Analysis
*
* Time Complexity:
* O(R * C), where R is number of rows and C is number of columns.
* Each cell is visited at most once.
*
* Space Complexity:
* O(R * C) in the worst case due to recursion stack.
*/
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) {
// Mark the current position as '2'
map[i][j] = 2;
// Move down
if (setWay(map, i + 1, j)) {
return true;
}
// Move right
else if (setWay(map, i, j + 1)) {
return true;
}
// Move up
else if (setWay(map, i - 1, j)) {
return true;
}
// Move left
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".
*
* @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
*/
private static boolean setWay2(int[][] map, int i, int j) {
if (map[6][5] == 2) {
return true;
}
if (map[i][j] == 0) {
map[i][j] = 2;
// Move up
if (setWay2(map, i - 1, j)) {
return true;
}
// Move right
else if (setWay2(map, i, j + 1)) {
return true;
}
// Move down
else if (setWay2(map, i + 1, j)) {
return true;
}
// Move left
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;
}
}