Ultraviolet (a) 1637 Double Patience probability DP

Source: Internet
Author: User

Ultraviolet (a) 1637 Double Patience probability DP




Double Patience
Time Limit:3000 MS Memory Limit:Unknown 64bit IO Format:% Lld & % llu

Submit Status

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 pairs with equal probability.

For example, if the top cards are KS, KH, KD, 9 H, 8 S, 8D, 7C, 7D, and 6 H, he removes any particle pair of (KS, KH), (KS, KD), (KH, KD), (8 S, 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 strategy is 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 file contains several test cases, each of them consists of 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'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'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

For each test case, output a line with 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 KSJC QH AC KH7S QD JD KDQS TS JS 9H6D TD AD 8SQC TH KC 8D8C 9D TC 7C9C 7H JH 7D8H 6S AH 6H

Sample Output

0.589314

Source


Root: aoapc ii: Beginning Algorithm Contests (Second Edition) (Rujia Liu): Chapter 10. Maths: Examples

Submit Status




#include 
 
  #include 
  
   #include 
   
    #include using namespace std;double dp[5][5][5][5][5][5][5][5][5];bool vis[5][5][5][5][5][5][5][5][5];char dui[10][5];char str[4][5];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]=true;    double& x =dp[w1][w2][w3][w4][w5][w6][w7][w8][w9];    bool flag=false;    int top[10]={w1,w2,w3,w4,w5,w6,w7,w8,w9};    for(int i=0;i<9;i++)        if(top[i]) {flag=true; break;}    if(flag==false)    {        return x=1.0;    }    int qu=0;    double sum=0.0;    for(int i=0;i<9;i++)    {        if(top[i]!=0)        for(int j=i+1;j<9;j++)        {            if(top[j]==0) continue;            if(dui[i][top[i]]==dui[j][top[j]])            {                top[i]--; top[j]--;                qu++;                sum+=dfs(top[0],top[1],top[2],top[3],top[4],top[5],top[6],top[7],top[8]);                top[i]++; top[j]++;            }        }    }    if(sum>0) x=sum/(1.*qu);    return x;}int main(){while(scanf("%s%s%s%s",str[0],str[1],str[2],str[3])!=EOF){    memset(dp,0,sizeof(dp));    memset(vis,0,sizeof(vis));for(int i=0;i<4;i++)dui[0][i+1]=str[i][0];for(int i=0;i<8;i++){scanf("%s%s%s%s",str[0],str[1],str[2],str[3]);for(int j=0;j<4;j++)dui[i+1][j+1]=str[j][0];}dfs(4,4,4,4,4,4,4,4,4);printf("%.6lf\n",dp[4][4][4][4][4][4][4][4][4]);}return 0;}
   
  
 



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.