-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPath in a Matrix.java
50 lines (39 loc) · 1.24 KB
/
Path in a Matrix.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
// https://www.hackerrank.com/contests/smart-interviews/challenges/si-path-in-a-matrix
import java.io.*;
import java.util.*;
public class Solution {
static int mod=(int)1e9+7;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int m = sc.nextInt();
int b = sc.nextInt();
long[][] dp = new long[n+1][m+1];
for(long[] rows:dp){
Arrays.fill(rows,-1);
}
dp[1][1]=1; // Base Condition
for(int i=0;i<b;i++){
int x=sc.nextInt();
int y=sc.nextInt();
dp[x+1][y+1]=0;
}
for(int i=0;i<=n;i++){
dp[i][0]=0;
}
for(int i=0;i<=m;i++){
dp[0][i]=0;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(dp[i][j]==-1){
dp[i][j]=(dp[i-1][j]+dp[i][j-1]+dp[i-1][j-1])%mod;
}
}
}
System.out.println(dp[n][m]);
}
}
}