-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid Paths.cpp
More file actions
158 lines (128 loc) · 2.83 KB
/
Grid Paths.cpp
File metadata and controls
158 lines (128 loc) · 2.83 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Memoization
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lld double
int mod=1e9+7;
const int array_size = 1e6+100;
bool primes[array_size];
ll gcd(ll a, ll b){if (b == 0)return a;return gcd(b, a % b);}
ll lcm(ll a, ll b){return ((a/gcd(a,b))*b);}
void sieve(){
primes[0] = false;
primes[1] = false;
for(ll i = 2;i*i<=array_size;i++){
if(primes[i]){
for(ll j=i*i;j<=array_size;j+=i)
primes[j] = false;
}
}
}
int find(vector<vector<char>>& a,int i,int j,int n,vector<vector<int>>& dp){
if(i<0 || j<0 || i>=n || j>=n || a[i][j]=='*')return 0;
if(dp[i][j]!=-1)return dp[i][j];
if(i==n-1 && j==n-1)return 1;
return dp[i][j] = (find(a,i+1,j,n,dp)+find(a,i,j+1,n,dp))%mod;
}
void solve(){
int n;
cin>>n;
vector<vector<char>> a(n,vector<char>(n));
vector<vector<int>> dp(n,vector<int>(n));
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>a[i][j];
dp[i][j]=-1;
}
}
cout<<find(a,0,0,n,dp);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
//memset(primes,true,sizeof(primes));
//sieve();
//memset(dp,false,sizeof(dp));
//precal();
/*int t;
cin>>t;
while(t--)*/
solve();
return 0;
}
//Tabulation Approach:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lld double
int mod=1e9+7;
const int array_size = 1e6+100;
bool primes[array_size];
ll gcd(ll a, ll b){if (b == 0)return a;return gcd(b, a % b);}
ll lcm(ll a, ll b){return ((a/gcd(a,b))*b);}
void sieve(){
primes[0] = false;
primes[1] = false;
for(ll i = 2;i*i<=array_size;i++){
if(primes[i]){
for(ll j=i*i;j<=array_size;j+=i)
primes[j] = false;
}
}
}
void solve(){
int n;
cin>>n;
vector<vector<char>> a(n,vector<char>(n));
vector<vector<int>> dp(n,vector<int>(n));
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>a[i][j];
}
}
if(a[n-1][n-1]=='*'){
cout<<0<<endl;
return;
}
dp[n-1][n-1]=1;
for(int i=n-2;i>=0;i--){
if(a[i][n-1]=='*'){
dp[i][n-1] = 0;
}
else{
dp[i][n-1] = dp[i+1][n-1];
}
}
for(int i=n-2;i>=0;i--){
if(a[n-1][i]=='*'){
dp[n-1][i] = 0;
}
else{
dp[n-1][i] = dp[n-1][i+1];
}
}
for(int i=n-2;i>=0;i--){
for(int j=n-2;j>=0;j--){
if(a[i][j] == '*')
dp[i][j]=0;
else
dp[i][j] = (dp[i+1][j]+dp[i][j+1])%mod;
}
}
cout<<dp[0][0];
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
//memset(primes,true,sizeof(primes));
//sieve();
//memset(dp,false,sizeof(dp));
//precal();
/*int t;
cin>>t;
while(t--)*/
solve();
return 0;
}