首页 > 代码库 > HDU 4778 Gems Fight!
HDU 4778 Gems Fight!
Gems Fight!
Time Limit: 10000ms
Memory Limit: 327680KB
This problem will be judged on HDU. Original ID: 477864-bit integer IO format: %I64d Java class name: Main
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 <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 22;18 int G,B,S,bag[maxn][maxn],dp[1<<maxn];19 int dfs(int cur,int sum,int *cooker){20 if(cur == 0 || sum <= 0) return 0;21 if(dp[cur]) return dp[cur];22 int ans = 0;23 for(int i = 0; i < B; ++i){24 if(cur&(1<<i)){25 int sta = cur^(1<<i),ret = 0,tcooker[maxn] = {0};26 for(int k = 0; k < maxn; ++k){27 tcooker[k] += cooker[k] + bag[i][k];28 ret += tcooker[k]/S;29 tcooker[k] %= S;30 }31 if(ret) ans = max(ans,ret+dfs(sta,sum-ret,tcooker));32 else ans = max(ans,sum - dfs(sta,sum,tcooker));33 }34 }35 return dp[cur] = ans;36 }37 int main() {38 while(scanf("%d %d %d",&G,&B,&S),G||B||S){39 int n,tmp,sum;40 int cooker[maxn] = {0};41 memset(bag,0,sizeof(bag));42 for(int i = 0; i < B; ++i){43 scanf("%d",&n);44 while(n--){45 scanf("%d",&tmp);46 ++bag[i][tmp];47 cooker[tmp]++;48 }49 }50 for(int i = sum = 0; i < maxn; ++i) sum += cooker[i]/S;51 memset(dp,0,sizeof(dp));52 memset(cooker,0,sizeof(cooker));53 printf("%d\n",2*dfs((1<<B)-1,sum,cooker) - sum);54 }55 return 0;56 }
HDU 4778 Gems Fight!
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。