import java.util.ArrayList;
public class GetMazePath {
public static void main(String[] args) {
ArrayList<String> ans = getMazePaths(0, 0, 2, 2);
System.out.println(ans);
}
// sr - source row
// sc - source column
// dr - destination row
// dc - destination column
public static ArrayList<String> getMazePaths(int sr, int sc, int dr, int dc) {
// base case
if (sr > dc || sc > dc) {
return new ArrayList<>();
}
if (sr == dr && sc == dc) {
ArrayList<String> horizontal = new ArrayList<>();
horizontal.add("");
return horizontal;
}
ArrayList<String> horizontal = new ArrayList<>();
ArrayList<String> vertical = new ArrayList<>();
// faith: vertical
vertical = getMazePaths(sr + 1, sc, dr, dc);
// faith: horizontal
horizontal = getMazePaths(sr, sc + 1, dr, dc);
// expectation meet faith
ArrayList<String> ans = new ArrayList<>();
for (String h : horizontal)
ans.add(h + "h");
for (String v : vertical)
ans.add(v + "v");
return ans;
}
}
Saturday, January 29, 2022
Get Maze Path
Subscribe to:
Post Comments (Atom)
Diagonal Traversal
eg. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 1 6 11 16 2 7 12 3 8 4 Approach:...
-
I/P: 24 10 20 50 -1 60 -1 -1 30 70 -1 80 110 -1 120 -1 -1 90 -1 -1 40 100 -1 -1 -1 O/P: 10 20 30 40 50 60 70 80 90 100 110 120 import ja...
-
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/ Given the sorted rotated array nums of unique elements, retu...
-
O(1) < O(log n) < O(n) < O(n log n) < O(n ^ 2) ..... < O(2 ^ n) < O(n!) Try to keep TC till O(n ^ 2) ====================...
No comments:
Post a Comment