uva 10118 Free Candies

Title:

There are 4 piles of candy, each pile has n pieces, and each time you take one from the top of a certain pile and put it into the basket. If there are 2 candies of the same color in the basket, you can put them in the bag.

When there are 5 candies in the basket and there are no candies of the same color, this time can no longer be taken.

Ask how many pairs of candies of the same color can be picked up.

Ideas:

Memory search.

Let dp[x][y][z][w] mean that the first pile has taken x pieces, the second pile has taken y pieces, the third pile has taken z, and the fourth pile has taken w pieces , the maximum logarithm that can be obtained.

Because there are only 20 colors of candies in the basket, they can be expressed by state compression. When the number of candies is greater than or equal to 5, it cannot continue.

Code:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4 using namespace std;
 5 const int N = 20;
 6 int n;
 7 int a[45][5];
 8 int dp[45][45][45][45];
 9 int dfs(int x,int y,int z,int w,int sta)
10 {
11     int &ans = dp[x][y][z][w];
12     if (~ans) return ans;
13     int cnt = 0;
14     for (int i = 0;i < 20;i++)
15     {
16         if (sta & (1<<i)) cnt++;
17     }
18     if (cnt >= 5) return ans = 0;
19     if (x < n)
20     {
21         int tmp;
22         if (sta & (1 << a[x+1][1]-1))
23         {
24             tmp = dfs(x+1,y,z,w,sta ^ (1 << a[x+1][1]-1)) + 1;
25         }
26         else
27         {
28             //printf("hh\n");
29             tmp = dfs(x+1,y,z,w,sta ^ (1 << a[x+1][1]-1));
30         }
31         ans = max(ans,tmp);
32     }
33     if (y < n)
34     {
35         int tmp;
36         if (sta & (1 << a[y+1][2]-1))
37         {
38             tmp = dfs(x,y+1,z,w,sta ^ (1 << a[y+1][2]-1)) + 1;
39         }
40         else
41         {
42             tmp = dfs(x,y+1,z,w,sta ^ (1 << a[y+1][2]-1));
43         }
44         ans = max(ans,tmp);
45     }
46     if (z < n)
47     {
48         int tmp;
49         if (sta & (1 << a[z+1][3]-1))
50         {
51             tmp = dfs(x,y,z+1,w,sta ^ (1 << a[z+1][3]-1)) + 1;
52         }
53         else
54         {
55             tmp = dfs(x,y,z+1,w,sta ^ (1 << a[z+1][3]-1));
56         }
57         ans = max(tmp,ans);
58     }
59     if (w < n)
60     {
61         int tmp;
62         if (sta & (1 << a[w+1][4]-1))
63         {
64             tmp = dfs(x,y,z,w+1,sta ^ (1 << a[w+1][4]-1)) + 1;
65         }
66         else
67         {
68             tmp = dfs(x,y,z,w+1,sta ^ (1 << a[w+1][4]-1));
69         }
70         ans = max(ans,tmp);
71     }
72     if (ans == -1) return ans = 0;
73     
74     return ans;
75 }
76 int main()
77 {
78     while (scanf("%d",&n) != EOF && n)
79     {
80         memset(dp,-1,sizeof(dp));
81         for (int i = 1;i <= n;i++)
82         {
83             for (int j = 1;j <= 4;j++) scanf("%d",&a[i][j]);
84         }
85         int ans = dfs(0,0,0,0,0);
86         printf("%d\n",ans);
87     }
88     return 0;
89 }
90 /*
91 3 1 2 3 4 5 6 7 8 1 2 3 4
92 */

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325312825&siteId=291194637