UVA 1637 Double Patience 概率DP




Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

 Status

Description

Download as PDF

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

Source


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

 Status




#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

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;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值