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
 
 
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
#define INF 0xffffff
struct abcd
{
int a[];
}p[];
int g,b,s,x;
int dp[<<],bb[];
int main()
{
// freopen("in.txt","r",stdin);
int i,j,k,cnt,m;
while(scanf("%d%d%d",&g,&b,&s),(g|b|s))
{
memset(p,,sizeof(p));
for(i=;i<b;i++)
{
scanf("%d",&m);
for(j=;j<m;j++)
{
scanf("%d",&x);
p[i].a[x]++;
}
}
for(i=;i<(<<b);i++)
dp[i]=-INF;
dp[]=;
for(i=;i<(<<b);i++)
{
memset(bb,,sizeof(bb));
for(j=;j<b;j++)
{
if(!(i&(<<j)))
{
for(k=;k<=g;k++)
bb[k]+=p[j].a[k];
}
}
for(j=;j<=g;j++)bb[j]%=s;
for(j=;j<b;j++)
{
if((i&(<<j)))
{
cnt=;
for(k=;k<=g;k++)
{
cnt+=(bb[k]+p[j].a[k])/s;
}
if(cnt)
dp[i]=max(dp[i],dp[i^(<<j)]+cnt);
else dp[i]=max(dp[i],-dp[i^(<<j)]);
}
}
}
printf("%d\n",dp[(<<b)-]);
} }

hdu 4778 Gems Fight! 状态压缩DP的更多相关文章

  1. Hdu 4778 Gems Fight! (状态压缩 + DP)

    题目链接: Hdu 4778 Gems Fight! 题目描述: 就是有G种颜色,B个背包,每个背包有n个宝石,颜色分别为c1,c2............两个人轮流取背包放到公共容器里面,容器里面有 ...

  2. hdu 4778 Gems Fight! 状压dp

    转自wdd :http://blog.csdn.net/u010535824/article/details/38540835 题目链接:hdu 4778 状压DP 用DP[i]表示从i状态选到结束得 ...

  3. hdu 4778 Gems Fight! 博弈+状态dp+搜索

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4102743.html 题目链接:hdu 4778 Gems Fight! 博弈+状态dp+搜 ...

  4. HDU 4511 (AC自动机+状态压缩DP)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=4511 题目大意:从1走到N,中间可以选择性经过某些点,比如1->N,或1->2-> ...

  5. HDU 3001 Travelling(状态压缩DP+三进制)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3001 题目大意:有n个城市,m条路,每条路都有一定的花费,可以从任意城市出发,每个城市不能经过两次以上 ...

  6. hdu 4057(ac自动机+状态压缩dp)

    题意:容易理解... 分析:题目中给的模式串的个数最多为10个,于是想到用状态压缩dp来做,它的状态范围为1-2^9,所以最大为2^10-1,那我们可以用:dp[i][j][k]表示长度为i,在tri ...

  7. HDU 4778 Gems Fight! (2013杭州赛区1009题,状态压缩,博弈)

    Gems Fight! Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 327680/327680 K (Java/Others)T ...

  8. hdu 2825(ac自动机+状态压缩dp)

    题意:容易理解... 分析:在做这道题之前我做了hdu 4057,都是同一种类型的题,因为题中给的模式串的个数最多只能为10个,所以我们就很容易想到用状态压缩来做,但是开始的时候我的代码超时了dp时我 ...

  9. HDU 1074 Doing Homework (状态压缩 DP)

    题目大意: 有 n 项作业需要完成,每项作业有上交的期限和需完成的天数,若某项作业晚交一天则扣一分.输入每项作业时包括三部分,作业名称,上交期限,完成所需要的天数.求出完成所有作业时所扣掉的分数最少, ...

随机推荐

  1. edittext基本用法总结.md

    光标的有关问题 edittext.setSelection(2); //记住一个不能越界的bug edittext.setCursorVisible(false); //设置光标显示,不能设置光标颜色 ...

  2. solr-geohsah 按照距离搜索分组

    通过solr的domain-import,将mysql的数据通过查询,导入到solr中.java通过使用solrj,链接solr,调用domaininport,并将分页参数设置到domain-impo ...

  3. Ext.grid.EditorGridPanel分页刷新

    store.reload(); var start = grid.getBottomToolbar().cursor;//获取当前页开始条数 上面获取当前页第一条记录的方法有时候说未定义,我现在使用下 ...

  4. Appium环境搭建(python)

    appium是一个开源的,适用于原生或者移动网络和混合应用程序在 iOS 和 Android 平台上的的开源自动化测试框架.在这里,详细的介绍Appium的环境搭建步骤. 1.安装Node.js在ht ...

  5. 一起来学linux:shell script(一)关于变量

    p { margin-bottom: 0.25cm; line-height: 120% } (一)首先来看什么是变量,在shell中打印出变量采用的是echo $path或者echo ${path} ...

  6. bootstrap 基础(一)

    1 bootstrap简介 bootstrap是Twitter公司的两名前端设计师设计的. bootstrap是一款基于HTML.CSS和JavaScript的一个前端框架. bootstrap的特点 ...

  7. main方法快速编辑日历

    public static void main(String[] args) { Scanner input=new Scanner (System.in); System.out.println(& ...

  8. 201521123113 《Java程序设计》第4周学习总结

    1.本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容. 设计类的技巧:类名和方法名要能够体现他们的职责,类名首字母要大写 如何识别一个类 方法.属性的 ...

  9. 201521123097《Java程序设计》第十一周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 2. 书面作业 1.互斥访问与同步访问 完成题集4-4(互斥访问)与4-5(同步访问) 1.1 除了使用synch ...

  10. 201521123092《java程序设计》第14周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多数据库相关内容. 2. 书面作业 1. MySQL数据库基本操作 建立数据库,将自己的姓名.学号作为一条记录插入.(截图,需出现自 ...