576:出界的路径数(时间不足,未做)
This commit is contained in:
parent
b92d989a2a
commit
9dc8833ec1
103
src/main/java/leetcode/editor/cn/OutOfBoundaryPaths.java
Normal file
103
src/main/java/leetcode/editor/cn/OutOfBoundaryPaths.java
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
//给你一个大小为 m x n 的网格和一个球。球的起始坐标为 [startRow, startColumn] 。你可以将球移到在四个方向上相邻的单元格内(可以
|
||||||
|
//穿过网格边界到达网格之外)。你 最多 可以移动 maxMove 次球。
|
||||||
|
//
|
||||||
|
// 给你五个整数 m、n、maxMove、startRow 以及 startColumn ,找出并返回可以将球移出边界的路径数量。因为答案可能非常大,返回对
|
||||||
|
//109 + 7 取余 后的结果。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:m = 2, n = 2, maxMove = 2, startRow = 0, startColumn = 0
|
||||||
|
//输出:6
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:m = 1, n = 3, maxMove = 3, startRow = 0, startColumn = 1
|
||||||
|
//输出:12
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 1 <= m, n <= 50
|
||||||
|
// 0 <= maxMove <= 50
|
||||||
|
// 0 <= startRow < m
|
||||||
|
// 0 <= startColumn < n
|
||||||
|
//
|
||||||
|
// Related Topics 动态规划
|
||||||
|
// 👍 190 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
//576:出界的路径数
|
||||||
|
class OutOfBoundaryPaths {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new OutOfBoundaryPaths().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
|
||||||
|
// 四个方向
|
||||||
|
int[][] dirs = new int[][] {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
|
||||||
|
// 取余
|
||||||
|
int MOD = 1000000007;
|
||||||
|
|
||||||
|
public int findPaths(int m, int n, int maxMove, int startRow, int startColumn) {
|
||||||
|
// 缓存
|
||||||
|
int[][][] memo = new int[m][n][maxMove + 1];
|
||||||
|
for (int i = 0; i < m; i++) {
|
||||||
|
for (int j = 0; j < n; j++) {
|
||||||
|
for (int k = 0; k <= maxMove; k++) {
|
||||||
|
memo[i][j][k] = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dfs(m, n, maxMove, startRow, startColumn, memo);
|
||||||
|
}
|
||||||
|
|
||||||
|
private int dfs(int m, int n, int moveCount, int i, int j, int[][][] memo) {
|
||||||
|
// 越界了就找到了一条路径
|
||||||
|
if (i < 0 || j < 0 || i >= m || j >= n) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 没有移动次数了,返回0
|
||||||
|
if (moveCount == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 缓存中存在
|
||||||
|
if (memo[i][j][moveCount] != -1) {
|
||||||
|
return memo[i][j][moveCount];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 剪枝:如果小球不管怎么移动都无法越出网格,那就剪掉这个枝
|
||||||
|
if (i - moveCount >= 0 && j - moveCount >= 0 && i + moveCount < m && j + moveCount < n) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 从这个点出发的符合条件的路径数量
|
||||||
|
int sum = 0;
|
||||||
|
for (int[] dir : dirs) {
|
||||||
|
// 记得取余
|
||||||
|
sum = (sum + dfs(m, n, moveCount - 1, i + dir[0], j + dir[1], memo)) % MOD;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 记录缓存
|
||||||
|
memo[i][j][moveCount] = sum;
|
||||||
|
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
31
src/main/java/leetcode/editor/cn/OutOfBoundaryPaths.md
Normal file
31
src/main/java/leetcode/editor/cn/OutOfBoundaryPaths.md
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<p>给你一个大小为 <code>m x n</code> 的网格和一个球。球的起始坐标为 <code>[startRow, startColumn]</code> 。你可以将球移到在四个方向上相邻的单元格内(可以穿过网格边界到达网格之外)。你 <strong>最多</strong> 可以移动 <code>maxMove</code> 次球。</p>
|
||||||
|
|
||||||
|
<p>给你五个整数 <code>m</code>、<code>n</code>、<code>maxMove</code>、<code>startRow</code> 以及 <code>startColumn</code> ,找出并返回可以将球移出边界的路径数量。因为答案可能非常大,返回对 <code>10<sup>9</sup> + 7</code> <strong>取余</strong> 后的结果。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/28/out_of_boundary_paths_1.png" style="width: 500px; height: 296px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>m = 2, n = 2, maxMove = 2, startRow = 0, startColumn = 0
|
||||||
|
<strong>输出:</strong>6
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/28/out_of_boundary_paths_2.png" style="width: 500px; height: 293px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>m = 1, n = 3, maxMove = 3, startRow = 0, startColumn = 1
|
||||||
|
<strong>输出:</strong>12
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>1 <= m, n <= 50</code></li>
|
||||||
|
<li><code>0 <= maxMove <= 50</code></li>
|
||||||
|
<li><code>0 <= startRow < m</code></li>
|
||||||
|
<li><code>0 <= startColumn < n</code></li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>动态规划</li></div></div>\n<div><li>👍 190</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user