Free Candies

                                                                                                 Free Candies

原题链接 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1059

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

两种做法,可以dp,也可以构建栈来做,直接搜索有重复,时间过大会超限,使用记忆化搜索保存状态,对于每组数据,先初始化vis,d,dp,三个数组,vis数组用来标记,某个糖果如果被放进篮子里便标记为1,四维记忆数组dp代表每堆糖果分别被拿了a,b,c,d个糖果时最多拿走的糖果对数,d数组记录每堆糖果已经被拿走的糖果数,构建完成后最后输出解即可。

以下是AC代码

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

int n;
int d[4],vis[100],a[4][100];
int dp[100][100][100][100];

int dfs(int t)
{
    if(dp[d[0]][d[1]][d[2]][d[3]]!=-1)
    {
        return dp[d[0]][d[1]][d[2]][d[3]];
    }
    if(t==5)
    {
        return dp[d[0]][d[1]][d[2]][d[3]]=0;
    }
    int ans=0;
    for(int i=0; i<4; i++)
    {
        if(d[i]==n)
        {
            continue;
        }
        int k=a[i][d[i]];
        d[i]++;
        if(vis[k])
        {
            vis[k]=0;
            ans=max(ans,dfs(t-1)+1);
            vis[k]=1;
        }
        else
        {
            vis[k]=1;
            ans=max(ans,dfs(t+1));
            vis[k]=0;
        }
        d[i]--;
    }
    return dp[d[0]][d[1]][d[2]][d[3]]=ans;
}

int main()
{
    while(~scanf("%d",&n),n)
    {
        memset(d,0,sizeof d);
        memset(dp,-1,sizeof dp);
        memset(vis,0,sizeof vis);
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<4; j++)
            {
                scanf("%d",&a[j][i]);
            }
        }
        printf("%d\n",dfs(0));
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这段程序的目的是计算将糖果均分给两个人所需的最小操作次数。让我们来分析一下为什么输入47会得到7作为结果。 当输入为47时,程序通过递归调用 `divide(candies, count, minCount)` 进行计算。初始调用是 `divide(47, 0, minCount)`。 首先,程序检查是否只剩下一个糖果。由于47不等于1,所以不满足条件。 接下来,程序检查47是否为偶数。由于47不是偶数,所以执行 `else` 分支。 在 `else` 分支中,程序进行了两个递归调用: 1. `divide(candies + 1, count + 1, minCount)`:这是将糖果数量加1的操作,并将操作次数加1。 2. `divide(candies - 1, count + 1, minCount)`:这是将糖果数量减1的操作,并将操作次数加1。 这两个递归调用会产生分支,并继续递归地进行计算。 对于第一个递归调用 `divide(candies + 1, count + 1, minCount)`,它会将糖果数量从47增加到48,并将操作次数从0增加到1。 接着,程序继续递归调用 `divide(candies // 2, count + 1, minCount)`,此时糖果数量为48。由于48是偶数,程序执行 `divide(candies // 2, count + 1, minCount)`,将糖果数量除以2,并将操作次数加1。 然后,程序继续递归调用 `divide(candies // 2, count + 1, minCount)`,此时糖果数量为24。同样地,程序将糖果数量除以2,并将操作次数加1。 接下来,程序继续递归调用 `divide(candies // 2, count + 1, minCount)`,此时糖果数量为12。同样地,程序将糖果数量除以2,并将操作次数加1。 继续递归调用 `divide(candies // 2, count + 1, minCount)`,此时糖果数量为6。同样地,程序将糖果数量除以2,并将操作次数加1。 接下来,程序继续递归调用 `divide(candies // 2, count + 1, minCount)`,此时糖果数量为3。由于3是奇数,程序将糖果数量加1,并将操作次数加1。 然后,程序继续递归调用 `divide(candies // 2, count + 1, minCount)`,此时糖果数量为4。同样地,程序将糖果数量除以2,并将操作次数加1。 最后,程序继续递归调用 `divide(candies // 2, count + 1, minCount)`,此时糖果数量为2。同样地,程序将糖果数量除以2,并将操作次数加1。 此时,糖果数量变为1,满足终止条件。程序将当前的操作次数1与 `minCount[0]` 中的值进行比较,并将较小值更新到 `minCount[0]` 中。 综上所述,最小操作次数为7。因此,输入47得到的结果是7。 如果你有任何其他问题,请告诉我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值