P1837 Single-Player Solitaire
Description
In a single-player solitaire game, a total of $36$ cards are divided into $9$ piles, each pile has $4$ face-up cards. Each time, the player may take two top cards from two different piles if they have the same rank (for example, a spade $10$ and a club $10$), and remove them together. If all cards are eventually removed, the player wins; otherwise, the player loses.
George is very keen on playing this game. However, whenever there are multiple choices, George does not know which one to take. He will randomly choose one from them. For example, suppose the $9$ top cards are $\tt KS,\tt KH,\tt KD,\tt 9H,\tt 8S,\tt 8D,\tt 7C,\tt 7D,\tt 6H$. Clearly there are $5$ ways to take pairs: $\tt (KS,KH),(KS,KD),(KH,KD),(8S,8D),(7C,7D)$. Of course, George chooses each option with probability $1/5$.
One day, George’s friend Andrew told him that this was foolish. But George did not believe it; he thinks that playing this way yields a very high chance of success. Please write a program to help George justify his claim: compute the probability of eventually winning when he follows this strategy.
Input Format
The input consists of $9$ lines. Each line contains $4$ space-separated strings. Each string has two characters, representing the rank and suit, given in order from the bottom of the pile to the top.
Output Format
Output one line: the final winning probability, to $6$ decimal places.
Explanation/Hint
Translated by ChatGPT 5