-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathCountingTheNumberOfPathsToReachFrom1ToN.java
53 lines (50 loc) · 1.71 KB
/
CountingTheNumberOfPathsToReachFrom1ToN.java
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
/**
* Google Online Coding Challenge: Counting paths
* You are given a matrix A having N rows and M columns. The rows are numbered 1 to N from top to bottom and columns are
* numbered 1 to M from left to right. You are allowed to move right and down only, i.e., if you are at cell (i,j), then
* you can move to (i+1, j) and (i, j+1). You are not allowed to move outside the matrix.
* Your task is to find the number of good paths starting from (1, 1) and ending at (N, M).
* Good Path: If the sum of all elements that lie in the path is divisible by K, then the path is considered as good.
*
*
*
**/
import java.io.*;
import java.util.Set;
import java.util.HashSet;
import java.util.Scanner;
public class CountingTheNumberOfPathsToReachFrom1ToN {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Scanner sc = new Scanner(System.in);
int testCase = Integer.parseInt(br.readLine());
while(testCase-- > 0){
long n = Long.parseLong(br.readLine());
long m = Long.parseLong(br.readLine());
long a[][] = new long[(int)n][(int)m];
for (int i = 0; i < (int)n; i++) {
for (int j = 0; j < (int)m; j++) {
a[i][j] = sc.nextLong();
}
}
long k = Long.parseLong(br.readLine());
printMat(a);
long res = paths(a, n, m);
System.out.println(res);
}
}
static void printMat(long[][] m){
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m[0].length; j++) {
System.out.print(m[i][j]+" ");
}
System.out.println();
}
}
private static long paths(long a[][], long N, long M){
if (a[(int)N][(int)M] == 1 || a[(int)N][(int)M] == 1) {
return 1;
}
return paths(a, N, M-1) + paths(a, M, N-1);
}
}