HDU 4778 Gems Fight!
2014-10-25 10:06
344 查看
Gems Fight!
Time Limit: 10000msMemory Limit: 327680KB
This problem will be judged on HDU. Original ID: 4778
64-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.
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.
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 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
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解题:记忆化状压搜索
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <vector> #include <queue> #include <cstdlib> #include <string> #include <set> #include <stack> #define LL long long #define pii pair<int,int> #define INF 0x3f3f3f3f using namespace std; const int maxn = 22; int G,B,S,bag[maxn][maxn],dp[1<<maxn]; int dfs(int cur,int sum,int *cooker){ if(cur == 0 || sum <= 0) return 0; if(dp[cur]) return dp[cur]; int ans = 0; for(int i = 0; i < B; ++i){ if(cur&(1<<i)){ int sta = cur^(1<<i),ret = 0,tcooker[maxn] = {0}; for(int k = 0; k < maxn; ++k){ tcooker[k] += cooker[k] + bag[i][k]; ret += tcooker[k]/S; tcooker[k] %= S; } if(ret) ans = max(ans,ret+dfs(sta,sum-ret,tcooker)); else ans = max(ans,sum - dfs(sta,sum,tcooker)); } } return dp[cur] = ans; } int main() { while(scanf("%d %d %d",&G,&B,&S),G||B||S){ int n,tmp,sum; int cooker[maxn] = {0}; memset(bag,0,sizeof(bag)); for(int i = 0; i < B; ++i){ scanf("%d",&n); while(n--){ scanf("%d",&tmp); ++bag[i][tmp]; cooker[tmp]++; } } for(int i = sum = 0; i < maxn; ++i) sum += cooker[i]/S; memset(dp,0,sizeof(dp)); memset(cooker,0,sizeof(cooker)); printf("%d\n",2*dfs((1<<B)-1,sum,cooker) - sum); } return 0; }
View Code
相关文章推荐
- HDU 4778 Gems Fight!(13年杭州区域赛-I题-博弈DP)
- HDU-4778 Gems Fight!
- HDU 4778 Gems Fight! 解题报告
- Hdu 4778 Gems Fight! (状态压缩 + DP)
- hdu 4778 Gems Fight!
- hdu 4778 Gems Fight!
- hdu 4778 Gems Fight!(状压+博弈)
- hdu 4778 Gems Fight! 2013 Asia Hangzhou Regional Contest
- HDU 4778 Gems Fight!
- hdu 4778 Gems Fight!
- hdu 4778 Gems Fight!
- HDU 4778 Gems Fight! [博弈+状压DP(记忆化搜索)]
- 状态压缩(2) Hdu 4778 Gems Fight!
- HDU 4778 Gems Fight!(dp)
- [博弈dp] hdu 4778 Gems Fight!
- HDU 4778 Gems Fight!(DP)
- hdu 4778 Gems Fight!
- hdu 4778 Gems Fight!(2013 杭州 现场赛 I 题) 总和一定的博弈,dp
- hdu-4778-Gems Fight!---状态压缩dp
- HDU 4778 Gems Fight! (博弈+状压DP)