Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java递归优化_Java_Optimization_Recursion_Max_Brute Force - Fatal编程技术网

Java递归优化

Java递归优化,java,optimization,recursion,max,brute-force,Java,Optimization,Recursion,Max,Brute Force,我必须使用蛮力方法递归地解决以下问题: 假设两个人,A和B,有偶数个有序的盒子,每个盒子都有一个给定的值。例如,box={5,3,7,10}。他们需要以这种方式在他们之间分割盒子:人A选择集合中的第一个或最后一个盒子,然后人B选择相同的盒子,依此类推,直到没有盒子剩下 人A想知道,他能得到的最大价值是什么,总的来说,记住每个回合人B也可以做出两个选择。换句话说,问题是提出一种算法,模拟两个人的所有选择,考虑到他们的目标都是在长期内获得最大价值 所以,现在我有这个: public static i

我必须使用蛮力方法递归地解决以下问题:

假设两个人,A和B,有偶数个有序的盒子,每个盒子都有一个给定的值。例如,box={5,3,7,10}。他们需要以这种方式在他们之间分割盒子:人A选择集合中的第一个或最后一个盒子,然后人B选择相同的盒子,依此类推,直到没有盒子剩下

人A想知道,他能得到的最大价值是什么,总的来说,记住每个回合人B也可以做出两个选择。换句话说,问题是提出一种算法,模拟两个人的所有选择,考虑到他们的目标都是在长期内获得最大价值

所以,现在我有这个:

public static int maxValue(ArrayList <Integer> boxes, int choice, int person){
    int value;

    //Stop condition - if there are no more boxes, return 0
    if (boxes.isEmpty())
        return 0;

    if (choice == 0) //Person chose the first box in the sequence
        value = boxes.remove(0);
    else //Person chose the last box in the sequence
        value = boxes.remove(boxes.size() - 1);

    //Person A makes a choice, checking which one works best in the long run
    if (person == 1)
        return (value + max(maxValue(boxes, 0, 2), maxValue(boxes, 1, 2)));

    //Person B makes a choice, checking which one works best in the long run
    else
        return (value + max(maxValue(boxes, 0, 1), maxValue(boxes, 1, 1)));
}
然后将所有值相加。我想这是因为函数是由人A调用的,它引用了MaxValueBox中的人B,0,2,MaxValueBox,1,2,反之亦然,也因为停止条件,如果我稍微更改它,返回的值是不同的

如果有人能看一看,也许能告诉我你的想法,我将不胜感激。
谢谢大家!

你真的想知道一个人有什么

        public static final Integer boxes[] = { 5, 3, 7, 10 };

public static void main(String[] args) {
    List<Integer> asList = new ArrayList<Integer>(Arrays.asList(boxes));
    System.out.println(getMaxValue(asList, 0, 0, true));
}

private static int getMaxValue(List<Integer> box, int sumPers1, int sumPers2, boolean isPers1Turn) {
    int chosenBoxIndex;
    if (box.get(0) > box.get(box.size() - 1)) {
        chosenBoxIndex = 0;
    } else {
        chosenBoxIndex = box.size() - 1;
    }
    Integer chosenBoxValue = box.remove(chosenBoxIndex);

    if (isPers1Turn) {
        sumPers1 += chosenBoxValue;
        System.out.println("Pers1 chose: " + chosenBoxValue + " now has a total of " + sumPers1);
    } else {
        sumPers2 += chosenBoxValue;
        System.out.println("Pers2 chose: " + chosenBoxValue + " now has a total of " + sumPers2);
    }
    if (box.size() == 0) {
        return sumPers1;
    }
    return getMaxValue(box, sumPers1, sumPers2, !isPers1Turn);
}
编辑

试试这个:我真的不知道这是否正确

public static void main(String[] args) {
    List<Integer> asList = new ArrayList<Integer>(Arrays.asList(boxes));
    System.out.println(getMaxValueXX(asList, 0, 0, true));
}

private static int getMaxValueXX(List<Integer> box, int sumPers1, int sumPers2, boolean isPers1Turn) {

    int path1 = getMaxValue(box, sumPers1, sumPers2, isPers1Turn, 0);

    int path2 = getMaxValue(box, sumPers1, sumPers2, isPers1Turn, box.size() - 1);

    if (path1 > path2) {
        return path1;
    }

    return path2;

}

private static int getMaxValue(List<Integer> origBox, int sumPers1, int sumPers2, boolean isPers1Turn, int chosenBoxIndex) {
    List<Integer> box = new ArrayList<Integer>(origBox);
    Integer chosenBoxValue = box.remove(chosenBoxIndex);
    if (isPers1Turn) {
        sumPers1 += chosenBoxValue;
        // System.out.println("Pers1 chose: " + chosenBoxValue +
        // " now has a total of " + sumPers1);
    } else {
        sumPers2 += chosenBoxValue;
        // System.out.println("Pers2 chose: " + chosenBoxValue +
        // " now has a total of " + sumPers2);
    }
    if (box.isEmpty()) {
        System.out.println("Value at the end for pers1: " + sumPers1);
        return sumPers1;
    }
    return getMaxValueXX(box, sumPers1, sumPers2, !isPers1Turn);
}

在这场比赛中,每一分不是对玩家1就是对玩家2。这意味着,玩家2最大化自己的分数与最小化玩家1的分数是一样的。让我们把玩家1的分数作为游戏的价值。您需要两种方法:一种模拟玩家1尝试最大化游戏价值的策略,另一种模拟玩家2尝试最小化游戏价值的策略。这种算法称为a

这并不一定是贪婪地选择两个选项中的最高值是最好的策略,尽管在游戏实例中,你给出的恰好是这种情况。对于一般的解决方案,您真的希望尝试这两种方法,看看会发生什么

在您的代码中,您只有一个框列表—它是一个对象,您可以通过引用传递它。您只能从中删除项目。在每一轮中,您有两个递归调用;当第二个调用时,您的列表已为空。因此,您必须复制列表并将其传递给递归调用。或者,传递当前正在考虑的子列表的第一个和最后一个元素的索引。您可以通过以下方式复制列表:

ArrayList<Integer> copy = new ArrayList<>(boxes);
例如,最大化该值的方法如下所示:

public static int maxValue(List<Integer> boxes)
{
    if (boxes.isEmpty())
        return 0;

    List<Integer> boxes1 = new ArrayList<Integer>(boxes);
    int value1 = boxes1.remove(0);
    value1 += minValue(boxes1);

    List<Integer> boxes2 = new ArrayList<Integer>(boxes);
    int value2 = boxes2.remove(boxes2.size() - 1);
    value2 += minValue(boxes2);

    return Math.max(value1, value2);
}

现在您只需要一个minValue实现:-.

我投票将此问题作为离题题结束,因为它属于on。您的问题相互矛盾。不管B个人的选择如何,并模拟两个人的所有选择哪一个是?@LutzHorn我会删除它并把它放在那里,然后!对不起,我甚至不知道codereview的existance@StephanBijzitter它模拟两个人的所有选择,因为它需要检查如果第二个人选择一个或另一个选项会发生什么。我知道我可能不清楚,我会改变的好的,现在清楚了。但是你不需要知道所有的选择,因为你只想要最大值,因此你总是让人1选择最好的选择,而人2选择最差的选择。在这种情况下,您甚至不需要递归。谢谢,假设每个人在每次选择最大值时都可以根据他们可以选择的值进行选择,那么这种方法很有效。问题是,函数每次都要考虑整个价值和选择。例如,对于框={10、150、3、7、9、9},如果他们仅根据他们目前所能做的选择,则人员A的最终值为26,人员B的最终值为162,因为A首先选择10而不是选择9,以防止B保持最大值150。对不起,我不确定我说的是否够清楚
ArrayList<Integer> copy = new ArrayList<>(boxes);
public static int maxValue(List<Integer> boxes)
{
    if (boxes.isEmpty())
        return 0;

    List<Integer> boxes1 = new ArrayList<Integer>(boxes);
    int value1 = boxes1.remove(0);
    value1 += minValue(boxes1);

    List<Integer> boxes2 = new ArrayList<Integer>(boxes);
    int value2 = boxes2.remove(boxes2.size() - 1);
    value2 += minValue(boxes2);

    return Math.max(value1, value2);
}