扑克牌(概率dp)

题目链接

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;

int A, B, C, D;
double f[20][20][20][20][5][5];
bool vis[20][20][20][20][5][5];

double dp(int a, int b, int c, int d, int x, int y){
    if(vis[a][b][c][d][x][y] == true) return f[a][b][c][d][x][y];
    vis[a][b][c][d][x][y] = true;
    double &res = f[a][b][c][d][x][y]; res = 0.0;
    int ta = a, tb = b, tc = c, td = d;
    if(x == 1) ta++; if(x == 2) tb++; if(x == 3) tc++; if(x == 4) td++;
    if(y == 1) ta++; if(y == 2) tb++; if(y == 3) tc++; if(y == 4) td++;
    if(ta >= A && tb >= B && tc >= C && td >= D) return res = 0.0;
    int rest = 54 - ta - tb - tc - td; //剩下的牌数
    if(rest <= 0) return res = 1e10;
    if(a < 13) res += dp(a+1, b, c, d, x, y) * (13 - a) / rest;
    if(b < 13) res += dp(a, b+1, c, d, x, y) * (13 - b) / rest;
    if(c < 13) res += dp(a, b, c+1, d, x, y) * (13 - c) / rest;
    if(d < 13) res += dp(a, b, c, d+1, x, y) * (13 - d) / rest;
    if(x == 0){ //考虑下一步抽到小王
        double tmp = dp(a, b, c, d, 1, y);
        tmp = min(tmp, dp(a, b, c, d, 2, y));
        tmp = min(tmp, dp(a, b, c, d, 3, y));
        tmp = min(tmp, dp(a, b, c, d, 4, y));
        res += tmp / rest;
    }
    if(y == 0){
        double tmp = dp(a, b, c, d, x, 1);
        tmp = min(tmp, dp(a, b, c, d, x, 2));
        tmp = min(tmp, dp(a, b, c, d, x, 3));
        tmp = min(tmp, dp(a, b, c, d, x, 4));
        res += tmp / rest;
    }
    return ++res;
}

int main()
{
    scanf("%d %d %d %d", &A, &B, &C, &D);
    double ans = dp(0, 0, 0, 0, 0, 0);
    if(ans > 100) printf("-1.000\n");
    else printf("%.3f\n", ans);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值