Double Patience
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 694   Accepted: 368
Case Time Limit: 1000MS   Special Judge

Description

Double Patience is a single player game played with a standard 36-card deck. The cards are shuffled and laid down on a table in 9 piles of 4 cards each, faces up.

After the cards are laid down, the player makes turns. In a turn he can take top cards of the same rank from any two piles and remove them. If there are several possibilities, the player can choose any one. If all the cards are removed from the table, the player wins the game, if some cards are still on the table and there are no valid moves, the player loses.

George enjoys playing this patience. But when there are several possibilities to remove two cards, he doesn't know which one to choose. George doesn't want to think much, so in such case he just chooses a random pair from among the possible variants and removes it. George chooses among all possible pairswith equal probability.

For example, if the top cards are KS, KH, KD, 9H, 8S, 8D, 7C, 7D, and 6H, he removes any particular pair of (KS, KH), (KS, KD), (KH, KD), (8S, 8D), and (7C, 7D) with the equal probability of 1/5.

Once George's friend Andrew came to see him and noticed that he sometimes doesn't act optimally. George argued, that it is not important - the probability of winning any given patience with his strategyis large enough.

Help George to prove his statement - given the cards on the table in the beginning of the game, find out what is the probability of George winning the game if he acts as described.

Input

The input contains nine lines. Each line contains the description of four cards that form a pile in the beginning of the game, from the bottom card to the top one.

Each card is described with two characters: one for rank, one for suit. Ranks are denoted as '6' for six, '7' for seven, '8' for eight, '9' for nine, 'T' for ten, 'J' for jack, 'Q' for queen, 'K' for king, and 'A' for ace.

Suits are denoted as 'S' for spades, 'C' for clubs, 'D' for diamonds, and 'H' for hearts. For example, "KS" denotes the king of spades.

Card descriptions are separated from each other by one space.

Output

Output one real number - the probability that George wins the game if he plays randomly. Your answer must be accurate up to 10-6.

Sample Input

AS 9S 6C KS
JC QH AC KH
7S QD JD KD
QS TS JS 9H
6D TD AD 8S
QC TH KC 8D
8C 9D TC 7C
9C 7H JH 7D
8H 6S AH 6H

Sample Output

0.589314

Source


用一个九元组表示状态每堆取到哪里,可以压成一个5进制数
全概率公式,d[i]=1/x*sum(d[j]),从i可以取到j
记忆化搜索就可以了
注意从5^9-1开始
//
// main.cpp
// poj2794
//
// Created by Candy on 26/10/2016.
// Copyright ? 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N=2e6+,M=;//!
double d[N];
char c[][],s[],vis[N];
double dp(int u){//printf("dp %d\n",u);
if(vis[u]) return d[u];
vis[u]=;
int a[],cnt=;
for(int i=,t=u;i<=;i++,t/=) a[i]=t%;//,printf("a %d %d\n",t,i,a[i]);
for(int i=;i<=;i++)
for(int j=i+;j<=;j++)
if(a[i]&&a[j]&&c[i][a[i]]==c[j][a[j]]){
cnt++;
int t=;
for(int k=;k>=;k--){
t*=;
if(k==i||k==j) t+=a[k]-;
else t+=a[k];
}
d[u]+=dp(t);
}
if(cnt)d[u]/=(double)cnt;
return d[u];
}
int main(int argc, const char * argv[]){
for(int i=;i<=;i++)
for(int j=;j<=;j++){
scanf("%s",s+);
c[i][j]=s[];
}
d[]=1.0;
printf("%.6f",dp(M));
return ;
}
 

POJ2794 Double Patience[离散概率 状压DP]的更多相关文章

  1. Codeforces Round #363 LRU(概率 状压DP)

    状压DP: 先不考虑数量k, dp[i]表示状态为i的概率,状态转移方程为dp[i | (1 << j)] += dp[i],最后考虑k, 状态表示中1的数量为k的表示可行解. #incl ...

  2. NOIP2016提高A组 A题 礼物—概率状压dp

    题目描述 夏川的生日就要到了.作为夏川形式上的男朋友,季堂打算给夏川买一些生 日礼物. 商店里一共有n种礼物.夏川每得到一种礼物,就会获得相应喜悦值Wi(每种礼物的喜悦值不能重复获得). 每次,店员会 ...

  3. [BZOJ5006][LOJ#2290][THUWC2017]随机二分图(概率+状压DP)

    https://loj.ac/problem/2290 题解:https://blog.csdn.net/Vectorxj/article/details/78905660 不是很好理解,对于边(x1 ...

  4. Luogu4547 THUWC2017 随机二分图 概率、状压DP

    传送门 考虑如果只有$0$组边要怎么做.因为$N \leq 15$,考虑状压$DP$.设$f_i$表示当前的匹配情况为$i$时的概率($i$中$2^0$到$2^{N-1}$表示左半边的匹配情况,$2^ ...

  5. HDU 4336 容斥原理 || 状压DP

    状压DP :F(S)=Sum*F(S)+p(x1)*F(S^(1<<x1))+p(x2)*F(S^(1<<x2))...+1; F(S)表示取状态为S的牌的期望次数,Sum表示 ...

  6. 多校7 HDU5816 Hearthstone 状压DP+全排列

    多校7 HDU5816 Hearthstone 状压DP+全排列 题意:boss的PH为p,n张A牌,m张B牌.抽取一张牌,能胜利的概率是多少? 如果抽到的是A牌,当剩余牌的数目不少于2张,再从剩余牌 ...

  7. BZOJ_1076_[SCOI2008]奖励关_状压DP

    BZOJ_1076_[SCOI2008]奖励关_状压DP 题意: 你正在玩你最喜欢的电子游戏,并且刚刚进入一个奖励关.在这个奖励关里,系统将依次随机抛出k次宝物, 每次你都可以选择吃或者不吃(必须在抛 ...

  8. 【xsy1596】旅行 期望+状压DP

    题目大意:有$m$个人要从城市$1$开始,依次游览城市$1$到$n$. 每一天,每一个游客有$p_i$的概率去下一个城市,和$1-p_i$的概率结束游览. 当游客到达城市$j$,他会得到$(1+\fr ...

  9. Educational Codeforces Round 13 E. Another Sith Tournament 状压dp

    E. Another Sith Tournament 题目连接: http://www.codeforces.com/contest/678/problem/E Description The rul ...

随机推荐

  1. 在IIS启用MP4

    在Windows 2008 R2 Server中,网站无法播放MP4视频.我们需要在IIS的MIME type: 如果我们不想在IIS管理介面更改与设置,或是我们没有更多权限,这样我们可以项目的Web ...

  2. java数据类型

    对String来说,”==“是用来判断两个字符串(对象)的地址是否相同,即判断是否是同一个字符串的应用.”equals()“则是判断两个字符串(对象)的值是否相等,如果相等则返回true.一般情况下, ...

  3. eclipse报错:Failed to load the JNI shared library

    Eclipse运行时提示“Failed to load the JNI shared library /Java/jre6/bin/client/jvm.dll”的一个解决方案 因为 Eclipse ...

  4. Hadoop单机伪分布式

    环境配置:Ubuntu11.10,Hadoop1.0.0 安装ssh 1 apt-get install ssh 安装rsy 1 apt-get install rsync 配置ssh免密码登录 1 ...

  5. 使用pyinstaller打包Python应用,生成EXE执行文件

    在命令行中切换到要打包的程序所在目录,或者在程序目录打开命令行,直接输入下面的指令即可pyinstaller -F xxx.py pyinstaller -F -w -i manage.ico app ...

  6. karma与webpack结合

    一.必备插件 1.babel:es6的语法支持 2.karma:测试框架 3.jasmine:断言框架 4.webpack:打包工具 5.karma-webpack:karma调用webpack打包接 ...

  7. iOS NSArray数组过滤

    需求:在一个数组里面,将在这个数组中的并且在另一个数组里面的元素过滤掉. 即:在一个数组dataArray里面,将在dataArray数组中的并且在filteredArray数组里面的元素过滤掉. / ...

  8. SharePoint 2013 Word 转换PDF服务介绍及示例

    前言:在SharePoint使用过程中,经常会发现将文档进行格式转换的需求,之前,看到SharePoint 2013有将PPT转换PDF文档的服务,后来,才发现SharePoint 2010开始,就有 ...

  9. android对话框的EditText点击后不显示输入法的问题

    dialog.show(); // 以下两行代码是对话框的EditText点击后不能显示输入法的 dialog.getWindow().clearFlags( WindowManager.Layout ...

  10. [转]Design Pattern Interview Questions - Part 4

    Bridge Pattern, Composite Pattern, Decorator Pattern, Facade Pattern, COR Pattern, Proxy Pattern, te ...