-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay57.cpp
More file actions
26 lines (25 loc) · 1.05 KB
/
Day57.cpp
File metadata and controls
26 lines (25 loc) · 1.05 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
class Solution {
public:
int mod = 1e9+7;
vector<vector<vector<int>>> dp;
int solve(int n, int m, int maxMove, int startRow, int startColumn){
if(startColumn < 0 || startColumn >= n || startRow < 0 || startRow >= m){
return 1;
}
if(maxMove == 0){
return 0;
}
if(dp[startRow][startColumn][maxMove] != -1){
return dp[startRow][startColumn][maxMove];
}
int u = solve(n, m, maxMove-1, startRow-1, startColumn);
int d = solve(n, m, maxMove-1, startRow+1, startColumn);
int l = solve(n, m, maxMove-1, startRow, startColumn-1);
int r = solve(n, m, maxMove-1, startRow, startColumn+1);
return dp[startRow][startColumn][maxMove] = ((u+d)%mod + (l+r) % mod) % mod;
}
int findPaths(int m, int n, int maxMove, int startRow, int startColumn) {
dp = vector<vector<vector<int>>>(m, vector<vector<int>>(n, vector<int>(maxMove+1,-1)));
return solve(n, m, maxMove, startRow, startColumn);
}
};