[Uva1637][DFS] [memory] card game Double Patience

Keywords: C++ P4

Write code must pay attention to!!!!!!

I wrote 1 + 1 for i+1 and changed it all night

Title:

Double Patience is a single player game that uses a standard 36 card deck. After shuffling, the cards are placed on a table, folded into 9 stacks, 4 for each stack, face up.

When the card is down, the player turns. Each time, he can remove the top cards of the same level from any two stacks and then remove them. If there are several possibilities, players can choose any one. If all cards are removed from the table, the player wins the game, and if some cards are still on the table and there is no effective movement, the player fails.

George likes this game. But when there are several possibilities, he doesn't know which one to choose. George doesn't want to think much, so in this case, he just needs to choose a pair of random ones from possible situations and delete it. George is equally likely to choose each situation.

For example, if the top card is Ks,Kh,Kd, 9h, 8s,8d, 7c,7d and 6h, he will delete any pair in (Ks,Kh), (Ks,Kd), (Kh,Kd), (8s,8d) and (7c,7d). The probability of deleting (Ks,Kh), (Ks,Kd), (Kh,Kd), (8s,8d) and (7c,7d) is 1 / 5.

Please work out at the beginning of the game, according to the cards on the table, the possibility of George winning the game if he follows the description

 

Roughly speaking, there are nine stacks of cards, four for each stack. When two stacks of top cards are the same at present, they can be taken away. Ask if there are any cards that can be taken away, and what's the probability of finishing playing cards at last

At first glance, this question has no idea other than search. It's obviously a search question

You can open a nine dimensional array directly, elegantly, simply and roughly

ans[5][5][5][5][5][5][5][5][5]

Record the odds in each case

So we can know:

Win when the card is finished, i.e. ans [0] [0] [0] [0] [0] [0] [0] [0] = 1;

Then you can write loop violence to enumerate each situation

The total number of schemes searched in this layer + 1 for each scheme of card acquisition;

The winning rate of this layer of search is (the sum of the winning rates of all found schemes) / (the number of all found schemes)

When no plan is found, the winning rate is obviously 0;

And then you'll find the TLE

So this question needs to be memorized

If a scheme is searched, it will directly return the winning rate without enumeration

So open another bool array to record the access status

Then the boundary is that the state of no card is accessed (i.e. directly return ans [0] [0] [0] [0] [0] [0] [0] [0] = 1)

This question is AC

 

Here is the code

This code is quite unsophisticated

 1 /*
 2 Train of thought: DFS
 3 Open two nine dimensional (sure) arrays, a double type, to record the winning rate of each deck of cards in each pile
 4 Of course, it can be searched violently, but it can be TLE, so the second array of bool type is useful: memorization
 5 bool Type this array records whether a situation has been experienced
 6 Winning rate of each case: the sum of winning rates searched from this case divided by the sum of all searched cases
 7 When a layer cannot be searched again, the winning rate of this state is 0
 8 When a card can be searched, the odds are 1
 9 No effect of decors on results
10 */
11 
12 # include <cstdio>
13 # include <iostream>
14 
15 using namespace std;
16 
17 int poker[10][5]; // Nine stacks of cards, one stack of four
18 double ans[5][5][5][5][5][5][5][5][5];
19 bool vis[5][5][5][5][5][5][5][5][5];
20 
21 double dfs(int h1, int h2, int h3, int h4, int h5, int h6, int h7, int h8, int h9){
22     if(vis[h1][h2][h3][h4][h5][h6][h7][h8][h9])
23         return ans[h1][h2][h3][h4][h5][h6][h7][h8][h9];
24     vis[h1][h2][h3][h4][h5][h6][h7][h8][h9] = true; // Memorization
25 
26     int height[20] = {0, h1, h2, h3, h4, h5, h6, h7, h8, h9}; // Record the height of each pile of cards
27     
28     double sumRate = 0.0; // Record winning rate
29     double sumSol = 0; // Record the total number of licensing methods found
30 
31     for(int i = 1; i <= 9; i++){
32         for(int j = i + 1; j <= 9; j++){
33             if(height[i] > 0 && height[j] > 0 && poker[i][height[i]] == poker[j][height[j]]){
34                 height[i] -= 1;
35                 height[j] -= 1;
36                 sumSol += 1;
37                 // if(height[1]>=0&&height[2]>=0&&height[3]>=0&&height[4]>=0&&height[5]>=0&&height[6]>=0&&height[7]>=0&&height[8]>=0&&height[9]>=0)
38                 sumRate += dfs(height[1], height[2], height[3], height[4], height[5], height[6], height[7], height[8], height[9]);
39                 height[i]++;
40                 height[j]++;
41             }
42         }
43     }
44 
45     if(sumSol > 0) return ans[h1][h2][h3][h4][h5][h6][h7][h8][h9] = sumRate / sumSol;
46     else return ans[h1][h2][h3][h4][h5][h6][h7][h8][h9];
47 }
48 
49 int main(){
50     vis[0][0][0][0][0][0][0][0][0] = 1;
51     ans[0][0][0][0][0][0][0][0][0] = 1.00;
52     char p1[3], p2[3], p3[3], p4[3];
53     for(int i = 1; i <= 9; i++){
54         cin>>p1>>p2>>p3>>p4;
55         poker[i][1] = p1[0] - '0';
56         poker[i][2] = p2[0] - '0';
57         poker[i][3] = p3[0] - '0';
58         poker[i][4] = p4[0] - '0';
59     }
60     
61     printf("%.6lf", dfs(4, 4, 4, 4, 4, 4, 4, 4, 4));
62 
63     return 0;    
64 }

Be it so

Posted by crouse on Thu, 05 Mar 2020 20:56:46 -0800