当前位置: 代码迷 >> 综合 >> HDU 5602 Black Jack (记忆化搜索+DP)*
  详细解决方案

HDU 5602 Black Jack (记忆化搜索+DP)*

热度:84   发布时间:2023-11-15 14:35:57.0

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5602

#include<bits/stdc++.h>
using namespace std;#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
const int  maxn =1e5+5;
const int mod=1e9+7;
const int ub=1e6;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
/*
题目大意:两个人,玩21点游戏,
一种操作叫叫牌,可以从卡机中等概率抽出任意点数牌,
点数超过21则立即算输,最终点数大的获胜,
两人回合轮流。根据概率其实回合数只要判断一回就够了,
那这样这道题就可以分析了,首先对于二维状态(a,b)
判断其赢的概率,用DFS搜索一下,整体预处理,
然后对于每个(a,b)状态player能赢的概率,
都是要取先叫牌和不叫牌的最大概率,
对于player先叫牌的概率,用记忆化搜索一下,
对于叫牌其拿的次数可以是任意次,
所以终止条件比较好判别。*/
char s[10];
int get(char c)
{if(c=='A') return 1;else if(c=='T') return 10;else if(c=='J'||c=='Q'||c=='K') return 10;else return c-'0';
}
double dp[30][30],f[30][30];
int vis[30][30];double dfs(int a,int b)
{if(b>21) return 0;if(a<=b) return 1;double ret=0;for(int i=1;i<=13;i++){int p=min(i,10);ret+=dfs(a,b+p)/13.0;}return ret;
}double dfs1(int a,int b)
{if(a>21) return 0;if(vis[a][b]) return f[a][b];double &tmp=f[a][b];tmp=0;vis[a][b]=1;for(int i=1;i<=13;i++){int p=min(i,10);tmp+=dfs1(a+p,b)/13;}return tmp=max(tmp,dp[a][b]);
}int main()
{int t;scanf("%d",&t);for(int i=1;i<=21;i++) for(int j=1;j<=21;j++) dp[i][j]=1-dfs(i,j);///dp状态表示i,j时候庄家叫牌输掉的可能性while(t--){scanf("%s",s);int a=get(s[0])+get(s[1]);int b=get(s[2])+get(s[3]);memset(vis,0,sizeof(vis));if(dfs1(a,b)>0.5) puts("YES");else puts("NO");}return 0;
}

 

  相关解决方案