首页 > 代码库 > UVA 1637 Double Patience 概率DP
UVA 1637 Double Patience 概率DP
Double Patience
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, 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. InputThe 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. OutputFor 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 InputAS 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 Output0.589314 Source Root :: AOAPC II: Beginning Algorithm Contests (Second Edition) (Rujia Liu) :: Chapter 10. Maths :: Examples
|
#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; }
UVA 1637 Double Patience 概率DP