谁能解释这个内存/动态规划问题/难题的解决方案?

标签 c algorithm dynamic-programming memoization

这是问题陈述:

This is a two player game. Initially there are n integer numbers in an array and players A and B get chance to take them alternatively. Each player can take one or more numbers from the left or right end of the array but cannot take from both ends at a time. He can take as many consecutive numbers as he wants during his time. The game ends when all numbers are taken from the array by the players. The point of each player is calculated by the summation of the numbers, which he has taken. Each player tries to achieve more points from other. If both players play optimally and player A starts the game then how much more point can player A get than player B?

Input

The input consists of a number of cases. Each case starts with a line specifying the integer n (0 < n ≤100), the number of elements in the array. After that, n numbers are given for the game. Input is terminated by a line where n=0.

Output

For each test case, print a number, which represents the maximum difference that the first player obtained after playing this game optimally.

Sample Input                                Output for Sample Input

4

4 -10 -20 7                                 7

4

1 2 3 4                                     10

5

4 -10 -20 7 19                              12

0

这就是这个问题的解决方案。

#include<stdio.h>
#include<stdlib.h>
#define maxn 103

//typedef long long bg;
typedef long bg;

bg Table[maxn][maxn];
bg Seq[maxn];
bool Flag[maxn][maxn];
bg N;

bg Sum(int l, int r) {
    int i, sum = 0;
    for (i = l; i <= r; i++)
        sum += Seq[i];
    return sum;
}

bg Recur(int L, int R) {
    bg max, i, d, k;
    if (L == R)
        return Seq[L];
    if (Flag[L][R])
        return Table[L][R];
    max = Sum(L, R);
    d = Seq[L];
    for (i = L + 1; i <= R; i++) {
        k = Recur(i, R);
        if ((d - k) > max)
            max = d - k;
        d += Seq[i];
    }
    d = Seq[R];
    for (i = R - 1; i >= L; i--) {
        k = Recur(L, i);
        if ((d - k) > max)
            max = d - k;
        d += Seq[i];
    }
    Flag[L][R] = true;
    Table[L][R] = max;
    return max;
}

void Cal() {
    bg max, i, d, k;
    max = Sum(1, N);
    d = Seq[1];
    for (i = 2; i <= N; i++) {
        k = Recur(i, N);
        if ((d - k) > max)
            max = d - k;
        d += Seq[i];
    }
    d = Seq[N];
    for (i = N - 1; i >= 1; i--) {
        k = Recur(1, i);
        if ((d - k) > max)
            max = d - k;
        d += Seq[i];
    }
    printf("%ld\n", max);
}

void Reset() {
    for (int i = 1; i <= 100; i++) {
        for (int j = 1; j <= 100; j++)
            Flag[i][j] = false;
    }
}
int main() {
    //freopen("in.txt", "r", stdin);
    int i;
    while (scanf("%ld", &N) && N) {
        for (i = 1; i <= N; i++)
            scanf("%ld", &Seq[i]);
        Cal();
        Reset();
    }
    return 0;
}

我确实调试了它,但发现很难理解解决方案。

任何人都可以解释这个问题的代码或解决方案。或者任何人都可以发布代码来使用动态编程而不是递归来解决这个问题吗?

最佳答案

这里的状态是 Table[left][right] 如果你有一个包含从 leftright< 的元素的序列,它代表最佳解决方案 包括在内。每一步都尝试每一步可能的移动 - 从左边取 N 个元素或从右边取 N 个元素,其中 N 在 1右 - 左

Example:
4 -10 -20 7

从左边开始:

Table[1][4] = max(sum(1, 1) - Table[2][4], sum(1, 2) - Table[3][4], sum(1, 3) - Table[4][4], sum(1, 4)).

从右边开始:

Table[1][4] = max(sum(4, 4) - Table[1][3], sum(3, 4) - Table[1][2], sum(2, 4) - Table[1][1], sum(1, 4)).

sum(L, R)LR 之间的数组数字之和。我减去是因为下一个对手回合。

关于谁能解释这个内存/动态规划问题/难题的解决方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8150273/

相关文章:

c - 我怎样才能终止系统()?

c - X-NUCLEO_NFC05A1有读取NFC-A(ISO14443A)标签的功能吗?

c - C 的 Sscanf 问题

python - 结果差异的原因是什么?

javascript - 分词算法

algorithm - 这个问题对应于哪个背包问题变体?

c - 了解 C 语言中的简单 For 循环代码

python - 如何快速获取numpy数组中非零值的索引?

algorithm - 生成模式的数学模型

javascript - 带重复的背包 - 阵列解决方案