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
| class Solution { public:
vector<vector<vector<int>>> cache; int MOD = (int) 1e9 + 7;
int findPaths(int m, int n, int maxMove, int startRow, int startColumn) { cache.resize(maxMove + 1, vector<vector<int>>(m, vector<int>(n, -1))); return dfs(m, n, maxMove, startRow, startColumn); }
int dfs(int m, int n, int u, int x, int y) { if (x >= m || x < 0) return 1; if (y >= n || y < 0) return 1; if (u == 0) return 0; if (cache[u][x][y] != -1) return cache[u][x][y]; int ans = 0; ans = dfs(m, n, u - 1, x + 1, y) % MOD; ans = (ans + dfs(m, n, u - 1, x, y + 1)) % MOD; ans = (ans + dfs(m, n, u - 1, x, y - 1)) % MOD; ans = (ans + dfs(m, n, u - 1, x - 1, y)) % MOD; cache[u][x][y] = ans; return ans; } };
|