UVA - 10118 Free Candies

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 Pocket Piles Basket Pocket
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 Pocket Piles Basket Pocket
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 Pocket Piles Basket Pocket
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?

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.

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


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

题解:有四堆糖果,每堆糖果的每个糖果都有编号,有个最多能放五颗糖果的篮子,每次拿糖果拿每堆的最顶端,如果所拿糖果编号和篮子里的某个一样,则可以拿走这俩,问最多能拿多少糖果。

贴代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,mp[50][5],dp[50][50][50][50];//dp存的是最多拿多少颗
int dfs(int num[], int f[], int lan)
{
    //num[x] x堆里的糖果已经拿了几颗,f[x] 篮子里有无编号为t的糖,篮子里有几颗糖
    int &ans=dp[num[0]][num[1]][num[2]][num[3]];
    if(ans>-1)
        return ans;
    ans=0;
    if(lan==5)//如果篮子里有五个不一样的糖,返回
        return ans;
    for(int i=0; i<4; i++)
    {
        if(num[i]==n)
            continue;
        int t=mp[num[i]][i];
        if(f[t])//篮子里有编号为t的糖
        {
            num[i]++;
            f[t]=0;
            ans=max(ans,dfs(num,f,lan-1)+1);
            f[t]=1;
            num[i]--;
        }
        else
        {
            num[i]++;
            f[t]=1;
            ans=max(ans,dfs(num,f,lan+1));
            f[t]=0;
            num[i]--;
        }
    }
    return ans;
}
int main()
{
    while(~scanf("%d",&n)&&n)
    {
        int i,j;
        memset(dp,-1,sizeof(dp));
        for(i=0; i<n; i++)
            for(j=0; j<4; j++)
                scanf("%d",&mp[i][j]);
        int num[5],f[50];
        memset(f,0,sizeof(f));
        memset(num,0,sizeof(num));
        printf("%d\n",dfs(num,f,0));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/GJLfly/article/details/81274451