当前位置:网站首页>最大路径和问题(摘樱桃问题)
最大路径和问题(摘樱桃问题)
2022-06-29 00:46:00 【GreyZeng】
最大路径和问题(摘樱桃问题)
作者:Grey
原文地址: 最大路径和问题(摘樱桃问题)
题目链接
主要思路
本题的难点在于尝试,如何模拟一来一回的情况,我们可以这样做,定义两个小人,两个人都从(0,0)位置出发,到右下角位置,每人同时选择不同的下一步,如果两个小人跳到了同一个位置,只计算一份。由于两个小人是同时走,而且每次只走一步,所以,两个小人一定是同时到达右下角的,两个小人一路收集的樱桃数量,就是一来一回收集的数量。
我们可以写出第一个尝试版本,定义递归函数
int p(int[][] matrix, int m, int n, int x1, int y1, int x2, int y2)
递归函数的含义表示,第一个小人从(x1,y1)开始到右下角,第二个小人从(x2,y2)开始到右下角,获取到的最大值是多少。所以p(matrix, m, n, 0, 0, 0, 0)就是我们需要的答案。
接下来考虑base case,即:两个小人都到达了右下角,由于题目已经说了,右下角的值一定不是-1,所以,可以获得一份樱桃数量。
if (x1 == m - 1 && y1 == n - 1) {
// 已经到了最右下角了
// 隐含条件:另外一个点也一定到达右下角
// 获得一份樱桃数
return matrix[x1][y1];
}
接下来就是普遍尝试,分别可以分成如下四种情况:
情况1:第一个小人往下,第二个小人往右。
情况2:第一个小人往下,第二个小人往下。
情况3:第一个小人往右,第二个小人往下。
情况4:第一个小人往右,第二个小人往右。
但是在走上述任何分支的时候,记得要满足两个条件
第一个条件,不能越界
第二个条件,下一个要走的位置不能是-1,因为题目说到,-1是不能走的位置。
int next = -1;
// 下,下
if (x1 + 1 < m && x2 + 1 < m && matrix[x1 + 1][y1] != -1 && matrix[x2 + 1][y2] != -1) {
next = Math.max(p(matrix, m, n, x1 + 1, y1, x2 + 1, y2), next);
}
// 下,右
if (x1 + 1 < m && y2 + 1 < n && matrix[x1 + 1][y1] != -1 && matrix[x2][y2 + 1] != -1) {
next = Math.max(p(matrix, m, n, x1 + 1, y1, x2, y2 + 1), next);
}
// 右,下
if (y1 + 1 < n && x2 + 1 < m && matrix[x1][y1 + 1] != -1 && matrix[x2 + 1][y2] != -1) {
next = Math.max(p(matrix, m, n, x1, y1 + 1, x2 + 1, y2), next);
}
// 右,右
if (y1 + 1 < n && y2 + 1 < m && matrix[x1][y1 + 1] != -1 && matrix[x2][y2 + 1] != -1) {
next = Math.max(p(matrix, m, n, x1, y1 + 1, x2, y2 + 1), next);
}
经过上述四个分支,如果next的值还是-1,说明无路可走,返回-1
if (next == -1) {
return -1;
}
如果next不等于-1,说明有可以走的路径,那么继续判断两个小人是否在同一位置,如果在同一个位置,则只收集一份樱桃,如果不在同一个位置,收集两个小人所在位置的樱桃之和。
if (x1 == x2) {
// 到达同一个位置,只取一个值
return next + matrix[x1][y1];
}
return next + matrix[x1][y1] + matrix[x2][y2];
注:判断同一个位置,只需要一个维度上的坐标相等即可。
完整代码
public static int cherryPickup1(int[][] matrix) {
return Math.max(0, p(matrix, matrix.length, matrix[0].length, 0, 0, 0, 0));
}
// 定义两个小人,两个人都从(0,0)位置出发,到右下角位置,每人同时选择不同的下一步,如果两个小人跳到了同一个位置,只计算一份。
public static int p(int[][] matrix, int m, int n, int x1, int y1, int x2, int y2) {
if (x1 == m - 1 && y1 == n - 1) {
// 已经到了最右下角了
// 隐含条件:另外一个点也一定到达右下角
// 获得一份樱桃数
return matrix[x1][y1];
}
int next = -1;
// 下,下
if (x1 + 1 < m && x2 + 1 < m && matrix[x1 + 1][y1] != -1 && matrix[x2 + 1][y2] != -1) {
next = Math.max(p(matrix, m, n, x1 + 1, y1, x2 + 1, y2), next);
}
// 下,右
if (x1 + 1 < m && y2 + 1 < n && matrix[x1 + 1][y1] != -1 && matrix[x2][y2 + 1] != -1) {
next = Math.max(p(matrix, m, n, x1 + 1, y1, x2, y2 + 1), next);
}
// 右,下
if (y1 + 1 < n && x2 + 1 < m && matrix[x1][y1 + 1] != -1 && matrix[x2 + 1][y2] != -1) {
next = Math.max(p(matrix, m, n, x1, y1 + 1, x2 + 1, y2), next);
}
// 右,右
if (y1 + 1 < n && y2 + 1 < m && matrix[x1][y1 + 1] != -1 && matrix[x2][y2 + 1] != -1) {
next = Math.max(p(matrix, m, n, x1, y1 + 1, x2, y2 + 1), next);
}
if (next == -1) {
return -1;
}
if (x1 == x2) {
// 到达同一个位置,只取一个值
return next + matrix[x1][y1];
}
return next + matrix[x1][y1] + matrix[x2][y2];
}
这个解法在 LeetCode 上直接超时。
在上述尝试的基础上,我们可以做进一步的优化,递归函数现在是四个可变参数,根据我们的设计,其实可以得到如下公式
x1 + y1 = x2 + y2
那么我们可以将递归函数省略一个参数y2,因为
y2 = x1 + y1 - x2
上述递归方法我们可以修改为
// 省略y2参数
public static int p(int[][] matrix, int m, int n, int x1, int y1, int x2) {
if (x1 == m - 1 && y1 == n - 1) {
// 已经到了最右下角了
// 隐含条件:另外一个点也一定到达右下角
// 获得一份樱桃数
return matrix[x1][y1];
}
int next = -1;
// 下,下
if (x1 + 1 < m && x2 + 1 < m && matrix[x1 + 1][y1] != -1 && matrix[x2 + 1][getY2(x1, y1, x2)] != -1) {
next = Math.max(p(matrix, m, n, x1 + 1, y1, x2 + 1), next);
}
// 下,右
if (x1 + 1 < m && getY2(x1, y1, x2) + 1 < n && matrix[x1 + 1][y1] != -1 && matrix[x2][getY2(x1, y1, x2) + 1] != -1) {
next = Math.max(p(matrix, m, n, x1 + 1, y1, x2), next);
}
// 右,下
if (y1 + 1 < n && x2 + 1 < m && matrix[x1][y1 + 1] != -1 && matrix[x2 + 1][getY2(x1, y1, x2)] != -1) {
next = Math.max(p(matrix, m, n, x1, y1 + 1, x2 + 1), next);
}
// 右,右
if (y1 + 1 < n && getY2(x1, y1, x2) + 1 < m && matrix[x1][y1 + 1] != -1 && matrix[x2][getY2(x1, y1, x2) + 1] != -1) {
next = Math.max(p(matrix, m, n, x1, y1 + 1, x2), next);
}
if (next == -1) {
return -1;
}
if (x1 == x2) {
// 到达同一个位置,只取一个值
return next + matrix[x1][y1];
}
return next + matrix[x1][y1] + matrix[x2][getY2(x1, y1, x2)];
}
public static int getY2(int x1, int y1, int x2) {
return x1 + y1 - x2;
}
我们可以做进一步的优化,由于三个可变参数的范围分别是[0...m-1],[0...n-1],[0...m-1],那么我们可以设置三维dp,将所有递归结果缓存起来
int[][][] dp = new int[m][n][m];
三维dp的所有位置初始值设置为Integer.MIN_VALUE,在递归方法中,将这个三维表作为缓存带入参数中,每次递归过程中,先从dp表中拿值,如果值不为Integer.MIN_VALUE,说明这个过程算过,直接取值即可。
public static int p(int[][] matrix, int m, int n, int x1, int y1, int x2, int[][][] dp) {
if (dp[x1][y1][x2] != Integer.MIN_VALUE) {
// 说明这个过程曾经算过,直接从缓存中取值即可
return dp[x1][y1][x2];
}
}
然后在每次递归过程中,将答案存入缓存,完整代码如下
// 动态规划
// 三维表
public static int cherryPickup(int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
int[][][] dp = new int[m][n][m];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < m; k++) {
// 初始值设置为Integer.MIN_VALUE,只要不等于Integer.MIN_VALUE,就说明递归过程已经算过了
dp[i][j][k] = Integer.MIN_VALUE;
}
}
}
return Math.max(0, p(matrix, m, n, 0, 0, 0, dp));
}
// 定义两个小人,两个人都从(0,0)位置出发,到右下角位置,每人同时选择不同的下一步,如果两个小人跳到了同一个位置,只计算一份。
public static int p(int[][] matrix, int m, int n, int x1, int y1, int x2, int[][][] dp) {
if (dp[x1][y1][x2] != Integer.MIN_VALUE) {
// 说明这个过程曾经算过,直接从缓存中取值即可
return dp[x1][y1][x2];
}
if (x1 == m - 1 && y1 == n - 1) {
// 已经到了最右下角了
// 隐含条件:另外一个点也一定到达右下角
// 获得一份樱桃数
dp[x1][y1][x2] = matrix[x1][y1];
return matrix[x1][y1];
}
int next = -1;
// 下,下
if (x1 + 1 < m && x2 + 1 < m && matrix[x1 + 1][y1] != -1 && matrix[x2 + 1][getY2(x1, y1, x2)] != -1) {
next = Math.max(p(matrix, m, n, x1 + 1, y1, x2 + 1, dp), next);
}
// 下,右
if (x1 + 1 < m && getY2(x1, y1, x2) + 1 < n && matrix[x1 + 1][y1] != -1 && matrix[x2][getY2(x1, y1, x2) + 1] != -1) {
next = Math.max(p(matrix, m, n, x1 + 1, y1, x2, dp), next);
}
// 右,下
if (y1 + 1 < n && x2 + 1 < m && matrix[x1][y1 + 1] != -1 && matrix[x2 + 1][getY2(x1, y1, x2)] != -1) {
next = Math.max(p(matrix, m, n, x1, y1 + 1, x2 + 1, dp), next);
}
// 右,右
if (y1 + 1 < n && getY2(x1, y1, x2) + 1 < m && matrix[x1][y1 + 1] != -1 && matrix[x2][getY2(x1, y1, x2) + 1] != -1) {
next = Math.max(p(matrix, m, n, x1, y1 + 1, x2, dp), next);
}
if (next == -1) {
dp[x1][y1][x2] = -1;
return -1;
}
if (x1 == x2) {
// 到达同一个位置,只取一个值
// 将答案存入缓存
dp[x1][y1][x2] = next + matrix[x1][y1];
return dp[x1][y1][x2];
}
// 将答案存入缓存
dp[x1][y1][x2] = next + matrix[x1][y1] + matrix[x2][getY2(x1, y1, x2)];
return dp[x1][y1][x2];
}
public static int getY2(int x1, int y1, int x2) {
return x1 + y1 - x2;
}
时间复杂度O(m*n*m),LeetCode 中直接AC。
更多
边栏推荐
- Is pension insurance a financial product? Where is the expected return?
- [staff] pedal mark (step on pedal ped mark | release pedal * mark | corresponding pedal command in MIDI | continuous control signal | switch control signal)
- Install MySQL on Windows platform (with Navicat premium 12 "using" tutorial)
- 11. target segmentation
- Realization of beauty system with MATLAB
- Introduction to four MySQL engines
- Baidu online disk login verification prompt: unable to access this page, or the QR code display fails, the pop-up window shows: unable to access this page, ensure the web address....
- [SV basics] some usage of queue
- Drawing ECG curve with WPF
- Haskell configuring vs code development environment (june2022)
猜你喜欢

光纤滑环价格过高的原因
![[MCU club] design of classroom number detection based on MCU [physical design]](/img/cf/65c1bdbb45afcc6db265a79c25a3ae.jpg)
[MCU club] design of classroom number detection based on MCU [physical design]

What is redis

Daily question 1: the number of numbers in the array

基于.NetCore开发博客项目 StarBlog - (13) 加入友情链接功能

Blazor University (34) forms - get form status

Document management.

Blazor University (34)表单 —— 获得表单状态
![[MCU club] design of blind water cup based on MCU [physical design]](/img/06/93d7a8fd97cdccbc639d2a95b10826.jpg)
[MCU club] design of blind water cup based on MCU [physical design]

Single machine multi instance MySQL master-slave replication
随机推荐
大型网站架构基础之笔记
Difference between applying for trademark in the name of individual and company
Leetcode daily question: implementing strstr()
畢業三年的25歲碼農總結
Analysis Framework -- establishment of user experience measurement data system
Ensemble de données sur les visages masqués et méthode de génération des visages masqués
Is the fund reliable and safe
ES6:let、const、箭头函数
Drawing ECG curve with WPF
运营级智慧校园系统源码 智慧校园小程序源码+电子班牌+人脸识别系统
Daily English articles, reading accumulation
Sampling with VerilogA module
Mask wearing face data set and mask wearing face generation method
基于.NetCore开发博客项目 StarBlog - (13) 加入友情链接功能
Reference materials in the process of using Excel
be based on. NETCORE development blog project starblog - (13) add friendship link function
Operation level smart campus system source code smart campus applet source code + electronic class card + face recognition system
Blazor University (34) forms - get form status
Chrome浏览器的基本使用
如果你会玩这4个自媒体运营工具,副业收入6000+很轻松