动态规划-Cherry Pickup
2020-02-03 17:46:04
问题描述:


问题求解:
非常好的题目,和two thumb其实非常类似,但是还是有个一点区别,就是本题要求最后要到达(n - 1, n - 1),只有到达了(n - 1, n - 1)才算是有效解,two thumb是一定会有解的,所以不用加特别判断。
也是一种路径规划类的题目,难点依然是状态的表示,我们这里使用的p1,p2的坐标位置作为状态。
另外,还需要注意的是在超界的时候,我们需要返回的是Integer.MIN_VALUE,这样就可以规避掉一些中间节点到不了终点的情况。
int[][][] dp = new int[51][51][51];
public int cherryPickup(int[][] grid) {
int n = grid.length;
for (int i = 0; i <= 50; i++) {
for (int j = 0; j <= 50; j++) {
Arrays.fill(dp[i][j], -1);
}
}
int res = dfs(grid, 0, 0, 0);
return dp[n - 1][n - 1][n - 1] == -1 ? 0 : res;
}
private int dfs(int[][] grid, int x1, int y1, int x2) {
int n = grid.length;
int y2 = x1 + y1 - x2;
if (x1 >= n || y1 >= n || x2 >= n || y2 >= n) return Integer.MIN_VALUE;
if (dp[x1][y1][x2] != -1) return dp[x1][y1][x2];
else if (x1 == n - 1 && y1 == n - 1) dp[x1][y1][x2] = grid[n - 1][n - 1];
else if (grid[x1][y1] == -1 || grid[x2][y2] == -1) dp[x1][y1][x2] = Integer.MIN_VALUE;
else {
int curr = x1 == x2 && y1 == y2 ? grid[x1][y1] : grid[x1][y1] + grid[x2][y2];
dp[x1][y1][x2] = curr + Math.max(Math.max(dfs(grid, x1 + 1, y1, x2 + 1), dfs(grid, x1 + 1, y1, x2)), Math.max(dfs(grid, x1, y1 + 1, x2 + 1), dfs(grid, x1, y1 + 1, x2)));
}
return dp[x1][y1][x2];
}
动态规划-Cherry Pickup的更多相关文章
- LeetCode741. Cherry Pickup
https://leetcode.com/problems/cherry-pickup/description/ In a N x N grid representing a field of che ...
- [LeetCode] 741. Cherry Pickup 捡樱桃
In a N x N grid representing a field of cherries, each cell is one of three possible integers. 0 mea ...
- [LeetCode] Cherry Pickup 捡樱桃
In a N x N grid representing a field of cherries, each cell is one of three possible integers. 0 mea ...
- [Swift]LeetCode741. 摘樱桃 | Cherry Pickup
In a N x N grid representing a field of cherries, each cell is one of three possible integers. 0 mea ...
- 741. Cherry Pickup
In a N x N grid representing a field of cherries, each cell is one of three possible integers. 0 mea ...
- LeetCode 741. Cherry Pickup
原题链接在这里:https://leetcode.com/problems/cherry-pickup/ 题目: In a N x N grid representing a field of che ...
- 动态规划Dynamic Programming
动态规划Dynamic Programming code教你做人:DP其实不算是一种算法,而是一种思想/思路,分阶段决策的思路 理解动态规划: 递归与动态规划的联系与区别 -> 记忆化搜索 -& ...
- leetcode动态规划题目总结
Hello everyone, I am a Chinese noob programmer. I have practiced questions on leetcode.com for 2 yea ...
- 【LeetCode】动态规划(下篇共39题)
[600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...
随机推荐
- Java的锁机制--synchronsized关键字
引言 高并发环境下,多线程可能需要同时访问一个资源,并交替执行非原子性的操作,很容易出现最终结果与期望值相违背的情况,或者直接引发程序错误. 举个简单示例,存在一个初始静态变量count=0,两个线程 ...
- nginx 502排错
线上一台机器(该论坛所在机器)近期频繁出现502,每100次访问就会出现10次,这频率也太高了.于是开始了我的502排查之旅 ps aux |grep -c php 结果为200 netstat -a ...
- jquery.form.js笔记
由于项目的原因,需要异步上传文件,网上找了找,很多都是用jquery.form插件的,于是乎找资料,调代码,做点小笔记. 官方资料:http://www.malsup.com/jquery/form/ ...
- 不要写很酷但同事看不懂的Java代码
你好呀,我是沉默王二,一个和黄家驹一样身高,和刘德华一样颜值的程序员.为了提高 Java 编程的技艺,我最近在 GitHub 上学习一些高手编写的代码.下面这一行代码(出自大牛之手)据说可以征服你的朋 ...
- 神奇的background——绘制图形
相信大家在平时工作中少不了会被要求在某些元添加一些特殊的背景图片,这时候通常就拿起ps就是切切切.不说这种方式麻烦,有ui给你切好的情况已经不错,没有的就有自己动手.还可能有需要切一整张超大图的情况. ...
- CSS3实现一个旋转的花朵
要效果图如下: 实现原理:其实很简单,就是中间的圆圈定位在中间,其他的6个圆圈,进行不同的绝对定位,然后进行旋转!代码: <!DOCTYPE html> <html lang=&qu ...
- 总结:利用asp.net core日志进行生产环境下的错误排查(asp.net core version 2.2,用IIS做服务器)
概述 调试asp.net core程序时,在输出窗口中,在输出来源选择“调试”或“xxx-ASP.NET Core Web服务器”时,可以看到类似“info:Microsoft.AspNetCore. ...
- mongoose .find().limit()返回undefined
当我们使用mongoose的find方法查询数据库的同时,可以使用limit方法来指定返回数据的条数. limit方法基本语法如下所示: >db.COLLECTION_NAME.find().l ...
- LoadRunner 11破解方法:
LoadRunner 11破解方法: 请严格安装顺序操作! a.用LR8.0中的mlr5lprg.dll.lm70.dll覆盖LR11安装目录下“bin”文件夹中的对应文件: b.运行deleteli ...
- Jira使用说明文档
1 建立项目 1.1 权限归属 Jira系统管理员 1.2 执行内容 建立项目.工作流分配调整.制定项目负责人及默认经办人 1.3 建立项目过程 登录使用Jira系统管理员 ...