-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursion11.java
More file actions
33 lines (24 loc) · 758 Bytes
/
Recursion11.java
File metadata and controls
33 lines (24 loc) · 758 Bytes
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
public class Recursion11 {
public static int countPaths(int i, int j, int n, int m) {
// Boundary condition (grid ke bahar chale gaye)
if (i == n || j == m) {
return 0;
}
// Destination reached
if (i == n - 1 && j == m - 1) {
return 1;
}
// Move down
int downPaths = countPaths(i + 1, j, n, m);
// Move right
int rightPaths = countPaths(i, j + 1, n, m);
// Total paths = sum (NOT multiply)
return downPaths + rightPaths;
}
public static void main(String args[]) {
int n = 3;
int m = 3;
int totalPaths = countPaths(0, 0, n, m);
System.out.println("Total Paths: " + totalPaths);
}
}