-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDAY5
More file actions
32 lines (27 loc) · 1.02 KB
/
DAY5
File metadata and controls
32 lines (27 loc) · 1.02 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
//out of boundary path
class Solution {
public:
int findPaths(int m, int n, int N, int x, int y) {
const int M = 1000000000 + 7;
vector<vector<int>> dp(m, vector<int>(n, 0));
dp[x][y] = 1;
int count = 0;
for (int moves = 1; moves <= N; moves++) {
vector<vector<int>> temp(m, vector<int>(n, 0));
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (i == m - 1) count = (count + dp[i][j]) % M;
if (j == n - 1) count = (count + dp[i][j]) % M;
if (i == 0) count = (count + dp[i][j]) % M;
if (j == 0) count = (count + dp[i][j]) % M;
temp[i][j] = (
((i > 0 ? dp[i - 1][j] : 0) + (i < m - 1 ? dp[i + 1][j] : 0)) % M +
((j > 0 ? dp[i][j - 1] : 0) + (j < n - 1 ? dp[i][j + 1] : 0)) % M
) % M;
}
}
dp = temp;
}
return count;
}
};