Java 二维数组中的最大和

Java 二维数组中的最大和,java,arrays,multidimensional-array,Java,Arrays,Multidimensional Array,我有一个2d数组(一个矩阵),我需要找到从任何位置开始,向右或向左向下,直到到达终点可以收集的最大和。我必须给出一个迭代解 这是我的密码 static int maxValue(double[][] field, int posR, int posC) { int r = field.length; int c = field[0].length; int sum = 0; double[][] temp = new double[r][c];

我有一个2d数组(一个矩阵),我需要找到从任何位置开始,向右或向左向下,直到到达终点可以收集的最大和。我必须给出一个迭代解

这是我的密码

    static int maxValue(double[][] field, int posR, int posC) {
    int r = field.length;
    int c = field[0].length;
    int sum = 0;
    double[][] temp = new double[r][c];
    for (int i = posR; i < r; i++) {
        for (int j = posC; j < c; j++) {
            if (i == posR && j == posC) {
                temp[i][j] = field[posR][posC];
                posR++; posC++;
            } else if (i == field.length-1) {
                temp[i][j] = field[i][j];
                break;
            } else if (j == field.length-1) {
                temp[i][j] = field[i][j];
                break;
            } else {
                temp[i][j] = Math.max(field[i+1][j-1], field[i+1][j+1]);
            }
        }
    }
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            sum += temp[i][j];
        }

    }
    return sum;
}
静态int-maxValue(双[][]字段,int-posR,int-posC){
int r=字段长度;
int c=字段[0]。长度;
整数和=0;
双精度[]温度=新双精度[r][c];
for(int i=posR;i

}一般来说,这是一个动态规划问题

逻辑为(第0行为左上角)

现在,您会注意到这是一个递归定义,因此实际上不需要for循环

所以,在轻微的伪代码

int maxValue(double[][] arr, int row, int col) {
    if (outOfBounds) return 0; 

    int value = arr[row][col];
    int leftDiag = maxValue(arr, row +1,col - 1);
    int rightDiag = maxValue(arr, row + 1, col + 1);
    return value + Math.max(leftDiag, rightDiag);
} 

从任何位置开始,您都应该能够调用该方法,它将递归地求和值并返回最大路径

这里有一个迭代解决方案的想法:你可以向下滑动一行,在底部你会发现最大值听起来很疯狂?让我用代码解释一下:

public static double maxZicZacSum(double[][] matrix) {
  double[] row = matrix[0]; // assign first row
  int n = row.length;
  for (int i = 1; i < matrix.length; i++) { // for each row, except the first
    double[] nextRow = new double[n];
    // special cases (left and right edge)
    nextRow[0] = row[1] <= 0 ? matrix[i][0] : row[1] + matrix[i][0];
    nextRow[n - 1] = row[n - 2] <= 0 ? matrix[i][n - 1] : row[n - 2] + matrix[i][n - 1];
    for (int j = 1; j < n - 1; j++) { // for each column except the edges
      double d = Math.max(row[j - 1], row[j + 1]); // which cell above is better?
      // if d is > 0, then the sum is also better, otherwise use (i,j) as new start
      nextRow[j] = d <= 0 ? matrix[i][j] : d + matrix[i][j];
    }
    row = nextRow; // finally assign nextRow to row for the next iteration
  }
  // the highest value in row is now the max sum
  double max = row[0];
  for (int i = 1; i < n; i++)
    if (row[i] > max)
      max = row[i];
  return max;
}
公共静态双maxZicZacSum(双[]矩阵){
double[]行=矩阵[0];//分配第一行
int n=行长度;
对于(int i=1;inextRow[0]=行[1]调试它,看看你在哪里遇到了问题@Sanjeev,它可以工作,但问题是它没有给出正确的答案。代码背后的想法是什么?你似乎是从一个区域而不是一条线取和。@maraca,也许这就是问题所在。你能建议如何修复代码以从一条线取和吗?非常感谢!我不知道你想从一个给定的矩阵中得到什么样的和,你能举一个矩阵的例子并解释这个问题吗?我不允许用递归来解决这个问题。他们要求我给出一个迭代的解决方案:(谢谢你的回答!它递归地迭代;)不过,我明白你的意思。我从来都不明白为什么赋值会让自己变得更难,而它们需要做。我赋值的另一部分是用递归解决问题。但这一点让我很难受:DI考虑了一点,你最好编写一些迭代方法,比如
maxLeft()
maxRight()
当迭代超出边界时结束迭代。棘手的部分是“之字形”-向下移动数组,谢谢!它工作得很好。你知道如何使它适用于任何起始位置,而不仅仅是从顶部开始吗?这适用于矩阵中的任何位置。你可以看到,如果更好的话,可以选择一个新的起始点……我错了,不需要复制第一行,因为这些值是指定给下一步。谢谢你接受,错误不是一个,因为你必须从任何一个起始位置走到终点,对吗?你不能在某个地方停下来。那么解决方案是正确的。没关系。它不必与负数一起工作。但我想还有另一个问题。我有一个递归解决方案解决这个问题,两个解决方案给出的结果不同(很难说,你可以用递归代码发布一个新问题;-)现在在写了一个迭代解决方案之后,我认为我的递归解决方案是“假的”。任何循环都可以变成递归。。。
public static double maxZicZacSum(double[][] matrix) {
  double[] row = matrix[0]; // assign first row
  int n = row.length;
  for (int i = 1; i < matrix.length; i++) { // for each row, except the first
    double[] nextRow = new double[n];
    // special cases (left and right edge)
    nextRow[0] = row[1] <= 0 ? matrix[i][0] : row[1] + matrix[i][0];
    nextRow[n - 1] = row[n - 2] <= 0 ? matrix[i][n - 1] : row[n - 2] + matrix[i][n - 1];
    for (int j = 1; j < n - 1; j++) { // for each column except the edges
      double d = Math.max(row[j - 1], row[j + 1]); // which cell above is better?
      // if d is > 0, then the sum is also better, otherwise use (i,j) as new start
      nextRow[j] = d <= 0 ? matrix[i][j] : d + matrix[i][j];
    }
    row = nextRow; // finally assign nextRow to row for the next iteration
  }
  // the highest value in row is now the max sum
  double max = row[0];
  for (int i = 1; i < n; i++)
    if (row[i] > max)
      max = row[i];
  return max;
}