HDU 4778 gems fight!

Source: Internet
Author: User
GEMS fight! Time Limit: 10000 msmemory limit: 327680 kbthis 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 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. source2013 Asia Hangzhou Regional Contest solution: memory-based pressure search
 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 }
View code

 

HDU 4778 gems fight!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.