标签:style io color ar os sp for on amp
The Problem
Little Bob is playing a game. He wants to win some candies in it - as many as possible.
There are 4 piles, each pile contains N candies. Bob is given a basket which can hold at most 5 candies. Each time, he puts a candy at the top of one pile into the basket, and if there‘re two candies of the same color in it ,he can take both of them outside the basket and put them into his own pocket. When the basket is full and there are no two candies of the same color, the game ends. If the game is played perfectly, the game will end with no candies left in the piles.
For example, Bob may play this game like this (N=5):
Step1 | Initial Piles | Step2 | Take one from pile #2 | ||
Piles | Basket | Piles | Basket | ||
1 2 3 4 1 5 6 7 2 3 3 3 4 9 8 6 8 7 2 1 |
nothing | nothing |
1 3 4 1 5 6 7 2 3 3 3 4 9 8 6 8 7 2 1 |
2 | nothing |
Step3 | Take one from pile #2 | Step4 | Take one from pile #3 | ||
Piles | Basket | Piles | Basket | ||
1 3 4 1 6 7 2 3 3 3 4 9 8 6 8 7 2 1 |
2 5 | nothing |
1 4 1 6 7 2 3 3 3 4 9 8 6 8 7 2 1 |
2 3 5 | nothing |
Step5 | Take one from pile #2 | Step6 | put two candies into his pocket | ||
Piles | Basket | Piles | Basket | ||
1 4 1 6 7 2 3 3 4 9 8 6 8 7 2 1 |
2 3 3 5 | nothing |
1 4 1 6 7 2 3 3 4 9 8 6 8 7 2 1 |
2 5 | a pair of 3 |
Note that different numbers indicate different colors, there are 20 kinds of colors numbered 1..20.
‘Seems so hard...‘Bob got very much puzzled. How many pairs of candies could he take home at most?
The input will contain no more than 10 test cases. Each test case begins with a line containing a single integer n(1<=n<=40) representing the height of the piles. In the following n lines, each line contains four integers xi1,xi2,xi3,xi4 (in the range 1..20). Each integer indicates the color of the corresponding candy. The test case containing n=0 will terminate the input, you should not give an answer to this case.
Output the number of pairs of candies that the cleverest little child can take home. Print your answer in a single line for each test case.
5 1 2 3 4 1 5 6 7 2 3 3 3 4 9 8 6 8 7 2 1 1 1 2 3 4 3 1 2 3 4 5 6 7 8 1 2 3 4 0
8 0 3
题意:有4堆糖果,每堆有n(最多40)个,有一个篮子,最多装5个糖果,我们每次只能从某一堆糖果里
拿出一个糖果,如果篮子里有两个相同的糖果,那么就可以把这两个(一对)糖果放进自己的口袋里,问
最多能拿走多少对糖果。糖果种类最多20种。
思路:dp[num][one][two][three][four] 表示第一堆取到第one个糖果,第二堆取到第two个糖果
第三堆取到第three个糖果,第四堆取到第four个糖果,篮子里有num个糖果时的最大糖果对数。vis[num][one][two][three][four] 表示第一堆取到第one个糖果,第二堆取到第two个糖果,第三堆取到第three个糖果,第四堆取到第four个糖果,篮子里有num个什么颜色的糖果。由于颜色很少,可以用二进制表示。
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int maxn=45; int dp[6][maxn][maxn][maxn][maxn],n; int vis[6][maxn][maxn][maxn][maxn]; int a[maxn],b[maxn],c[maxn],d[maxn]; void initial() { memset(dp,-1,sizeof(dp)); } void input() { for(int i=1;i<=n;i++) scanf("%d %d %d %d",&a[i],&b[i],&c[i],&d[i]); } int DP(int num,int one,int two,int three,int four) { if(num==5) return 0; if(dp[num][one][two][three][four]!=-1) return dp[num][one][two][three][four]; int ans=0,t,p=vis[num][one][two][three][four]; if(one<n) { t=1<<(a[one+1]-1); if(p & t) { vis[num-1][one+1][two][three][four]=p-t; ans=max(ans,DP(num-1,one+1,two,three,four)+1); } else { vis[num+1][one+1][two][three][four]=p|t; ans=max(ans,DP(num+1,one+1,two,three,four)); } } if(two<n) { t=1<<(b[two+1]-1); if(p & t) { vis[num-1][one][two+1][three][four]=p-t; ans=max(ans,DP(num-1,one,two+1,three,four)+1); } else { vis[num+1][one][two+1][three][four]=p|t; ans=max(ans,DP(num+1,one,two+1,three,four)); } } if(three<n) { t=1<<(c[three+1]-1); if(p & t) { vis[num-1][one][two][three+1][four]=p-t; ans=max(ans,DP(num-1,one,two,three+1,four)+1); } else { vis[num+1][one][two][three+1][four]=p|t; ans=max(ans,DP(num+1,one,two,three+1,four)); } } if(four<n) { t=1<<(d[four+1]-1); if(p & t) { vis[num-1][one][two][three][four+1]=p-t; ans=max(ans,DP(num-1,one,two,three,four+1)+1); } else { vis[num+1][one][two][three][four+1]=p|t; ans=max(ans,DP(num+1,one,two,three,four+1)); } } return dp[num][one][two][three][four]=ans; } int main() { while(scanf("%d",&n)!=EOF) { if(n==0) break; initial(); input(); vis[0][0][0][0][0]=0; printf("%d\n",DP(0,0,0,0,0)); } return 0; }
Uva 10118 Free Candies (DP+记忆化搜索)
标签:style io color ar os sp for on amp
原文地址:http://blog.csdn.net/u012596172/article/details/41172531