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的更多相关文章

  1. LeetCode741. Cherry Pickup

    https://leetcode.com/problems/cherry-pickup/description/ In a N x N grid representing a field of che ...

  2. [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 ...

  3. [LeetCode] Cherry Pickup 捡樱桃

    In a N x N grid representing a field of cherries, each cell is one of three possible integers. 0 mea ...

  4. [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 ...

  5. 741. Cherry Pickup

    In a N x N grid representing a field of cherries, each cell is one of three possible integers. 0 mea ...

  6. LeetCode 741. Cherry Pickup

    原题链接在这里:https://leetcode.com/problems/cherry-pickup/ 题目: In a N x N grid representing a field of che ...

  7. 动态规划Dynamic Programming

    动态规划Dynamic Programming code教你做人:DP其实不算是一种算法,而是一种思想/思路,分阶段决策的思路 理解动态规划: 递归与动态规划的联系与区别 -> 记忆化搜索 -& ...

  8. leetcode动态规划题目总结

    Hello everyone, I am a Chinese noob programmer. I have practiced questions on leetcode.com for 2 yea ...

  9. 【LeetCode】动态规划(下篇共39题)

    [600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...

随机推荐

  1. R (Ani Katchova) · Eric

    首先介绍一下Ani Katchova的R教程,然后再继续总结Advanced R. R introduction setwd("path")设置工作路径 mydata<-re ...

  2. C++扬帆远航——9(小学生算数程序)

    /* * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:studentjishu.cpp * 作者:常轩 * 微信公众号 ...

  3. USB小白学习之路(4)HID键盘程序

    HID键盘程序 1. 特别注意 需要特别注意,各个例程中的设备描述符,配置描述符等各种描述符都是已经配置好了的,我们需要做的只是在例程中将代码修改为自己需要的部分即可,一般情况下是不可以串搭配的. 2 ...

  4. 微信小程序从开发至发布的流程

    今天在公司将这几天开发的小程序,进行版本上线,在这里记录下. 首先,将微信开发工具里的代码上传到微信小程序平台里,如下所示 之后,在微信小程序平台进行审核提交,填写相应的信息即可,注意,审核提交分为紧 ...

  5. C# 客户端内存优化分析

    背景概述 C# 开发客户端系统的时候,.net 框架本身就比较消耗内存资源,特别是xp 这种老爷机内存配置不是很高的电脑上运行,所以就需要进行内存上的优化,才能流畅的在哪些低端电脑上运行. 想要对C# ...

  6. node--静态服务器

    1.同步读取文件 const data = fs.readFileSync('./model/mime.json');   // 这里是添加了可以正常链接其他格式文件的服务器 const http = ...

  7. Python——工厂模式

    目录 前言 一.简单工厂 二.工厂方法 抽象工厂 结论 参考 前言 工厂模式,顾名思义就是我们可以通过一个指定的"工厂"获得需要的"产品". 在设计模式中主要用 ...

  8. npm项目创建初始过程详解

    npm install 就是安装模块,npm run dev  就是执行npm script中的命令.当我们执行npm命令的时候,它到哪里去找,这就要说到每个node项目中都有的核心文件package ...

  9. vue+webpack工程环境搭建

    使用Vue-cli脚手架(属于vue全家桶)快速构建一个项目: [1]首先需要安装好node.js; [2]安装webpack,指令$npm install -g webpack; //如果之前有安装 ...

  10. 用反射机制和pandas,实现excel数据的读取以及参数化${arg}的赋值

    反射类:class GetData: index = pd.read_excel(file_name, sheet_name).loc[0, ['index']].values[0] email = ...