UVa 10891 Game of Sum

本文介绍了一种结合博弈论与动态规划的游戏算法问题。玩家A和B轮流从数组两端取整数,目标是在双方都采取最优策略的情况下,计算玩家A能够比玩家B多获得的最大分数。文章给出了详细的DP状态转移方程,并附带了完整的C++实现代码。
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
4
4 -10 -20 7
4
1 2 3 4
0
Sample Output
7

10

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

区间DP+博弈论~

看起来是博弈论的题目,实际上却是DP~

用f[i][j]表示剩下的区间是[i,j]时从一边取数的最大值,那么枚举中间值k,f[i][j]=min(f[i][j],sum[i][j]-min(f[i][k],f[k+1][j])),其中sum[i][j]表示区间[i,j]的数值总和。可以由前缀和求出。

要注意的是[i,j]此时取数的人也可以直接取走所有数,所以f[i][j]的初始值为sum[i][j]~

最后输出a-b,注意到a+b=sum[1][n],那么答案就是f[1][n]-(sum[1][n]-f[1][n]),化简得f[1][n]*2-sum[1][n]~


#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;

int n,a[101],f[101][101],inf;

int read()
{
	int totnum=0,f=1;char ch=getchar();
	while(ch<'0' || ch>'9') {if(ch=='-') f=-1;ch=getchar();}
	while(ch>='0' && ch<='9') {totnum=(totnum<<1)+(totnum<<3)+ch-'0';ch=getchar();}
	return totnum*f;
}

int main()
{
	while(1)
	{
		n=read();
		if(!n) break;
		for(int i=1;i<=n;i++) f[i][i]=read(),a[i]=a[i-1]+f[i][i];
		for(int l=2;l<=n;l++)
		  for(int i=1,j=l;j<=n;i++,j++)
		  {
		  	f[i][j]=a[j]-a[i-1];
		  	for(int k=i;k<j;k++) f[i][j]=max(f[i][j],a[j]-a[i-1]-min(f[i][k],f[k+1][j]));
		  }
		printf("%d\n",f[1][n]*2-a[n]);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值