Last updated
Was this helpful?
Last updated
Was this helpful?
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:
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.
N阶方阵上0代表可通过,1代表有果子,-1代表不可通过,从(0, 0)开始可每次向下或向右走到(N - 1, N-1),再每步向上或向左走回到(0,0),问最多能拿到多少果子。往回走可以看作是另一条正着走的path。但用两遍max path sum的谈心法是错误的,因为第二遍会取决于第一遍时的结果,而第一遍时最优解不一定是全局最优。既然问题出在了第二遍时丢失了第一遍时的信息,那把两条paths看作是两个人同时独立走,并在它俩相遇时只拿一次果子:把两条paths的位置信息encode进DP状态,就避免了第二条path错误的只关联在第一条path的最优解上。action分别为第一个人下/右与第二个人下右四种,dp[x1][y1][x2]表示从(x1, y1, x2, x1 + y1 - x2)出发到(N-1, N-1)能拿到最多果子数。
思路参考自。
https://leetcode.com/problems/cherry-pickup/