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

思路:这题比较迷惑,从左往右,然后再返回到0,其实可以理解成有两个人同时从(n-1, n-1)往(0,0)走,两个人同步。

这样就有(x1, y1) (x2, y2),同步也就是x1 + y1 = x2 + y2;,那么y2坐标即使可以= x1+y1 - x2,那么dp的状态就是:在x1,y1,x2,y2的时候,可以得到的最大的cherry是多少。那么这题可以用dfs + memory search来解决;

(x1,y1), (x2, y2) 可以分别走上面和左边,那么就有四个点可以走,

                   2   (x1-1,y1)                     3  (x2 -1, y2)

1  (x1, y1-1)    (x1,y1)     4 (x2, y2 -1)   (x2, y2)

那么就有4种组合。(1,3), (1,4), (2,3),( 2,4) 这题注意,两个同时在一个点的时候,只能1个人取,两个人分别的时候,可以分别取;

class Solution {
    public int cherryPickup(int[][] grid) {
        if(grid == null || grid.length == 0 || grid[0].length == 0) {
            return 0;
        }
        int n = grid.length;
        int[][][] dp = new int[n][n][n];
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                for(int k = 0; k < n; k++) {
                    dp[i][j][k] = Integer.MIN_VALUE;
                }
            }
        }
        
        return Math.max(0, dfs(grid, dp, n - 1, n - 1, n - 1));
    }
    
    private int dfs(int[][] grid, int[][][] dp, int x1, int y1, int x2) {
        int y2 = x1 + y1 - x2;
        if(x1 < 0 || y1 < 0 || x2 < 0 || y2 < 0) {
            return -1;
        }
        if(dp[x1][y1][x2] != Integer.MIN_VALUE) {
            return dp[x1][y1][x2];
        }
        if(x1 == 0 && y1 == 0) {
            return grid[0][0];
        }
        if(grid[x1][y1] == -1 || grid[x2][y2] == -1) {
            dp[x1][y1][x2] = -1;
            return -1;
        }
        int value = 0;
        value = Math.max(
            Math.max(dfs(grid, dp, x1, y1 - 1, x2), dfs(grid, dp, x1, y1 - 1, x2 - 1)),
            Math.max(dfs(grid, dp, x1 - 1, y1, x2), dfs(grid, dp, x1 - 1, y1, x2 - 1)));
        // 从上面四种状态来到当前格子;
        if(value < 0) {
            dp[x1][y1][x2] = -1; // 记住,状态一定要记住,然后返回;
            return -1;
        }
        value += grid[x1][y1];
        if(x1 != x2 && y1 != y2) {// 如果两个坐标点不一样,则可以加入两个value;
            value += grid[x2][y2];
        }
        dp[x1][y1][x2] = value;
        return dp[x1][y1][x2];
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值