zoukankan      html  css  js  c++  java
  • POJ2794 Double Patience[离散概率 状压DP]

    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+5,M=1953124;//!
    double d[N];
    char c[10][5],s[5],vis[N];
    double dp(int u){//printf("dp %d
    ",u);
        if(vis[u]) return d[u];
        vis[u]=1;
        int a[10],cnt=0;
        for(int i=1,t=u;i<=9;i++,t/=5) a[i]=t%5;//,printf("a %d %d
    ",t,i,a[i]);
        for(int i=1;i<=9;i++)
            for(int j=i+1;j<=9;j++)
                if(a[i]&&a[j]&&c[i][a[i]]==c[j][a[j]]){
                    cnt++;
                    int t=0;
                    for(int k=9;k>=1;k--){
                        t*=5;
                        if(k==i||k==j) t+=a[k]-1;
                        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=1;i<=9;i++)
            for(int j=1;j<=4;j++){
                scanf("%s",s+1);
                c[i][j]=s[1];
            }
        d[0]=1.0;
        printf("%.6f",dp(M));
        return 0;
    }
     
  • 相关阅读:
    Java数据结构与算法(24)
    urllib2使用总结
    Python常见文件操作的函数示例
    Java数据结构与算法(23)
    python代码风格检查工具──pylint
    Python抓取框架:Scrapy的架构
    Java数据结构与算法(22)
    【codeforces 431D】Random Task
    【codeforces 449C】Jzzhu and Apples
    【codeforces 20B】Equation
  • 原文地址:https://www.cnblogs.com/candy99/p/6000964.html
Copyright © 2011-2022 走看看