问题在于按照这些规则在游戏的每一时刻选择最佳选择:
你们只能拿到最左边或最右心。
你们的反对者将首先从左边或最右心中选择最高卡。 如果它有tie,它就将取胜。 考虑到这一点并不总是最佳选择。
有时是无法赢得胜利的,但是,你必须显示,通过打击这种反对者,你能够增加最大的距离(或说是战略)。
例:
Cards: 1 2 4 2 8 4 3
Opponent: 3 4 2 2 = 11
Me: 1 8 4 = 13
在此,我选择了第二次,而不是4次,因此,我可以在8次发言。 因此,选择最高卡并不总是最佳的。
I ve been trying to implement this solution using recursion but I m not sure it s the best option. Any ideas on how to design this algorithm?
[EDIT] Thanks to @PengOne for it s generous help. This is the code im trying to implement, but unfortunately it s giving me errors. What should I fix in it? I m editing this as I progress.
static int cardGameValue(List<int> D, int myScore, int opponentScore)
{
if (D.Count == 0) return myScore;
else
{
if (D[0] <= D[D.Count - 1])
{
opponentScore += D[D.Count - 1];
D.RemoveAt(D.Count - 1);
}
else
{
opponentScore += D[0];
D.RemoveAt(0);
}
int left = cardGameValue(
new List<int>(D.GetRange(1, D.Count - 1)),
myScore + D[0],
opponentScore);
int right = cardGameValue(
new List<int>(D.Take(D.Count - 2)),
myScore + D[D.Count - 1],
opponentScore);
if (left >= right)
{ return left; }
else
{ return right; }
}
}