-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindPaths.cpp
More file actions
53 lines (50 loc) · 1.79 KB
/
findPaths.cpp
File metadata and controls
53 lines (50 loc) · 1.79 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
// DFS + memorize
class Solution {
public:
int dfs(int x, int y, int maxMove) {
if (x < 0 || x >= m || y < 0 || y >= n)
return 1;
if (x - maxMove >= 0 && x + maxMove < m && y - maxMove >= 0 && y + maxMove < n)
return 0;
if (cache[x][y][maxMove - 1] != -1)
return cache[x][y][maxMove - 1];
int ret = 0;
for (int i = 0; i < 4; ++i) {
ret += dfs(x + dir[i][0], y + dir[i][1], maxMove - 1) % 1000000007;
ret %= 1000000007;
}
cache[x][y][maxMove - 1] = ret;
return ret;
}
int findPaths(int m, int n, int maxMove, int startRow, int startColumn) {
cache.resize(m, vector<vector<int> >(n, vector<int>(maxMove, -1)));
this -> m = m;
this -> n = n;
return dfs(startRow, startColumn, maxMove);
}
private:
vector<vector<vector<int> > > cache;
int m, n, dir[4][2] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
};
// DP
class Solution {
public:
int findPaths(int m, int n, int maxMove, int startRow, int startColumn) {
vector<vector<vector<int>>> dp(maxMove + 1, vector<vector<int>>(m, vector<int>(n, 0)));
int res = 0;
for (int k = 1; k <= maxMove; k++) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
long up = (i == 0 ? 1 : dp[k - 1][i - 1][j]);
long down = (i == m - 1 ? 1 : dp[k - 1][i + 1][j]);
long left = (j == 0 ? 1 : dp[k - 1][i][j - 1]);
long right = (j == n - 1 ? 1 : dp[k - 1][i][j + 1]);
dp[k][i][j] = (up + down + left + right) % MOD;
}
}
}
return dp[maxMove][startRow][startColumn];
}
private:
const int MOD = 1000000007;
};