原题链接在这里:https://leetcode.com/problems/cherry-pickup/

题目:

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

  • 0 means the cell is empty, so you can pass through;
  • 1 means the cell contains a cherry, that you can pick up and pass through;
  • -1 means the cell contains a thorn that blocks your way.

Your task is to collect maximum number of cherries possible by following the rules below:

  • Starting at the position (0, 0) and reaching (N-1, N-1) by moving right or down through valid path cells (cells with value 0 or 1);
  • After reaching (N-1, N-1), returning to (0, 0) by moving left or up through valid path cells;
  • When passing through a path cell containing a cherry, you pick it up and the cell becomes an empty cell (0);
  • If there is no valid path between (0, 0) and (N-1, N-1), then no cherries can be collected.

Example 1:

Input: grid =
[[0, 1, -1],
[1, 0, -1],
[1, 1, 1]]
Output: 5
Explanation:
The player started at (0, 0) and went down, down, right right to reach (2, 2).
4 cherries were picked up during this single trip, and the matrix becomes [[0,1,-1],[0,0,-1],[0,0,0]].
Then, the player went left, up, up, left to return home, picking up one more cherry.
The total number of cherries picked up is 5, and this is the maximum possible.

Note:

  • grid is an N by N 2D array, with 1 <= N <= 50.
  • Each grid[i][j] is an integer in the set {-1, 0, 1}.
  • It is guaranteed that grid[0][0] and grid[N-1][N-1] are not -1.

题解:

It could be understanding as two people collecting cheeries from (n-1, n-1) to (0, 0).

Two people cooridinates are (x1, y1), (x2, y2).

dfs(x1, y1, x2, y2) returns maximum cheeries collected from two cooridinates to (0, 0).

Thus max(x1, y1, x2, y2) = grid[x1][y1] + grid[x2][y2] + max(dfs(x1-1, y1, x2-1, y2), dfs(x1, y1-1, x2, y2-1), dfs(x1-1, y1, x2, y2-1), dfs(x1, y1-1, x2-1, y2)).

First person could move from top, or left. Second person could do the same. Totally 4 combinations.

And of cource, if x1==y1, which means both people are on the same grid, its cheery can't be collected twice.

y2 = x1+y1-x2. since both of them have same total steps.

Time Complexity: O(n^3).

Space: O(n^3).

AC Java:

 class Solution {
int [][][] dp;
int n;
public int cherryPickup(int[][] grid) {
if(grid == null || grid.length == 0 || grid[0].length == 0){
return 0;
} n = grid.length;
dp = new int[n][n][n];
for(int i = 0; i<n; i++){
for(int j = 0; j<n; j++){
Arrays.fill(dp[i][j], Integer.MIN_VALUE);
}
} return Math.max(0, dfs(grid, n-1, n-1, n-1));
} private int dfs(int [][] grid, int x1, int y1, int x2){
int y2 = x1+y1-x2;
if(x1<0 || y1<0 || x2<0 || y2<0){
return -1;
} if(grid[x1][y1]<0 || grid[x2][y2]<0){
return -1;
} if(dp[x1][y1][x2] != Integer.MIN_VALUE){
return dp[x1][y1][x2];
} if(x1==0 && y1==0){
dp[0][0][0] = grid[0][0];
return grid[0][0];
} int res = Math.max(Math.max(dfs(grid, x1-1, y1, x2-1), dfs(grid, x1, y1-1, x2)), Math.max(dfs(grid, x1-1, y1, x2), dfs(grid, x1, y1-1, x2-1)));
if(res < 0){
dp[x1][y1][x2] = -1;
return -1;
} res += grid[x1][y1];
if(x1 != x2){
res += grid[x2][y2];
} dp[x1][y1][x2] = res;
return res;
}
}

LeetCode 741. Cherry Pickup的更多相关文章

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

  2. 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. LeetCode741. Cherry Pickup

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

  6. Java实现 LeetCode 741 摘樱桃(DFS || 递推 || 传纸条)

    741. 摘樱桃 一个N x N的网格(grid) 代表了一块樱桃地,每个格子由以下三种数字的一种来表示: 0 表示这个格子是空的,所以你可以穿过它. 1 表示这个格子里装着一个樱桃,你可以摘到樱桃然 ...

  7. 动态规划-Cherry Pickup

    2020-02-03 17:46:04 问题描述: 问题求解: 非常好的题目,和two thumb其实非常类似,但是还是有个一点区别,就是本题要求最后要到达(n - 1, n - 1),只有到达了(n ...

  8. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  9. leetcode 学习心得 (4)

    645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...

随机推荐

  1. cannot access org.springframework.core.io.InputStreamSouce

    cannot access org.springframework.core.io.InputStreamSouce错误,把mian路径下main.iml文件备份一下,然后删除该文件,报错就会消失,但 ...

  2. js中Function引用类型中一些常见且有用的方法和属性

    Function类型 函数由于是Function类型的一个实例,所以函数名就是一个指向函数对象的指针,不会与某个函数死死的连接在一起,这也导致了js中没有真正的重载,但好处是,函数对象可以作为另一个函 ...

  3. 51book机票接口对接,吐血整理(含PHP封装代码)

    前言 最近在对接51book的机票接口,遇到了挺多坑,所以整理一份作为记录 机票有两个不同的接口,一个是机票,另一个是保险 一.申请 要接51book的机票,首先是要申请账号,这时候应该是有客户经理跟 ...

  4. LeetCode 1223. 掷骰子模拟 Dice Roll Simulation - Java - DP

    题目链接:1223. 掷骰子模拟 有一个骰子模拟器会每次投掷的时候生成一个 1 到 6 的随机数. 不过我们在使用它时有个约束,就是使得投掷骰子时,连续 掷出数字 i 的次数不能超过 rollMax[ ...

  5. Go 关键字Select

    select select 是Go语言中常用的一个关键字,Linux再也早也引入了这个函数,用来实现非阻塞的一种方式,一个select语句用来选择哪个case中的发送或接收操作可以被立即执行.它类似于 ...

  6. Spark实战电影点评系统(一)

    一.通过RDD实战电影点评系统 日常的数据来源有很多渠道,如网络爬虫.网页埋点.系统日志等.下面的案例中使用的是用户观看电影和点评电影的行为数据,数据来源于网络上的公开数据,共有3个数据文件:uers ...

  7. C# vb .net实现透视阴影特效滤镜

    在.net中,如何简单快捷地实现Photoshop滤镜组中的透视阴影特效效果呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 ...

  8. 1.Javascript实现Symbol

    // 当调用 Symbol 的时候,会采用以下步骤: //1. 如果使用 new ,就报错 //2. 如果 description 是 undefined,让 descString 为 undefin ...

  9. PHP7中方法的弃用

    php7与数据库连接创建函数方法调用: function fun_conn($sql) { $con = mysqli_connect("localhost", "roo ...

  10. UAVCAN DSDL介绍

    原文:http://uavcan.org/Specification/3._Data_structure_description_language/ DSDL:Data structure descr ...