-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUniquePathsGrid.java
More file actions
45 lines (39 loc) · 1.14 KB
/
UniquePathsGrid.java
File metadata and controls
45 lines (39 loc) · 1.14 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
public class UniquePathsGrid {
public static void main(String args[]){
// int grid[][] = {
// {0,0,0},
// {0,1,0},
// {0,0,0}
// };
// int grid[][] = {
// {0,0,1},
// {1,0,0},
// {0,0,1}
// };
// int grid[][] = {
// {0,0,0,1,0},
// {1,0,0,0,1},
// {1,1,0,0,0}
// };
int grid[][] = {
{1}
};
System.out.println("Unique paths in the matrix: "+ uniquePaths(grid));
}
public static int uniquePaths(int[][] grid) {
//initial call with (0,0)
int res = countUniquePaths(grid,0,0);
return res;
}
public static int countUniquePaths(int [][] grid, int i, int j){
System.out.println(i+" "+j);
if(i == grid.length-1 && j == grid[0].length-1){
return 1;
}
else if(i> grid.length-1 || j> grid[0].length-1 || grid[i][j] == 1){
return 0;
}
//right + down possbilities
return countUniquePaths(grid, i, j+1) + countUniquePaths(grid, i+1, j);
}
}