hdu4778 Gems Fight!
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 327680/327680 K (Java/Others)
Total Submission(s): 1912 Accepted Submission(s): 824
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.
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.
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
-3
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.
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 99999999
int dp[4200000][2];
int vis[4200000];
int num[30][30];
int p[10]; //把p[]设为全局变量,这样dfs中的一个层改变,总体也改变了
int tot,g,b,s;
void dfs(int state,int turn)
{
int i,j,st,turn1;
int pp[10];
if( state==(1<<b)-1 ){
dp[state][0]=dp[state][1]=0;
return;
}
if(vis[state])return;
dp[state][1]=0;
dp[state][0]=-inf;
int cha=-inf;
for(j=1;j<=8;j++){ //这里要先把p[]备份,因为还要回溯
pp[j]=p[j];
}
for(i=1;i<=b;i++){
int t1=0;
int t2=0;
if(state&(1<<(i-1)) )continue; //这里要选择一个当前状态没有选择过的背包
st=state|(1<<(i-1) );
int cnt=0;
for(j=1;j<=g;j++){
p[j]+=num[i][j];
cnt+=p[j]/s;
p[j]%=s;
}
if(cnt==0){
turn1=1^turn; //这里表示是不是要换成对手拿
}
else turn1=turn;
dfs(st,turn1);
for(j=1;j<=8;j++){
p[j]=pp[j];
}
t1+=cnt;
if(cnt==0){ //如果交换了,那么先手t1的值要加上st状态后手拿的最大值
t1+=dp[st][1];
t2+=dp[st][0];
}
else{
t1+=dp[st][0];
t2+=dp[st][1];
}
if(t1-t2>cha){
cha=t1-t2;
dp[state][0]=t1;
dp[state][1]=t2;
}
}
vis[state]=1; //访问过的状态就不用访问了,相当于剪枝
return ;
}
int main()
{
int n,m,i,j,c,t;
while(scanf("%d%d%d",&g,&b,&s)!=EOF)
{
if(g==0 && b==0 && s==0)break;
memset(num,0,sizeof(num));
for(i=1;i<=b;i++){
scanf("%d",&t);
for(j=1;j<=t;j++){
scanf("%d",&c);
num[i][c]++;
}
}
memset(p,0,sizeof(p));
memset(vis,0,sizeof(vis));
dfs(0,0);
printf("%d\n",dp[0][0]-dp[0][1]);
}
return 0;
}
也可以用状压dp,用dp[state]表示在state状态下先手与后手的最大差距。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 15
int a[25][22],dp[1<<23];
int color[25],color1[25];
int main()
{
int n,m,i,j,T,G,B,S,c,state;
while(scanf("%d%d%d",&G,&B,&S)!=EOF)
{
if(G==0 && B==0 && S==0)break;
memset(a,0,sizeof(a));
for(i=1;i<=B;i++){
scanf("%d",&n);
for(j=1;j<=n;j++){
scanf("%d",&c);
a[i][c]++;
}
}
dp[0]=0;
for(state=1;state<(1<<B);state++){
dp[state]=-inf;
for(i=1;i<=G;i++)color[i]=0;
for(i=1;i<=B;i++){
if((state&(1<<(i-1) ))==0 ){
for(j=1;j<=G;j++){
color[j]+=a[i][j];
color[j]=color[j]%S;
}
}
}
for(i=1;i<=B;i++){
if(state&(1<<(i-1) )){
int state1=(state^(1<<(i-1) ) );
for(j=1;j<=G;j++)color1[j]=color[j];
int cnt=0;
for(j=1;j<=G;j++){
color1[j]+=a[i][j];
cnt+=color1[j]/S;
}
if(cnt!=0) dp[state]=max(dp[state],dp[state1]+cnt);
else dp[state]=max(dp[state],-dp[state1]); //这里没有产生魔法石,所以先后手互换
}
}
}
printf("%d\n",dp[(1<<B)-1]);
}
return 0;
}
hdu4778 Gems Fight!的更多相关文章
- hdu 4778 Gems Fight! 博弈+状态dp+搜索
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4102743.html 题目链接:hdu 4778 Gems Fight! 博弈+状态dp+搜 ...
- hdu 4778 Gems Fight! 状态压缩DP
Gems Fight! Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 327680/327680 K (Java/Others)T ...
- HDU 4778 Gems Fight! (2013杭州赛区1009题,状态压缩,博弈)
Gems Fight! Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 327680/327680 K (Java/Others)T ...
- Hdu 4778 Gems Fight! (状态压缩 + DP)
题目链接: Hdu 4778 Gems Fight! 题目描述: 就是有G种颜色,B个背包,每个背包有n个宝石,颜色分别为c1,c2............两个人轮流取背包放到公共容器里面,容器里面有 ...
- HDU 4778 Gems Fight!(DP)
题目链接 当我放弃的时候过了.sb啊,卡常数!!! 换了好几个姿势,本来没写预处理,预处理+俩剪枝,尼玛就过了.. #include <stdio.h> #include <stri ...
- hdu 4778 Gems Fight! 状压dp
转自wdd :http://blog.csdn.net/u010535824/article/details/38540835 题目链接:hdu 4778 状压DP 用DP[i]表示从i状态选到结束得 ...
- hdu 4778 Gems Fight!
第一次写状压dp-- 题意:http://blog.csdn.net/dyx404514/article/details/15506601 状压dp+博弈吧-- #include<iostrea ...
- Unable to download data from http://ruby.taobao.org/ & don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.
安装cocoapods,记录两个问题! 1.镜像已经替换成了 http://ruby.taobao.org/, 还是不能不能安装cocoapods, 报错:Unable to download dat ...
- Australian troops to the fight against Islamic State militants.
He arrived in Arnhem Land on Sunday, honouring an election promise to spend a week every year in an ...
随机推荐
- 【Linux】如何查看命令来源于哪个包
Debian:(Ubuntu等) 先安装apt-file sudo apt-get install -y apt-file apt-file update 查询命令:(已查询ifconfig为例) r ...
- 【IMPDP】ORA-31655
出现ora-31655错误的情况是因为不是同一个schema,导致的问题产生 解决的方法; 在导入语句最后添加上remap_schema=old:new 着old是原schema,也就是导出的用户名, ...
- CVE-2020-0796复现
今天整理资料时发现了之前存的一个cve漏洞复现过程,当时打算跟着复现来着,后来也没去复现,今天刚好有时间,所以来复现一下这个漏洞 漏洞讲解 https://www.freebuf.com/vuls/2 ...
- 小试牛刀ElasticSearch大数据聚合统计
ElasticSearch相信有不少朋友都了解,即使没有了解过它那相信对ELK也有所认识E即是ElasticSearch.ElasticSearch最开始更多用于检索,作为一搜索的集群产品简单易用绝对 ...
- Canal:同步mysql增量数据工具,一篇详解核心知识点
老刘是一名即将找工作的研二学生,写博客一方面是总结大数据开发的知识点,一方面是希望能够帮助伙伴让自学从此不求人.由于老刘是自学大数据开发,博客中肯定会存在一些不足,还希望大家能够批评指正,让我们一起进 ...
- [Poi2012]Rendezvous
题目描述 给定一个n个顶点的有向图,每个顶点有且仅有一条出边. 对于顶点i,记它的出边为(i, a[i]). 再给出q组询问,每组询问由两个顶点a.b组成,要求输出满足下面条件的x.y: 从顶点a沿着 ...
- 集成 12 种协议、可于 USBC 端口的快充协议芯片IP2188
1. 特性 支持 12 种 USB 端口快充协议 支持 USB TypeC PD2.0/PD3.0/PPS DFP 协议 支持多种充电协议(QC3.0/QC2.0,FCP,SCP, AFC,MT ...
- Java中的Date类型无法赋值给数据库的datetime类型
因为Java中new Date()的结果是"Thu Aug 27 19:03:54 CST 2020",而mysql中的datetime不接受这样的日期格式,插入数据会报错. 解决 ...
- 获取控制台的错误信息 onerror
js 获取控制台的错误信息 https://www.bbsmax.com/A/Vx5ML2NmJN/ <!DOCTYPE html> <html lang="en" ...
- 你应该了解的25个JS技巧
目录 1. 类型检查小工具 2. 检查是否为空 3. 获取列表最后一项 4. 带有范围的随机数生成器 5. 随机 ID 生成器 6. 创建一个范围内的数字 7. 格式化 JSON 字符串,string ...