algorithm - 了解为涉及采金 jar 的游戏寻找最佳策略的解决方案

标签 algorithm

我无法理解 this question on CareerCup 解决方案背后的原因.

Pots of gold game: Two players A & B. There are pots of gold arranged in a line, each containing some gold coins (the players can see how many coins are there in each gold pot - perfect information). They get alternating turns in which the player can pick a pot from one of the ends of the line. The winner is the player which has a higher number of coins at the end. The objective is to "maximize" the number of coins collected by A, assuming B also plays optimally. A starts the game.

The idea is to find an optimal strategy that makes A win knowing that B is playing optimally as well. How would you do that?

At the end I was asked to code this strategy!

这是 Google 面试中的一个问题。

建议的解决方案是:

function max_coin( int *coin, int start, int end ):
    if start > end:
        return 0

    // I DON'T UNDERSTAND THESE NEXT TWO LINES
    int a = coin[start] + min(max_coin(coin, start+2, end), max_coin(coin, start+1, end-1))
    int b = coin[end] + min(max_coin(coin, start+1,end-1), max_coin(coin, start, end-2))

    return max(a,b)

有两个具体部分我不明白:

  1. 在第一行中,为什么我们使用范围 [start + 2, end] 和 [start + 1, end - 1]?它总是遗漏一个硬币 jar 。不应该是[start + 1, end]因为我们把起始币 jar 拿出来了吗?
  2. 在第一行中,为什么我们取两个结果中的最小值而不是最大值?
  3. 因为我对为什么这两行取最小值以及为什么我们选择这些特定范围感到困惑,所以我不太确定 ab 实际代表什么?

最佳答案

首先ab分别代表播放start(分别为end)时的最大增益.

让我们解释一下这一行:

int a = coin[start] + min(max_coin(coin, start+2, end), max_coin(coin, start+1, end-1))

如果我玩start,我会立即获得coin[start]。另一个玩家现在必须在 start+1end 之间玩。他玩游戏是为了最大化他的 yield 。然而,由于硬币的数量是固定的,这相当于最小化我的。注意

  • 如果他玩 start+1 我将获得 max_coin(coin, start+2, end)
  • 如果他玩 end 我将获得 max_coin(coin, start+1, end-1)

因为他试图最小化我的 yield ,所以我将获得这两者中的最小值。

同样的推理适用于我播放 end 的另一行。

注意:这是一个糟糕的递归实现。首先,max_coin(coin, start+1, end-1) 被计算了两次。即使你解决了这个问题,你最终也会计算出很多时间更短的情况。这与您尝试使用递归计算斐波那契数列时发生的情况非常相似。最好使用记忆化或动态编程。

关于algorithm - 了解为涉及采金 jar 的游戏寻找最佳策略的解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22195300/

相关文章:

algorithm - 查找 'shortest range of indices' 的大小,查找所有唯一路径都通过

algorithm - 最短距离旅行 - 共同集合点

c++ - 为什么贪婪的方法在这种情况下不起作用?

c - For 循环不起作用,但 while 循环起作用

c# - 什么是检查给定值列表是否上下交替的好算法?

algorithm - 在闭合路径上定位点以最大化到加权点样本的距离总和(游戏 AI)

algorithm - 最小皮带,已知距离

algorithm - 逆向算法LISP的实现

algorithm - 运行时间,复杂性,编译时间和执行时间有什么区别?

algorithm - 根据 a、b 和 a^n 以 p 为模计算 b^n