不会做,想到死都想不出来,先CV再说,哈哈哈
class Solution {
public int cherryPickup(int[][] grid) {
int n = grid.length;
int[][][] f = new int[2 * n - 1][n ][n];
// 初始化数组为最小值,避免没有的情况
for (int i = 0; i < n * 2 - 1; i++){
for (int j = 0; j < n; j++){
Arrays.fill(f[i][j], Integer.MIN_VALUE);
}
}
// 初始化
f[0][0][0] = grid[0][0];
for (int k = 1; k < 2 * n - 1; k++){
for (int x1 = Math.max(k - n + 1, 0); x1 <= Math.min(k, n - 1); x1++){
int y1 = k - x1;
if (grid[x1][y1] == -1){
continue;
}
for (int x2 = x1; x2 <= Math.min(k, n - 1); x2++){
int y2 = k - x2;
if (grid[x2][y2] == -1){
continue;
}
int res = f[k - 1][x1][x2];// 都向右
if (x1 > 0){
res = Math.max(res, f[k - 1][x1 - 1][x2]);
}
if (x2 > 0){
res = Math.max(res, f[k - 1][x1][x2 - 1]);
}
if (x1 > 0 && x2 > 0){
res = Math.max(res, f[k - 1][x1 - 1][x2 - 1]);
}
res += grid[x1][y1];
if (x1 != x2){
res += grid[x2][y2];
}
f[k][x1][x2] = res;
}
}
}
return Math.max(f[n * 2 - 2][n - 1][n - 1], 0);
}
}