动态规划-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 ...
随机推荐
- Errors running builder JavaScript Validator
问题: 解决方法: 方法一. 选择对应项目—-右键Properties—-Builders—-取消“JavaScript Validator”的勾就OK了 方法二. 找到“.project”文件,找到 ...
- C++走向远洋——29(长方柱类)
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:长方柱类.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- 有了这个开源 Java 项目,开发出炫酷的小游戏好像不难?
本文适合有 Java 基础知识的人群,跟着本文可学习和运行 Java 的游戏. 本文作者:HelloGitHub-秦人 HelloGitHub 推出的<讲解开源项目>系列,今天给大家带来一 ...
- 使用 custom element 创建自定义元素
很早我们就可以在 HTML 文档中写 <custome-element></custom-element> 这样的自定义名称标签.但是浏览器对于不认识的标签一律当成一个普通的行 ...
- Yuchuan_Linux_C 编程之十 进程及进程控制
一.整体大纲 二.基础知识 1. 进程相关概念 1)程序和进程 程序,是指编译好的二进制文件,在磁盘上,不占用系统资源(cpu.内存.打开的文件.设备.锁....) 进程,是一个抽象的概念,与 ...
- jsp内置对象(三)-----response对象
response对象 response对象包含了响应客户端请求的有关信息,但在JSP中很少直接用到它.他是HttpServletResponse类的实例,response对象具有页面作用域,即访问一 ...
- js中所有函数的参数(按值和按引用)都是按值传递的,怎么理解?
我觉着我可能对这块有点误解,所以单独开个博说下自己的理解,当然是研究后的正解了. 1,参数传递是基本类型,看个例子: function addTen(num){ num += 10; return n ...
- AB实验的高端玩法系列4- 实验渗透低?用户未被触达?CACE/LATE
CACE全称Compiler Average Casual Effect或者Local Average Treatment Effect.在观测数据中的应用需要和Instrument Variable ...
- 浅谈Spring框架
一.Spring简介 Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的.框架的主要优势之一就是其分层架构, 分层架构允许您选择使用哪一个组件,同时为 J2EE 应用程序开发提供集 ...
- 2020 webstorm 最新激活方式 有效期2021年11月 可用
MIIElT25XE-eyJsaWNlbnNlSWQiOiJPUVQzT0oyNVhFIiwibGljZW5zZWVOYW1lIjoi5rC45LmF5r+A5rS7IGlkZWEubWVkZW1pb ...