这个题 就是 9堆纸牌。 每堆4张。
既然求的是成功的平均概率。 拿 第一行来说 点数可能是 1 2 3 4 5 6 7 1 1. 那么 取走 第一位的1 和倒数第二位1的成功概率为p1 第一位与最后一位为p2 最后两位为p3
那么平均概率为 (p1+p2+p3) / 3;
自然后面每次情况都是这样。 递归求解。 用 九维数组来表示。 每一维 大小为4 代表剩余几张牌。 记忆化搜索即可。
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <string>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <cctype>
using namespace std;
#define ll long long
typedef unsigned long long ull;
#define maxn 1300000+100
#define INF 1<<30
double dp[5][5][5][5][5][5][5][5][5] = {0};
int vis[5][5][5][5][5][5][5][5][5] = {0};
vector <char> q[10];
double d(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 top[10] = {w1,w2,w3,w4,w5,w6,w7,w8,w9};
int flag = 0;
double & x = dp[w1][w2][w3][w4][w5][w6][w7][w8][w9];
for(int i = 0; i < 9; i++){ //是否 全部拿完
if(top[i]){
flag = 1;
break;
}
}
if(!flag){
return x = 1;
}
int hap = 0;
for(int i = 0; i < 8; i++){
if(top[i]){
for(int j = i+1; j < 9; j++){
if(top[j] && q[i][top[i]-1] == q[j][top[j]-1]){
hap++; //转移总次数
top[i]--,top[j]--;
x += d(top[0],top[1],top[2],top[3],top[4],top[5],top[6],top[7],top[8]);
top[i]++,top[j]++;
}
}
}
}
// printf("%lf\n",dp[w1][w2][w3][w4][w5][w6][w7][w8][w9]);
if(x == 0) return x = 0;
else return x = x * 1.0 / (hap * 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 <= 8; 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]);
}
printf("%lf\n",d(4,4,4,4,4,4,4,4,4));
memset(dp,0,sizeof(dp));
memset(vis,0,sizeof(vis));
for(int i = 0; i < 10; i++)
q[i].clear();
}
return 0;
}