注意 一个是等概率选择 则 d[i]为后继状态的成功概率的平均值 意思是 如果取了 牌堆 1 2的牌后 剩下的选择只有 取1 3 后才能成功 则取 1 2成功的概率 为 取 1 3的方案 即 1 除以总方案数
#include<bits/stdc++.h>
using namespace std;
char card[9][4][3];
map<vector<int>,double>d;//记忆化
double dp(vector<int>&cnt,int c){
if(c==0)return 1;//如果卡牌剩余数量为0 则已经成功
if(d.count(cnt) != 0)return d[cnt];//计划化
double sum=0;
int tot=0;
for(int i=0;i<9;i++)if(cnt[i])//如果i堆还有牌
for(int j=i+1;j<9;j++)
if(cnt[j])
{
if(card[i][cnt[i]-1][0]==card[j][cnt[j]-1][0]){//由题意首字母相同就行
tot++;//每次一种移动 都是一种方案
cnt[i]--;cnt[j]--;//移动之后减去牌堆顶部的牌
sum+=dp(cnt,c-2);//记忆话搜索
cnt[i]++;cnt[j]++;//复原
}
}
if(tot==0)return d[cnt]=0;//如果没有方案 则概率该种方案的成功概率为0
else return d[cnt]=sum/tot;//如果有方案 因为题意每种拿法的概率相等 所以是 成功的概率除以总方案数
}
bool read(){
for(int i=0;i<9;i++)
for(int j=0;j<4;j++)//读入数据 输入卡牌位置由低到高
if(scanf("%s",card[i][j])!=1)return 0;
return 1;
}
int main(){
while(read()){
vector<int>cnt(9,4);//初始化 每堆4个
d.clear();
printf("%.6lf\n",dp(cnt,36));
}
return 0;
}