Gems Fight!

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 327680/327680 K (Java/Others)
Total Submission(s): 114    Accepted Submission(s): 46

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
 

时限比较多。

二进制表示状态,然后处理就可以了。

dp[1<<21]

dp[i]表示现在状态是i,先手-后手的分数。

枚举i里面存在的位j,   i & (1<<j) != 0

如果取了j, 增加了,那么就是 + dp[i ^ (1<<j)]

否则是  - dp[i ^ (1<<j)]

表示换人了。

 /* ***********************************************
Author :kuangbin
Created Time :2013-11-9 12:55:09
File Name :E:\2013ACM\专题强化训练\区域赛\2013杭州\1009.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std; int c[][];
int b[];
int d[];
int dp[<<];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int G,B,S;
int n,t;
while(scanf("%d%d%d",&G,&B,&S) == )
{
if(G == && B == && S == )break;
memset(c,,sizeof(c));
for(int i = ;i < B;i++)
{
scanf("%d",&n);
while(n--)
{
scanf("%d",&t);
c[i][t]++;
}
}
dp[] = ;
int tot = (<<B);
for(int i = ;i < tot;i++)
{
dp[i] = -;
for(int j = ;j <= G;j++)
b[j] = ;
for(int j = ;j < B;j++)
if((i&(<<j)) == )
{
for(int k = ;k <= G;k++)
{
b[k] += c[j][k];
while(b[k] >= S)
b[k] -= S;
}
}
//cout<<i<<"*"<<endl;
//for(int j = 1;j <= G;j++)
//cout<<b[j]<<endl;
for(int j = ;j < B;j++)
if(i & (<<j))
{
for(int k = ;k <= G;k++)
d[k] = b[k];
int cnt = ;
for(int k = ;k <= G;k++)
{
d[k] += c[j][k];
while(d[k] >= S)
{
d[k] -= S;
cnt++;
}
}
if(cnt > )dp[i] = max(dp[i],cnt + dp[i^(<<j)]);
else dp[i] = max(dp[i],cnt - dp[i^(<<j)]);
} }
printf("%d\n",dp[tot-]);
}
return ;
}

HDU 4778 Gems Fight! (2013杭州赛区1009题,状态压缩,博弈)的更多相关文章

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

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

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

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

  3. HDU 4777 Rabbit Kingdom (2013杭州赛区1008题,预处理,树状数组)

    Rabbit Kingdom Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  4. hdu 4778 Gems Fight! 状态压缩DP

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

  5. HDU 4771 Stealing Harry Potter's Precious (2013杭州赛区1002题,bfs,状态压缩)

    Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  6. HDU 4770 Lights Against Dudely (2013杭州赛区1001题,暴力枚举)

    Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  7. hdu 4778 Gems Fight! 状压dp

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

  8. HDU 4778 Gems Fight!(DP)

    题目链接 当我放弃的时候过了.sb啊,卡常数!!! 换了好几个姿势,本来没写预处理,预处理+俩剪枝,尼玛就过了.. #include <stdio.h> #include <stri ...

  9. hdu 4778 Gems Fight!

    第一次写状压dp-- 题意:http://blog.csdn.net/dyx404514/article/details/15506601 状压dp+博弈吧-- #include<iostrea ...

随机推荐

  1. [python]python三元表达式另类实现方式

    () variable = a if exper else b ()variable = (exper and [b] or [c])[] () variable = exper and b or c

  2. HDU 1867 A + B for you again 字符匹配

    解题报告:给你两个字符串,让你连接起来,没有前后顺序,要求是长度最短优先,其次是字典序最小.这题我用的是KMP,做两次匹配,分别把第一次跟第二次输入的字符串放前面,然后比较两次得到的字符窜的长度和字典 ...

  3. CodeAction_beta02 斐波那契 (多维DP)

    题面: solution: 这题和斐波那契数列没有任何关系!!!!! 这题就是一个无脑DP!!!!!!!!!! 因为所有数都要出现至少一次,所以只需考虑其组合而不用考虑其排列,最后乘个 n!就是了(意 ...

  4. 跳过复制错误——sql_slave_skip_counter

    昨天不少同学讨论<小心,前方有雷 —— sql_slave_skip_counter>,有说作者在玩文字游戏,扯了那么多sql_slave_skip_counter=1不还是跳过一个事务嘛 ...

  5. requests(二): json请求中固定键名顺序&消除键和值之间的空格

    继上一篇requests发送json请求的文章后,实际工作中遇到了以下2种情况. 1:服务端要求json字符串,键名的顺序固定  2.服务端对于接收到的json数据中,若key和value之间有空格, ...

  6. imperva代理拦截

    <external-traffic-monitoring-in-kern>true</external-traffic-monitoring-in-kern> 添加这段就可以开 ...

  7. 八、mini2440裸机程序之UART(2)UART0与PC串口通信【转】

    转自:http://blog.csdn.net/shengnan_wu/article/details/8309417 版权声明:本文为博主原创文章,未经博主允许不得转载. 1.相关原理图 2.相关寄 ...

  8. 解决getJSON跨域登录Session丢失的问题

    最近在做项目中发现,我用下面的代码异步请求到login.ashx: var memberUrl = rooturl + 'member.ashx?r=' + Math.random() + '& ...

  9. makefile 中 foreach

    四.foreach 函数 foreach函数和别的函数非常的不一样.因为这个函数是用来做循环用的,Makefile中的foreach函数几乎是仿照于Unix标准Shell(/bin/sh)中的for语 ...

  10. 【最简单的方法】js判断字符串是否为JSON格式(20180115更新)

    前言 针对 “js判断字符串是否为JSON格式” 这个问题,在网上查了许多资料,都没找到自己想要的答案. 但是看到这个帖子<js判断字符串是否为JSON格式>后,突然灵光一闪,想到一种很简 ...