-
Notifications
You must be signed in to change notification settings - Fork 30
/
Special Matrix.cpp
70 lines (52 loc) · 1.97 KB
/
Special Matrix.cpp
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
69
70
/*
Solution by Rahul Surana
***********************************************************
You are given a matrix having n rows and m columns.
The special property of this matrix is that some of the cells of this matrix are blocked i.e. they cannot be reached.
Now you have to start from the cell (1,1) and reach the end (n,m) provided during the journey you can move horizontally
right from the current cell or vertically down from the current cell.
Can you answer the number of ways you can traverse the matrix obeying the above constraints starting from (1,1) and ending at (n,m).
***********************************************************
*/
// { Driver Code Starts
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
int MOD = 1e9+7;
int df(int i, int j, vector<vector<bool>> &go,vector<vector<int>> &dp){
if(i== go.size() || j == go[0].size() || !go[i][j]) return 0;
if(dp[i][j] != -1) return dp[i][j];
if(i==go.size()-1 && j == go[0].size()-1) return 1;
int ans = (df(i+1,j,go,dp)%MOD + df(i,j+1,go,dp)%MOD) %MOD;
return dp[i][j]=ans;
}
int FindWays(int n, int m, vector<vector<int>> blocked_cells){
vector<vector<bool>> go(n,vector<bool>(m,true));
vector<vector<int>> dp(n,vector<int>(m,-1));
for(auto bc: blocked_cells){
go[bc[0]-1][bc[1]-1] = false;
}
return df(0,0,go,dp);
}
};
// { Driver Code Starts.
int main(){
int tc;
cin >> tc;
while(tc--){
int n, m, k;
cin >> n >> m >> k;
vector<vector<int>>blocked_cells;
for(int i = 0; i < k; i++){
int x, y;
cin >> x >> y;
blocked_cells.push_back({x, y});
}
Solution obj;
int ans = obj.FindWays(n, m, blocked_cells);
cout << ans <<'\n';
}
return 0;
} // } Driver Code Ends