Uva 10118 Free Candies (DP+记忆化搜索)

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):

Step1Initial PilesStep2Take one from pile #2
PilesBasketPocketPilesBasketPocket
1 2 3 4
1 5 6 7
2 3 3 3
4 9 8 6
8 7 2 1
nothingnothing
1   3 4
1 5 6 7
2 3 3 3
4 9 8 6
8 7 2 1
2nothing
Step3Take one from pile #2Step4Take one from pile #3
PilesBasketPocketPilesBasketPocket
1   3 4
1   6 7
2 3 3 3
4 9 8 6
8 7 2 1
2 5nothing
1     4
1   6 7
2 3 3 3
4 9 8 6
8 7 2 1
2 3 5nothing
Step5Take one from pile #2Step6put two candies into his pocket
PilesBasketPocketPilesBasketPocket
1     4
1   6 7
2   3 3
4 9 8 6
8 7 2 1
2 3 3 5nothing
1     4
1   6 7
2   3 3
4 9 8 6
8 7 2 1
2 5a 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

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.

The Output

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.

Sample Input

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

Sample Output

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;
}



 

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值