-
Notifications
You must be signed in to change notification settings - Fork 0
/
UvA 825 Top Down.cpp
58 lines (35 loc) · 983 Bytes
/
UvA 825 Top Down.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
#include <bits/stdc++.h>
#define pb push_back
#define lp(p,n) for(int p=0; p<n; p++)
using namespace std;
int r,c;
int arr[500][500];
int dp[500][500];
int number_of_ways(int i,int j){
if(i>=r || j>=c || arr[i][j]==-1) return 0;
if(i==r-1 && j==c-1) return 1;
if(dp[i][j]!=-1) return dp[i][j];
return dp[i][j]=number_of_ways(i+1,j)+number_of_ways(i,j+1);
}
int main(){
// freopen("file.txt", "r", stdin);
// freopen("file2.txt", "w", stdout);
int t;
cin>>t;
while(t--){
cin>>r>>c;
memset(arr,0,sizeof arr);
cin.ignore(); // ignore '\n'
string str;
for (int i = 1, j; i <= r; ++i) {
getline(cin, str);
stringstream ss(str);
ss >> j; // ignore the first number
while (ss >> j)
arr[i-1][j-1] = -1;
}
memset(dp,-1,sizeof dp);
cout<<number_of_ways(0,0)<<endl;
if(t!=0) cout<<endl;
}
}