-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathb_Mazepath.java
More file actions
48 lines (36 loc) · 1.41 KB
/
b_Mazepath.java
File metadata and controls
48 lines (36 loc) · 1.41 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
package Day_06;
import java.util.ArrayList;
public class b_Mazepath {
// Step 1 : Define function and its arguments
static ArrayList<String> getMaze(int currentRow, int currentCol, int endRow, int endCol) {
// Positive Base Case : if you reach at the end of grid
if(currentRow == endRow && currentCol == endCol) {
ArrayList<String> temp = new ArrayList<>();
temp.add("");
return temp;
}
// Negative Base Case
if(currentRow > endRow || currentCol > endCol) {
ArrayList<String> temp = new ArrayList<>();
return temp;
}
ArrayList<String> result = new ArrayList<>();
// Move one step vertical
// currentRow + 1
ArrayList<String> verticalResult = getMaze(currentRow+1, currentCol, endRow, endCol);
for(String temp : verticalResult) {
result.add("V" + temp);
}
// Move one step horizontal
// currentCol + 1
ArrayList<String> horizontalResult = getMaze(currentRow, currentCol+1, endRow, endCol);
for(String temp : horizontalResult) {
result.add("H" + temp);
}
return result;
}
public static void main(String[] args) {
ArrayList<String> res = getMaze(0, 0, 2, 2);
System.out.println(res);
}
}