Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
out-of-boundary-paths.cpp 876 Bytes
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2017-06-07 23:59 +08:00 . Update out-of-boundary-paths.cpp
// Time: O(N * m * n)
// Space: O(m * n)
class Solution {
public:
int findPaths(int m, int n, int N, int x, int y) {
const auto M = 1000000000 + 7;
vector<vector<vector<int>>> dp(2, vector<vector<int>>(m, vector<int>(n)));
int result = 0;
for (int moves = 0; moves < N; ++moves) {
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
dp[(moves + 1) % 2][i][j] = (((i == 0 ? 1 : dp[moves % 2][i - 1][j]) +
(i == m - 1 ? 1 : dp[moves % 2][i + 1][j])) % M +
((j == 0 ? 1 : dp[moves % 2][i][j - 1]) +
(j == n - 1 ? 1 : dp[moves % 2][i][j + 1])) % M) % M;
}
}
}
return dp[N % 2][x][y];
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助