您的位置:首页 > 其它

状态压缩(2) Hdu 4778 Gems Fight!

2014-02-19 17:28 417 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4778 

Gems Fight!

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 327680/327680 K (Java/Others)

Total Submission(s): 742    Accepted Submission(s): 313


[align=left]Problem Description[/align]
  Alice and Bob are playing "Gems Fight!":

  There are Gems of G different colors , packed in B bags. Each bag has several Gems. G different colors are numbered from color 1 to color G.

  Alice and Bob take turns to pick one bag and collect all the Gems inside. A bag cannot be picked twice. The Gems collected are stored in a shared cooker.

  After a player ,we name it as X, put Gems into the cooker, if there are S Gems which are the same color in the cooker, they will be melted into one Magic Stone. This reaction will go on and more than one Magic Stone may be produced, until no S Gems of the
same color remained in that cooker. Then X owns those new Magic Stones. When X gets one or more new Magic Stones, he/she will also get a bonus turn. If X gets Magic Stone in a bonus turn, he will get another bonus turn. In short,a player may get multiple bonus
turns continuously.

  There will be B turns in total. The goal of "Gems Fight!" is to get as more Magic Stones than the opponent as possible.

  Now Alice gets the first turn, and she wants to know, if both of them act the optimal way, what will be the difference between the number of her Magic Stones and the number of Bob's Magic Stones at the end of the game.
 

[align=left]Input[/align]
  There are several cases(<=20).

  In each case, there are three integers at the first line: G, B, and S. Their meanings are mentioned above.

  Then B lines follow. Each line describes a bag in the following format:

  

  n c1 c2 ... cn

  

  It means that there are n Gems in the bag and their colors are color c1,color c2...and color cn respectively.

   0<=B<=21, 0<=G<=8, 0<n<=10, S < 20.

  There may be extra blank lines between cases. You can get more information from the sample input.

  The input ends with G = 0, B = 0 and S = 0.
 

[align=left]Output[/align]
  One line for each case: the amount of Alice's Magic stones minus the amount of Bob's Magic Stones.
 

[align=left]Sample Input[/align]

3 4 3
2 2 3
2 1 3
2 1 2
3 2 3 1

3 2 2
3 2 3 1
3 1 2 3

0 0 0

 

[align=left]Sample Output[/align]

3
-3
Hint
  For the first case, in turn 2, bob has to choose at least one bag, so that Alice will make a Magic Stone at the end of turn 3, thus get turn 4 and get all the three Magic Stones.

 

[align=left]Source[/align]
2013 Asia Hangzhou
Regional Contest
 

题意:有B个包裹,里面有各种颜色的GEM,共有G种颜色,Alice 和 Bob轮流挑选包裹到一个地方,如果选出来的同种颜色的GEM个数超过了S个,当前回合者可一个得分,每超过S个 得一个魔法石,如果再当前回合内得分,可以额外进行一个回合。问Alice先手,Alice的得分 减去 Bob 的得分最大是多少。

状态:dp[i] 表示初始可取包裹状态为 i 的时候,先手利用剩下的包裹还能取到的最大魔法石的数量。答案既是 dp[(1<<b)-1].

状态转移: 如果可以得到魔法石,dp[i] = max(dp[i] ,ans + dp[ i ^ (1<<x)]); 如果得不到,先后手逆转,dp[i] = max(dp[i],-dp[i^(1<<x)]);

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define inf 999999999
int b,g,n,s,a,fir[20],left[20];
int bag[25][10];
int dp[1<<21];

int main()
{
while(scanf("%d%d%d",&g,&b,&s)&& g+b+s)
{
memset(bag,0,sizeof bag);
for(int i=0; i<b; i++)
{
scanf("%d",&n);
while(n--)
{
scanf("%d",&a);
bag[i][a]++;
}
}
dp[0]=0;
for(int i = 1; i<(1<<b); i++)
{
dp[i]=-inf;
memset(fir,0,sizeof(fir));
for(int j = 0; j < b; j++)
{
if((i&(1<<j))==0)//对于i状态还能取第j个包
{
for(int k=1; k<=g; k++)
{
fir[k]+=bag[j][k];
fir[k]%=s;
}
}
}
for(int j = 0; j < b; j++)
{
if((i&(1<<j)) != 0)
{
int ans=0;
for(int k=1; k<=g; k++)
left[k]=fir[k];
for(int k=1; k<=g; k++)
{
left[k] += bag[j][k];
ans += left[k]/s;
left[k] %= s;
}
if(ans>0)dp[i]=max(dp[i],ans+dp[i^(1<<j)]);
else dp[i]=max(dp[i],-dp[i^(1<<j)]);//无法得到,先后手逆转。
}
}
}
printf("%d\n",dp[(1<<b)-1]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  状态压缩 DP