Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Backtracking/paths_in_matrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
program to count all possible paths in a matrix
*/

import java.io.*;

public class NumberOfPath {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(read.readLine()); //read test case
while(T--> 0) {
String s = read.readLine(); //read size of matrix (i.e 2 3)
String[] str = s.split(" ");
int m = Integer.parseInt(str[0]);
int n = Integer.parseInt(str[1]);
System.out.println(countPath(m,n));
}

}

private static int countPath(int m, int n) {
if (m==1 || n==1) {
return 1;
}
return countPath(m-1, n)+countPath(m, n-1);
}

}