-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathShortest_path_count.java
More file actions
68 lines (48 loc) · 1.36 KB
/
Shortest_path_count.java
File metadata and controls
68 lines (48 loc) · 1.36 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
import java.util.Stack;
public class Shortest_path_count {
int count=0;
public void findPaths(int[][] mat, Stack<Integer> path, int i, int j)
{
if (mat == null || mat.length == 0) {
return;
}
int M = mat.length;
int N = mat[0].length;
if (i == M - 1 && j == N - 1)
{
path.add(mat[i][j]);
System.out.println(path);
path.pop();
count++;
return;
}
path.add(mat[i][j]);
// move right
if ((i >= 0 && i < M && j + 1 >= 0 && j + 1 < N)) {
findPaths(mat, path, i, j + 1);
}
// move down
if ((i + 1 >= 0 && i + 1 < M && j >= 0 && j < N)) {
findPaths(mat, path, i + 1, j);
}
path.pop();
}
public void rev(){
System.out.println("Total number of Paths will be::"+count);
}
public static void main(String[] args)
{
int[][] mat =
{
{ 10, 11, 12 },
{ 13, 14, 15 },
{ 16, 17, 18 }
};
Stack<Integer> path = new Stack<>();
// start from `(0, 0)` cell
int x = 0, y = 0;
Shortest_path_count ob=new Shortest_path_count();
ob.findPaths(mat, path, x, y);
ob.rev();
}
}