首页 > 代码库 > hdu 4778 Gems Fight! 状态压缩DP
hdu 4778 Gems Fight! 状态压缩DP
Gems Fight!
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 327680/327680 K (Java/Others)
Total Submission(s): 1069 Accepted Submission(s): 456
Problem Description
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.
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.
Input
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.
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.
Output
One line for each case: the amount of Alice‘s Magic stones minus the amount of Bob‘s Magic Stones.
Sample Input
3 4 32 2 32 1 32 1 23 2 3 13 2 23 2 3 13 1 2 30 0 0
Sample Output
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.
Source
2013 Asia Hangzhou Regional Contest
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <vector> 5 #include <iostream> 6 using namespace std; 7 #define INF 0xffffff 8 struct abcd 9 {10 int a[10];11 }p[30];12 int g,b,s,x;13 int dp[1<<22],bb[10];14 int main()15 {16 // freopen("in.txt","r",stdin);17 int i,j,k,cnt,m;18 while(scanf("%d%d%d",&g,&b,&s),(g|b|s))19 {20 memset(p,0,sizeof(p));21 for(i=0;i<b;i++)22 {23 scanf("%d",&m);24 for(j=0;j<m;j++)25 {26 scanf("%d",&x);27 p[i].a[x]++;28 }29 }30 for(i=0;i<(1<<b);i++)31 dp[i]=-INF;32 dp[0]=0;33 for(i=1;i<(1<<b);i++)34 {35 memset(bb,0,sizeof(bb));36 for(j=0;j<b;j++)37 {38 if(!(i&(1<<j)))39 {40 for(k=1;k<=g;k++)41 bb[k]+=p[j].a[k];42 }43 }44 for(j=1;j<=g;j++)bb[j]%=s;45 for(j=0;j<b;j++)46 {47 if((i&(1<<j)))48 {49 cnt=0;50 for(k=1;k<=g;k++)51 {52 cnt+=(bb[k]+p[j].a[k])/s;53 }54 if(cnt)55 dp[i]=max(dp[i],dp[i^(1<<j)]+cnt);56 else dp[i]=max(dp[i],-dp[i^(1<<j)]);57 }58 }59 }60 printf("%d\n",dp[(1<<b)-1]);61 }62 63 }
hdu 4778 Gems Fight! 状态压缩DP
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。