LightOJ 1364 Expected Cards (概率DP)

75 篇文章 0 订阅

解析:

设dp[c][d][h][s][i][j]为有c,d,h,s个对应的牌,i、j记录大小王的状态。

记忆化搜索一下就ok

[code]:

#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

int C,D,H,S,cnt[5];
double dp[15][15][15][15][5][5];

void init(){
    int c,d,h,s,i,j;
    for(c = 0;c < 14;c++)
    for(d = 0;d < 14;d++)
    for(h = 0;h < 14;h++)
    for(s = 0;s < 14;s++)
    for(i = 0;i < 5;i++)
    for(j = 0;j < 5;j++)
        dp[c][d][h][s][i][j] = -1;
}

double dfs(int c,int d,int h,int s,int i,int j){
    if(c > 13||d > 13||h > 13||s > 13) return 0;
//printf("status: %d %d %d %d %d %d\n",c,d,h,s,i,j);
    cnt[1] = c;
    cnt[2] = d;
    cnt[3] = h;
    cnt[4] = s;
    if(i) cnt[i]++;
    if(j) cnt[j]++;
    if(cnt[1]>=C&&cnt[2]>=D&&cnt[3]>=H&&cnt[4]>=S) return dp[c][d][h][s][i][j] = 0;
    if(dp[c][d][h][s][i][j] > -0.5) return dp[c][d][h][s][i][j];
    int k,num = cnt[1]+cnt[2]+cnt[3]+cnt[4];
    double ans = 0,tmp;
    num = 54 - num;
    if(num == 0) return 0;
    if(!i){
        tmp = 100;
        for(k = 1;k <= 4;k++){
            tmp = min(tmp,dfs(c,d,h,s,k,j));
        }
        ans += tmp;
    }
    if(!j){
        tmp = 100;
        for(k = 1;k <= 4;k++){
            tmp = min(tmp,dfs(c,d,h,s,i,k));
        }
        ans += tmp;
    }
    ans += (13-c)*dfs(c+1,d,h,s,i,j) + (13-d)*dfs(c,d+1,h,s,i,j)
            + (13-h)*dfs(c,d,h+1,s,i,j) + (13-s)*dfs(c,d,h,s+1,i,j);
    ans = ans/num+1;
    return dp[c][d][h][s][i][j] = ans;
}

int main(){
    int i,j,cas;
    scanf("%d",&cas);
    for(int T=1;T<=cas;T++){
        scanf("%d%d%d%d",&C,&D,&H,&S);
        init();
        printf("Case %d: ",T);
        int x = (C > 13? C - 13 : 0) + (D > 13? D - 13 : 0) + (H > 13? H - 13 : 0) + (S > 13? S - 13 : 0);
        if(x > 2) puts("-1");
        else{
            printf("%.10f\n",dfs(0,0,0,0,0,0));
        }
    }

    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值