poj 2794 Double Patience(状态dp)

来源:互联网 发布:手机身份证读取软件 编辑:程序博客网 时间:2024/06/12 01:30

题意:

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

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

分析:

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


#include<bits/stdc++.h>using namespace std;#define LL long longmap<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;}

原创粉丝点击