题意:36张牌分成9堆,没堆4张,每次可以拿走某两堆顶部的牌,但需要的点数相同,如果有多种拿法就等概率随机拿,问拿完所有牌的概率。
分析:直接用9元组表示当前状态,即每堆剩余的牌数,状态数为5^9=1953125。设d[i]表示状态i对应的概率,则根据全概率公式,d[i]为后继状态的成功概率的平均值,按照动态规划的写法计算即可。
代码如下:
//
//Created by BLUEBUFF 2016/1/11
//Copyright (c) 2016 BLUEBUFF.All Rights Reserved
//
#pragma comment(linker,"/STACK:102400000,102400000")
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/hash_policy.hpp>
//#include <bits/stdc++.h>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <time.h>
#include <cstdlib>
#include <cstring>
#include <complex>
#include <sstream> //isstringstream
#include <iostream>
#include <algorithm>
using namespace std;
//using namespace __gnu_pbds;
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, LL> pp;
#define REP1(i, a, b) for(int i = a; i < b; i++)
#define REP2(i, a, b) for(int i = a; i <= b; i++)
#define REP3(i, a, b) for(int i = a; i >= b; i--)
#define CLR(a, b) memset(a, b, sizeof(a))
#define MP(x, y) make_pair(x,y)
template <class T1, class T2>inline void getmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void getmin(T1 &a, T2 b) { if (b<a)a = b; }
const int maxn = 100010;
const int maxm = 1e5+5;
const int maxs = 10;
const int maxp = 1e3 + 10;
const int INF = 1e9;
const int UNF = -1e9;
const int mod = 1e9 + 7;
const int rev = (mod + 1) >> 1; // FWT
const double PI = acos(-1);
//head
double dp[5][5][5][5][5][5][5][5][5] = {0};
bool vis[5][5][5][5][5][5][5][5][5];
vector <char> q[10];
double dfs(int w1, int w2, int w3, int w4, int w5, int w6, int w7, int w8, int w9){
if(vis[w1][w2][w3][w4][w5][w6][w7][w8][w9])
return dp[w1][w2][w3][w4][w5][w6][w7][w8][w9];
vis[w1][w2][w3][w4][w5][w6][w7][w8][w9] = 1;
int tmp[10] = {w1, w2, w3, w4, w5, w6, w7, w8, w9};
bool flag = 0;
double &x = dp[w1][w2][w3][w4][w5][w6][w7][w8][w9];
for(int i = 0; i < 9; i++){
if(tmp[i]){
flag = 1;
break;
}
}
if(flag == 0){
return x = 1;
}
//else{
int cnt = 0;
for(int i = 0; i < 8; i++){
if(tmp[i]){
for(int j = i + 1; j < 9; j++){
if(tmp[j] && q[i][tmp[i] - 1] == q[j][tmp[j] - 1]){
cnt++;
tmp[i]--;
tmp[j]--;
x += dfs(tmp[0], tmp[1], tmp[2], tmp[3], tmp[4], tmp[5], tmp[6], tmp[7], tmp[8]);
tmp[i]++;
tmp[j]++;
}
}
}
}
if(x == 0) return x = 0;
else{
return x = x * 1.0 / (cnt * 1.0);
}
//}
}
int main()
{
char s1[10], s2[10], s3[10], s4[10];
while(scanf("%s%s%s%s", s1, s2, s3, s4) != EOF)
{
q[0].push_back(s1[0]);
q[0].push_back(s2[0]);
q[0].push_back(s3[0]);
q[0].push_back(s4[0]);
for(int i = 1; i < 9; i++){
scanf("%s%s%s%s", s1, s2, s3, s4);
q[i].push_back(s1[0]);
q[i].push_back(s2[0]);
q[i].push_back(s3[0]);
q[i].push_back(s4[0]);
}
//cout << " ** " << endl;
double ans = dfs(4, 4, 4, 4, 4, 4, 4, 4, 4);
printf("%.6f\n", ans);
CLR(dp, 0);
CLR(vis, 0);
REP1(i, 0, 10) q[i].clear();
}
}