Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Count all possible paths in a matrix #1613

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
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);
}

}