uva1637 概率dp(记忆化搜索)

来源:互联网 发布:剑雨江湖灵骑进阶数据 编辑:程序博客网 时间:2024/05/16 09:00

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, 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
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 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
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 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

36张牌分成9堆,每堆4张牌。每次可以拿走某两堆顶部的牌,但需要点数相同。如果有 多种拿法则等概率的随机拿。例如,9堆顶部的牌分别为KS, KH, KD, 9H, 8S, 8D, 7C, 7D, 6H,则有5种拿法(KS,KH), (KS,KD), (KH,KD), (8S,8D), (7C,7D),每种拿法的概率均为1/5。 如果最后拿完所有牌则游戏成功。按顺序给出每堆牌的4张牌,求成功概率。
【分析】
用9元组表示当前状态,即每堆牌剩的张数,状态总数为5^9=1953125。设d[i]表示状 态i对应的成功概率,则根据全概率公式,d[i]为后继状态的成功概率的平均值,按照动态规 划的写法计算即可。


dfs搜索+记忆化

【代码】:

#include<bits/stdc++.h>using namespace std;typedef long long ll;inline ll qpow(ll n,ll m){ll ans=1;while(m){if(m%2)ans=(ans*n);m/=2;n=(n*n);}return ans;}char s[10][6][3];int c[10];//每一堆的个数double dp[2000000];bool vis[2000000];double dfs(int sta,int card){    if(!card)return 1;//成功    if(vis[sta])return dp[sta];//记忆化搜索    double p=0;    int res=0;    for(int u=1;u<=9;u++)if(c[u])    {        for(int v=1;v<=9;v++)if(u!=v&&c[v])        {            if(s[u][c[u]][0]==s[v][c[v]][0])            {                res++;                c[u]--;c[v]--;                p+=dfs(sta-qpow(5,u-1)-qpow(5,v-1),card-2);                c[u]++;c[v]++;            }        }    }    vis[sta]=1;    if(res==0)return dp[sta]=0;    return dp[sta]=p/res;}int read(){    for(int i=1;i<=9;i++)        for(int j=1;j<=4;j++)            if(scanf("%s",s[i][j])!=1)return 0;    return 1;}int main(){    while(read())    {        memset(vis,0,sizeof(vis));        for(int i=1;i<=9;i++)c[i]=4;        double ans=dfs(qpow(5,9)-1,36);        printf("%.6lf\n",ans);    }}


原创粉丝点击