• 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.
     
    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
     
     
     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
    ",dp[(1<<b)-1]);
    61     }
    62 
    63 }
    View Code
  • 相关阅读:
    URAL 1736 Chinese Hockey 网络流+建图
    python基础教程_学习笔记14:标准库:一些最爱——re
    吐槽下CSDN编辑器
    让你提前认识软件开发(23):怎样在C语言中运行shell命令?
    GDB十分钟教程
    任务调度开源框架Quartz动态加入、改动和删除定时任务
    AfxMessageBox和MessageBox差别
    Linux Shell脚本入门--awk命令详解
    鲁棒性的获得 —— 测试的架构
    C Tricks(十八)—— 整数绝对值的实现
  • 原文地址:https://www.cnblogs.com/ERKE/p/3982055.html
Copyright © 2020-2023  润新知