Free Candies 【UVA - 10118 】

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 3 4
1 5 6 7 1 5 6 7
2 3 3 3 nothing nothing 2 3 3 3 2 nothing
4 9 8 6 4 9 8 6
8 7 2 1 8 7 2 1
Step3 Take one from pile #2 Step4 Take one from pile #3
Piles Basket Pocket Piles Basket Pocket
1 3 4 1 4
1 6 7 1 6 7
2 3 3 3 2 5 nothing 2 3 3 3 2 3 5 nothing
4 9 8 6 4 9 8 6
8 7 2 1 8 7 2 1
Step5 Take one from pile #2 Step6 Put two candies into his pocket
Piles Basket Pocket Piles Basket Pocket
1 4 1 4
1 6 7 1 6 7
2 3 3 2 3 3 5 nothing 2 3 3 2 5 a pair of 3
4 9 8 6 4 9 8 6
8 7 2 1 8 7 2 1
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 not 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
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种.

代码:

#include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;

int n,maxx;
int book[42][42][42][42],s[50][5];

stack<int>q[4];
void dfs(int x,int step,int sum,int p[])
{
    if(step>4)
        return;

    if(sum>maxx)
        maxx=sum;

    for(int i = 0; i < 4; i ++)
    {
        p[i]++;
        if(!q[i].empty() && !book[p[0]][p[1]][p[2]][p[3]])
        {
           int t=q[i].top();
           q[i].pop();
           if(x&(1<<t))
           {
               int now=x-(x&(1<<t));
               dfs(now,step-1,sum+1,p);
           }
           else
           {
               int now=x|(1<<t);
               dfs(now,step+1,sum,p);
           }
           book[p[0]][p[1]][p[2]][p[3]]=1;
           q[i].push(t);
        }
        p[i]--;
    }
}

int main()
{
    while(~scanf("%d",&n)&&n)
    {
        memset(book,0,sizeof(book));
        maxx=0;
        for(int i = 0; i < 4; i ++)
            while(!q[i].empty())
                q[i].pop();

        for(int i = 0; i < n; i ++)
            for(int j = 0; j < 4; j ++)
                scanf("%d",&s[i][j]);

        for(int i = n-1; i >= 0; i --)
            for(int j = 0; j < 4; j ++)
                q[j].push(s[i][j]);

        book[0][0][0][0]=1;
        int l[4]={0};
        dfs(0,0,0,l);
        printf("%d\n",maxx);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Meant_To_Be/article/details/81274539