爪哇 - 通过二维数组中的路径最大总和 [英] Java - Maximum sum in path through a 2D array

查看:160
本文介绍了爪哇 - 通过二维数组中的路径最大总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我有一个去类似的东西到这是一个问题:

Basically I have a problem that goes something similar to this:

有草莓植株的花园再由2D,方形阵列psented $ P $。各厂(每个元素)的数草莓。你开始在阵列的左上角,而你只能向右移动或向下。我需要设计一个递归方法来计算穿过花园和路径,然后再输出哪一个得到最草莓。

There is a garden of strawberry plants represented by a 2D, square array. Each plant(each element) has a number of strawberries. You start at the top left corner of the array, and you can only move to the right or down. I need to design a recursive method to calculate the paths through the garden and then output which one yields the most strawberries.

我想我真的很简单递归问题的理解,但这个问题已经这样在我的头上。我真的不知道从哪里开始或在哪里,只要创建一个递归方法去。

I think I have an understanding of really really simple recursion problems, but this problem has gone way over my head. I'm not really sure where to start or where to go as far as creating a recursive method.

任何涉及到code或帮助我了解这背后的问题的概念,帮助是很大的AP preciated。谢谢你。

Any help related to the code or helping me understand the concept behind this problem is greatly appreciated. Thanks.

推荐答案

就像dasblinkenlight说,最有效的方式做到这一点是使用记忆化或者动态规划技术。我倾向于preFER动态编程,但我会在这里用纯递归。

Like dasblinkenlight said, the most efficient way to do this is using a memoization or dynamic programming technique. I tend to prefer dynamic programming, but I'll use pure recursion here.

周围的答案的答案中心,一个基本的问题:如果我在广场上排r和我的字段列C,我怎么可以评估的路径,从左上角到这里,这样的草莓数量最大化?

The answer centers around the answer to one fundamental question: "If I'm in the square in row r and column c on my field, how can I evaluate the path from the top left to here such that the number of strawberries is maximized?"

要实现的关键是,只有两种方法可以在行r和列c中的情节:要么我可以从上面到达那里,用行r-1和C列的情节,或者我可以到那里从侧面看,用行r和列C-1的情节。在此之后,你只需要确保你知道你的基本情况......这意味着,从根本上,我纯粹是递归的版本是这样的:

The key to realize is that there's only two ways to get in the plot in row r and column c: either I can get there from above, using the plot in row r-1 and column c, or I can get there from the side, using the plot in row r and column c-1. After that, you just need to make sure you know your base cases...which means, fundamentally, my purely recursive version would be something like:

int[][] field;    
int max(int r, int c) {
    //Base case
    if (r == 0 && c == 0) {
        return field[r][c];
    }
    //Assuming a positive number of strawberries in each plot, otherwise this needs
    //to be negative infinity
    int maxTop = -1, maxLeft = -1;
    //We can't come from the top if we're in the top row
    if (r != 0) {
        maxTop = field[r-1][c];
    }
    //Similarly, we can't come from the left if we're in the left column
    if (c != 0) {
        maxLeft = field[r][c-1];
    }
    //Take whichever gives you more and return..
    return Math.max(maxTop, maxLeft) + field[r][c];
}

呼叫MAX(R-1,C-1),以获得答案。请注意,有很多低效率在这里;你会做使用动态规划(我将在下面说明)或记忆化(这已经被定义)要好得多。事情要记住,虽然,是,DP和记忆化技术,这两个都是来自这里使用的递归原理简单更有效的方法。

Call max(r-1, c-1) to get your answer. Notice there's a lot of inefficiency here; you'll do much better by using dynamic programming (which I'll provide below) or memoization (which has already been defined). The thing to remember, though, is that both the DP and memoization techniques are simply more efficient ways that come from the recursive principles used here.

DP:

int maxValue(int[][] field) {
    int r = field.length;
    int c = field[0].length;
    int[][] maxValues = new int[r][c];
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            if (i == 0 && j == 0) {
                maxValues[i][j] = field[i][j];
            } else if (i == 0) {
                maxValues[i][j] = maxValues[i][j-1] + field[i][j];
            } else if (j == 0) {
                maxValues[i][j] = maxValues[i-1][j] + field[i][j];
            } else {
                maxValues[i][j] = Math.max(maxValues[i][j-1], maxValues[i-1][j]) + field[i][j];
            }
        }
    }
    return maxValues[r-1][c-1];
}

在这两种情况下,如果要重新创建实际的路径,只是不停地布尔值与对应的二维表难道我从上方或左边的?如果大多数草莓路径来自上面,把真实的,否则把假的。这可以让你追溯计算后的补丁。

In both cases, if you want to recreate the actual path, just keep a 2D table of booleans that corresponds with "Did I come from above or to the left"? If the most strawberry path comes from above, put true, otherwise put false. That can allow you to retrace the patch after the calculation.

请注意,这是主要的还是递​​归:在每个步骤中,我们回头看我们的previous结果。我们恰好是我们的缓存previous结果,这样我们就不用浪费一堆的工作,而我们在进攻的智能顺序子问题,这样我们可以随时解决这些问题。欲了解更多动态规划,请参见维基百科

Notice that this is still recursive in principal: at each step, we're looking back at our previous results. We just happen to be caching our previous results so we don't waste a bunch of work, and we're attacking the subproblems in an intelligent order so that we can always solve them. For more on dynamic programming, see Wikipedia.

这篇关于爪哇 - 通过二维数组中的路径最大总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆