Free Candies (dfs + 栈)

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?

 

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

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

题意:给了四堆多种颜色的糖果(每种数字代表一种颜色),还有一个篮子,篮子一次最多只能装5个糖果,如果篮子内有两个糖果的颜色一样,就可以把这一对糖果放口袋带回家了。每次只能从某堆的堆顶取糖果,问最多能带回家多少对糖果。

思路:我们可以把这四堆糖果看成四个栈,每次我们可以尝试从某个栈顶取糖果,以此进行dfs。再想,因为每次只能从栈顶取糖果,那就意味着每种状态(四个栈内分别还剩下多少糖果)只能出现一次,因为不管你是怎样取的,只要达到以前有过的状态,那么肯定是有相同的结果的,所以不需要重复的去搜索。仔细体会一下这个剪枝,也可以理解成,虽然前面取得顺序不同,但此时状态是一样的,那以后的肯定也一样了。

代码如下:

#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
int n,maxx,num[50][5],book[45][45][45][45];//标记状态
stack<int>Q[4];   //四个栈
void dfs(int x,int step,int ans,int p[])//x用位运算来记录此时已有的糖果种类,step篮子里已经装了多少个糖果了
{
                                       //已经装口袋ans对了,p[]四个栈内各取了多少个
    if(step>=5)   //篮子装满了
    {
        return;
    }
    if(ans>maxx)   //更新最大值
        maxx=ans;
    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,ans+1,p);
            }
            else   //没有这个颜色的糖果
            {
                int now=x|(1<<t);  //把这个颜色的糖果放入篮子内
                dfs(now,step+1,ans,p);
            }
            book[p[0]][p[1]][p[2]][p[3]]=1;  //这种状态标记一下
            Q[i].push(t);  //重新入栈
        }
        p[i]--;   //不拿这个栈的糖果了
    }
}
int main()
{
    while(~scanf("%d",&n)&&n)
    {
        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",&num[i][j]);
            }
        for(int i=n-1; i>=0; i--) //分别入栈
        {
            for(int j=0; j<4; j++)
                Q[j].push(num[i][j]);
        }
        memset(book,0,sizeof(book));  //初始化
        book[0][0][0][0]=1;
        int len[4]= {0}; //此时没有从四个栈内拿糖果
        dfs(0,0,0,len);
        printf("%d\n",maxx);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值