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) 可以分别走上面和左边,那么就有四个点可以走,那么就有4种组合。(1,3), (1,4), (2,3),( 2,4)

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

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

这题注意,两个同时在一个点的时候,只能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[][][] cache = 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++) {
                    cache[i][j][k] = Integer.MIN_VALUE;
                }
            }
        }
        return Math.max(0, dfs(grid, cache, n - 1, n - 1, n - 1));
    }
    
    // top down, memorization + DFS;
    private int dfs(int[][] grid, int[][][] cache, int x1, int y1, int x2) {
        int y2 = x1 + y1 - x2;
        if(x1 < 0 || y1 < 0 || x2 < 0 || y2 < 0) {
            return -1;
        }
        if(cache[x1][y1][x2] != Integer.MIN_VALUE) {
            return cache[x1][y1][x2];
        }
        // grid[i][j] == -1; not reachable;
        if(grid[x1][y1] < 0 || grid[x2][y2] < 0) {
            cache[x1][y1][x2] = -1;
            return -1;
        }
        if(x1 == 0 && y1 == 0) { // arrive at the same time to [0,0]
            return grid[0][0];
        }
        int ans = Math.max(
        Math.max(dfs(grid, cache, x1, y1-1, x2), dfs(grid, cache, x1,y1-1, x2 -1)),   
        Math.max(dfs(grid, cache, x1-1, y1, x2), dfs(grid, cache, x1-1, y1, x2-1)));
        if(ans < 0) {
            cache[x1][y1][x2] = -1;
            return -1;
        }
        // ans can only be add once, 1 person pick cherry;
        ans += grid[x1][y1];
        // add another person's cherry, pick 2nd people cherry;
        if(x1 != x2 && y1 != y2) {
            ans += grid[x2][y2];
        }
        cache[x1][y1][x2] = ans;
        return ans;
    }
}
发布了663 篇原创文章 · 获赞 13 · 访问量 18万+

猜你喜欢

转载自blog.csdn.net/u013325815/article/details/105156290