扑克牌(概率dp)

题目链接

#include
#include
#include
#include
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;
}

你可能感兴趣的:(算法竞赛进阶指南刷题)