UVALive 6913 I Want That Cake【博弈+记忆化搜索】【好题】
2016-10-21 13:42
411 查看
题目链接
/* 题意:AB 两队吃蛋糕,每个队n个人,共有m块蛋糕,每人至少吃一块蛋糕, 最多吃k块蛋糕,哪个队伍吃到最后一块就算获胜,问你谁能胜利 类型:博弈+记忆化搜索 分析:首先,把连着的A或者B给合并了,合并以后记录一下A和B在当前位置 的个数有多少,然后我们就可以用当前位置的A/B的个数*k得到这个 位置吃蛋糕的数量范围; 接下来就是记忆化搜索,尝试每种状态下是否有可能发展成使得下一位为必败态 */ #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> using namespace std; const int maxn=2105; int n,m,k,dp[maxn][maxn]; char str[maxn]; int v[maxn]; int dfs(int who,int shengyu){ if(dp[who][shengyu]!=-1)//如果当前状态在前面已经出现过,直接返回 return dp[who][shengyu]; if(shengyu<=v[who]*k) //如果剩余的蛋糕比当前的人能吃的少,那么当前状态是必胜态 return dp[who][shengyu]=1; dp[who][shengyu]=0; //否则暂定当前状态为必败态,再往后搜索,如果找到能必胜,则改为必胜返回 for(int i=v[who];i<=v[who]*k;i++){ //搜索当前位所有吃蛋糕的数量 if(dfs(who+1,shengyu-i)==0) //如果下一位为必败态,则当前态改为必胜态返回 return dp[who][shengyu]=1; } return dp[who][shengyu];//找不到必胜态也要记得返回 } int main(){ // freopen("D:\\input.txt","r",stdin); int t; scanf("%d",&t); for(int ik=1;ik<=t;ik++){ memset(dp,-1,sizeof(dp)); memset(v,0,sizeof(v)); scanf("%d%d%d",&n,&m,&k); scanf("%s",str); int cnt=0,flag=1; for(int i=1;i<2*n;i++){ if(str[i]==str[i-1])flag++; else{ v[cnt++]=flag; flag=1; } } v[cnt++]=flag; // for(int i=0;i<cnt;i++) // printf("%d ",v[i]); // printf("\n-----------------------------------------------------------\n"); printf("Case #%d: ",ik); if(dfs(0,m)){ if(str[0]=='A') puts("A"); else puts("B"); }else{ if(str[0]=='A') puts("B"); else puts("A"); } } return 0; }
相关文章推荐
- UVALive 6913 I Want That Cake 博弈dp
- UVALive 6913 I Want That Cake 博弈dp
- UVALive 6913 I Want That Cake 博弈+dp
- UVA LIVE-3263 - That Nice Euler Circuit
- Shanghai 2004(UVaLive 3263) - That Nice Euler Circuit
- uvalive6913 I Want That Cake(博弈dp)
- UVAlive 3263 That Nice Euler Circuit(欧拉定理)
- UVALive - 3263 That Nice Euler Circuit (几何)
- UVALive 3263 That Nice Euler Circuit 计算几何欧拉定理
- UVALive 3263 That Nice Euler Circuit(计算几何)
- HDU 1665 or UVALive 3263 || That Nice Euler Circuit (欧拉定理求面数 F = E+2-V
- uvaLive 3263 That Nice Euler Circuit 欧拉定理
- UVALive - 3263 That Nice Euler Circuit (几何)
- That Nice Euler Circuit UVALive - 3263
- UVALive 3263 That Nice Euler Circuit
- UVA LIVE-3263 - That Nice Euler Circuit
- UVALive 6256 LA 6256 Who wants to live forever?
- UVALive 3263: That Nice Euler Circuit (计算几何)
- UVALive 6862 Triples (找规律 暴力)
- C - Magical GCD UVALive - 6582 (STL )