We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 4e4c2b2 commit 5bf0c35Copy full SHA for 5bf0c35
Dynamic Programming/Grid Unique Paths II.cpp
@@ -0,0 +1,31 @@
1
+long long dp[505][505];
2
+const int MOD = 1000000007;
3
+
4
+int uniquePathsII(vector<vector<int>>& A) {
5
+ int n=A.size();
6
+ if(n==0)
7
+ return 0;
8
+ int m=A[0].size();
9
+ if(m==0)
10
11
+ if(A[n-1][m-1]||A[0][0])
12
13
+ for(int i=0; i<n; ++i)
14
+ for(int j=0; j<m; ++j)
15
+ dp[i][j]=0;
16
+ dp[0][0]=1LL;
17
+ for(int i=1; i<n; ++i)
18
+ if(A[i][0]==0)
19
+ dp[i][0]=dp[i-1][0];
20
+ for(int i=1; i<m; ++i)
21
+ if(A[0][i]==0)
22
+ dp[0][i]=dp[0][i-1];
23
+ for(int i=1; i<n;++i)
24
+ for(int j=1; j<m; ++j)
25
+ if(A[i][j]==0)
26
+ dp[i][j]=(dp[i-1][j]+dp[i][j-1])%MOD;
27
+ return dp[n-1][m-1];
28
+}
29
+int Solution::solve(vector<vector<int> > &A) {
30
+ return uniquePathsII(A);
31
0 commit comments