简体   繁体   English

将此递归解决方案转换为DP

[英]Converting this recursive solution to DP

Given a stack of integers, players take turns at removing either 1, 2, or 3 numbers from the top of the stack. 给定一堆整数,玩家轮流从堆栈顶部移除1,2或3个数字。 Assuming that the opponent plays optimally and you select first, I came up with the following recursion: 假设对手以最佳方式进行游戏并且您先选择,我想出了以下递归:

int score(int n) {
    if (n <= 0) return 0;

    if (n <= 3) {
        return sum(v[0..n-1]);
    }

    // maximize over picking 1, 2, or 3 + value after opponent picks optimally
    return max(v[n-1] + min(score(n-2), score(n-3), score(n-4)),
               v[n-1] + v[n-2] + min(score(n-3), score(n-4), score(n-5)),
               v[n-1] + v[n-2] + v[n-3] + min(score(n-4), score(n-5), score(n-6)));
}

Basically, at each level comparing the outcomes of selecting 1, 2, or 3 and then your opponent selecting either 1, 2, or 3. 基本上,在每个级别比较选择1,2或3的结果然后你的对手选择1,2或3。

I was wondering how I could convert this to a DP solution as it is clearly exponential. 我想知道如何将其转换为DP解决方案,因为它显然是指数级的。 I was struggling with the fact that there seem to be 3 dimensions to it: num of your pick, num of opponent's pick, and sub problem size, ie, it seems the best solution for table[p][o][n] would need to be maintained, where p is the number of values you choose, o is the number your opponent chooses and n is the size of the sub problem. 我正在努力解决这样一个事实:它有三个维度:你的选择数,对手选择数和子问题大小,即,它似乎是table[p][o][n]的最佳解决方案需要维护,其中p是您选择的值的数量, o是您的对手选择的数字, n是子问题的大小。

Do I actually need the 3 dimensions? 我真的需要3个尺寸吗? I have seen this similar problem: http://www.geeksforgeeks.org/dynamic-programming-set-31-optimal-strategy-for-a-game/ , but couldn't seem to adapt it. 我已经看到了类似的问题: http//www.geeksforgeeks.org/dynamic-programming-set-31-optimal-strategy-for-a-game/ ,但似乎无法适应它。

Here is way the problem can be converted into DP :- 这是问题可以转换成DP的方式: -

score[i] = maximum{ sum[i] - score[i+1] , sum[i] - score[i+2] , sum[i] - score[i+3]  } 

Here score[i] means max score generated from game [i to n] where v[i] is top of stack . 这里score[i] means max score generated from game [i to n]其中v[i] is top of stack sum[i] is sum of all elements on the stack from i onwards . sum[i] is sum of all elements on the stack from i onwards sum[i] can be evaluated using a separate DP in O(N) . sum[i]可以使用O(N)中的单独DP来评估。 The above DP can be solved using table in O(N) 可以使用O(N)中的表来解决上述DP

Edit :- Following is a DP solution in JAVA :- 编辑: - 以下是JAVA中的DP解决方案: -

public class game {

    static boolean play_game(int[] stack) {
        if(stack.length<=3)
            return true;
        int[] score = new int[stack.length];
        int n = stack.length;
        score[n-1] = stack[n-1];
        score[n-2] = score[n-1]+stack[n-2];
        score[n-3] = score[n-2]+stack[n-3];
        int sum = score[n-3]; 
        for(int i=n-4;i>=0;i--) {
            sum = stack[i]+sum;
            int min = Math.min(Math.min(score[i+1],score[i+2]),score[i+3]);
            score[i] = sum-min;
        }
        if(sum-score[0]<score[0]) 
            return true;

        return false;
    }

    public static void main(String args[]) {
        int[] stack = {12,1,7,99,3};
        System.out.printf("I win => "+play_game(stack));
    }

EDIT:- 编辑:-

For getting a DP solution you need to visualize a problems solution in terms of the smaller instances of itself. 要获得DP解决方案,您需要根据自身的较小实例可视化问题解决方案。 For example in this case as both players are playing optimally , after the choice made by first one ,the second player also obtains an optimal score for remaining stack which the subproblem of the first one. 例如,在这种情况下,当两个玩家都在最佳地玩时,在第一个玩家做出的选择之后,第二个玩家也获得剩余堆栈的最佳分数,该剩余堆栈是第一个的子问题 The only problem here is that how represent it in a recurrence . 这里唯一的问题是如何再次表示它。 To solve DP you must first define a recurrence relation in terms of subproblem which precedes the current problem in any way of computation. 要解决DP,必须首先根据子问题定义递归关系问题以任何计算方式在当前问题之前。 Now we know that whatever second player wins , first player loses so effectively first player gains total sum - score of second player. 现在我们知道,无论第二名球员获胜,第一名球员如此有效地输掉第一名球员获得total sum - score第二名球员total sum - score As second player as well plays optimally we can express the solution in terms of recursion. 作为第二个玩家也能以最佳方式进行游戏,我们可以在递归方面表达解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM