1463. Cherry Pickup II

题目:

Given a rows x cols matrix grid representing a field of cherries. Each cell in grid represents the number of cherries that you can collect.

You have two robots that can collect cherries for you, Robot #1 is located at the top-left corner (0,0) , and Robot #2 is located at the top-right corner (0, cols-1) of the grid.

Return the maximum number of cherries collection using both robots  by following the rules below:

  • From a cell (i,j), robots can move to cell (i+1, j-1) , (i+1, j) or (i+1, j+1).
  • When any robot is passing through a cell, It picks it up all cherries, and the cell becomes an empty cell (0).
  • When both robots stay on the same cell, only one of them takes the cherries.
  • Both robots cannot move outside of the grid at any moment.
  • Both robots should reach the bottom row in the grid.

Example 1:

Input: grid = [[3,1,1],[2,5,1],[1,5,5],[2,1,1]]
Output: 24
Explanation: Path of robot #1 and #2 are described in color green and blue respectively.
Cherries taken by Robot #1, (3 + 2 + 5 + 2) = 12.
Cherries taken by Robot #2, (1 + 5 + 5 + 1) = 12.
Total of cherries: 12 + 12 = 24.

Example 2:

Input: grid = [[1,0,0,0,0,0,1],[2,0,0,0,0,3,0],[2,0,9,0,0,0,0],[0,3,0,5,4,0,0],[1,0,2,3,0,0,6]]
Output: 28
Explanation: Path of robot #1 and #2 are described in color green and blue respectively.
Cherries taken by Robot #1, (1 + 9 + 5 + 2) = 17.
Cherries taken by Robot #2, (1 + 3 + 4 + 3) = 11.
Total of cherries: 17 + 11 = 28.

Example 3:

Input: grid = [[1,0,0,3],[0,0,0,3],[0,0,3,3],[9,0,3,3]]
Output: 22

Example 4:

Input: grid = [[1,1],[1,1]]
Output: 4

Constraints:

  • rows == grid.length
  • cols == grid[i].length
  • 2 <= rows, cols <= 70
  • 0 <= grid[i][j] <= 100 

思路:

三维DP,f[i][j][k]代表了第i行,robot1在第j列,robot2在第k列可以获得的最大的樱桃数。对于初始化,显然第一行是固定的,即f[0][0][n-1]应该等于grid[0][0]+grid[0][n-1],n是列数。我们不用在乎f[0][0][0],因为这种情况并不会发生。这里比较巧妙的地方是用当前的列去判断,如果按照一般思维,我们会在第i行判断i+1行的三个能否取到,有没有超出边界;但是这里因为第0行是固定的,因此我们可以从第1行开始循环,我们对于j和k,给定6个数,即j-1,j,j+1和k,k-1,k+1,只要i-1行的这6个中,各自的三个没有全超出边界,那么就可以取到[i][j][k]这一格。然后DP逻辑的话是如果j和k重叠,那么只要计算上一行的这六个格子+grid[i][j]和原本f[i][j][k]的最大值即可;如果不重叠,那么前面就要再加上gird[i][k],因为两个机器人各自占了不同格子。

代码:

class Solution {
public:
    int cherryPickup(vector<vector<int>>& grid) {
        int m=grid.size(), n=grid[0].size();
        vector<vector<vector<int>>> f(m,vector<vector<int>>(n,vector<int>(n,INT_MIN)));
        f[0][0][n-1]=grid[0][0]+grid[0][n-1];
        int count =0;
        for(int i=1;i<m;i++)
        {
            for(int j=0;j<n;j++)
            {
                for(int offset1=-1;offset1<=1;offset1++)
                {
                    int y1=j+offset1;
                    if(y1<0||y1>=n)
                        continue;
                    for(int k=0;k<n;k++)
                    {
                        for(int offset2=-1;offset2<=1;offset2++)
                        {
                            int y2=k+offset2;
                            if(y2<0|y2>=n)
                                continue;
                            if(j==k)
                                f[i][j][k]=max(f[i][j][k], f[i-1][y1][y2]+grid[i][j]);
                            else 
                                f[i][j][k]=max(f[i][j][k], f[i-1][y1][y2]+grid[i][j]+grid[i][k]);
                            count=max(count,f[i][j][k]);
                        }
                    }
                }
            }
        }
        return count;
    }
};

猜你喜欢

转载自blog.csdn.net/weixin_49991368/article/details/111411702