poj 2794 Double Patience(状态dp)

题意:

一共有9堆牌,每堆牌四张。每次可以取堆顶点数相同的两张牌,如果有多种方案则选取是随机的。

如果最后将所有牌取完,则视为游戏胜利,求胜利的概率。

分析:

用一个九元组表示状态,分别代表每堆牌剩余的牌数。根据全概率公式,d[i]为后继状态成功概率的平均值。


#include<bits/stdc++.h>
using namespace std;
#define LL long long
map<vector<int>, float> dp;
const int MAXN = 100008;
const int MOD = 1e9+7;
char s[12][8][6];

float dfsdp(vector<int> cnt, int tot) {
    if (!tot) return 1;
    if (dp.count(cnt)) return dp[cnt];//count 访问次数
    int ans = 0;
    float res = 0;
    for(int i = 1; i <= 9; i++) if (cnt[i] > 0) {
        for(int j = i+1; j <= 9; j++) if (cnt[j] > 0) {
            if (s[i][cnt[i]][0] == s[j][cnt[j]][0]) {
                cnt[i]--;cnt[j]--;
                ans++;
                res += dfsdp(cnt, tot-1);
                cnt[i]++;cnt[j]++;
            }
        }
    }
    if (!ans) return dp[cnt] = 0;
    return dp[cnt] = res/ans;
}

bool read_input() {
    for(int i = 1; i <= 9; i++) {
        for(int j = 1; j <= 4; j++) {
            if (scanf("%s", s[i][j]) != 1) return false;
        }
    }
    return true;
}

int main() {
    freopen("double.in", "r", stdin);
    freopen("double.out", "w", stdout);
    read_input();
    vector<int> cnt(10, 4); //前十个数初始为4;
    dp.clear();
    printf("%.6f\n", dfsdp(cnt, 18));
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值