题目:

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

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

题意:

一共有四堆糖果;

给你一个数n,代表每堆有n个糖果。篮子能放5个糖果,每一次只能从每一堆的顶端取一个糖果放在篮子里,如果篮子里有两个颜色一样的糖果就可以把这两个糖果放进自己的口袋,如果篮子满了游戏就结束了;

让你求出来你最多可以得到几对糖果;

思路:

这道题肯定要用记忆化搜索,因为直接搜索会超时,把每一种顶端出现的情况记录下来再进行搜索;

代码如下:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;int n;
int a[5][50];//存放每堆糖果;
int dp[50][50][50][50];//存储出现的各种情况;
int book[50];//标记出现过的糖果;
int top[5];//记录每堆糖果的顶端情况;int dfs(int s)
{int i,ss;if(dp[top[0]][top[1]][top[2]][top[3]]!=-1)//如果这种情况出现过直接返回结果;return dp[top[0]][top[1]][top[2]][top[3]];if(s==5)//篮子满了,游戏结束;return dp[top[0]][top[1]][top[2]][top[3]]=0;int ans=0;for(i=0; i<4; i++){if(top[i]==n)//这一堆糖果取完了,换下一堆;continue;ss=a[i][top[i]];//记录这种糖果的颜色;top[i]+=1;//这一堆糖果取走的数量加一,取到了top[i]+1个;if(book[ss])//篮子中有这种颜色的糖果;{book[ss]=0;//篮子中现在没有这种颜色的糖果,因为成对被取走;ans=max(ans,dfs(s-1)+1);//篮子中糖果数量减少,成对被取走的糖果数量加一;book[ss]=1;//否则还原;}else//篮子中没有这种颜色的糖果;{book[ss]=1;//篮子中现在有这种颜色的糖果;ans=max(ans,dfs(s+1));//篮子中糖果数量增加;book[ss]=0;//还原;}top[i]-=1;//没被取走;}return dp[top[0]][top[1]][top[2]][top[3]]=ans;
}int main()
{while(~scanf("%d",&n)&&n){int i,j;for(i=0; i<n; i++){for(j=0; j<4; j++){scanf("%d",&a[j][i]);}}memset(book,0,sizeof book);memset(dp,-1,sizeof dp);memset(top,0,sizeof top);int ans=dfs(0);printf("%d\n",ans);}return 0;
}

Free Candies相关推荐

  1. K - Candies POJ - 3159(利用了自定义比较操作符)

    K - Candies POJ - 3159 题意: 孩子 A 觉得 B 得到的糖果不能比自己多超过 c,求 n 比 1 最多能多几颗糖果 思路:DJ,松弛条件: sweet[A] > swee ...

  2. AtCoder Beginner Contest 215 G - Colorful Candies 2

    AtCoder Beginner Contest 215 G - Colorful Candies 2 有n个糖果,每个糖果有着一个颜色a[i],每次拿k个糖果期望拿到E(x)个不同颜色的糖果,求出k ...

  3. 575. Distribute Candies 平均分糖果,但要求种类最多

    [抄题]: Given an integer array with even length, where different numbers in this array represent diffe ...

  4. 1431. Kids With the Greatest Number of Candies

    Title 给你一个数组 candies 和一个整数 extraCandies ,其中 candies[i] 代表第 i 个孩子拥有的糖果数目. 对每一个孩子,检查是否存在一种方案,将额外的 extr ...

  5. HDU - 6126 Give out candies

    Give out candies 题解: 第一次遇见这样处理的网络流模型. 将问题转换成最小割问题. 具体的题解参考自:传送门 先将每个人的拆成m个人. 然后s向第1人连边流量为inf.第i个人向第i ...

  6. CF思维联系– CodeForces - 991C Candies(二分)

    ACM思维题训练集合 After passing a test, Vasya got himself a box of n candies. He decided to eat an equal am ...

  7. poj 2886 Who Gets the Most Candies?(线段树)

    题目链接:poj 2886 Who Gets the Most Candies? 题目大意:N个人围成一圈玩约瑟夫环游戏,不同的是.步长不固定,由前一个出局的人决定.给定K表示起始的人. 第i个淘汰的 ...

  8. POJ 3159 Candies(差分约束+SPAF)

    题意: 给n个小朋友分发糖果,但小朋友们之间有嫉妒心.接下来m行,每行三个数,分别表示小朋友A希望B得到的糖果不能比他多x个.要求你计算在满足所有小朋友的条件的情况下最多需要准备多少颗糖. 题目: D ...

  9. 1189C. Candies

    C. Candies:题目 思维题,我是笨蛋.我看了半天也没想明白怎么dp,可恶啊. #include <bits/stdc++.h> using namespace std; #defi ...

  10. Give Candies【快速幂+欧拉】

    Give Candies 时间限制: 1 Sec 内存限制: 128 MB 提交: 243 解决: 92 [提交] [状态] [命题人:admin] 题目描述 There are N children ...

最新文章

  1. 知识星球!!!!!!
  2. leetcode算法题--最长的斐波那契子序列的长度
  3. AB1601中断的问题
  4. linux架构师高级系统调优策略
  5. jQuery scroll事件
  6. 听腾讯 TEG 大佬谈腾讯万亿级 Elasticsearch 技术解密
  7. SAP Commerce Cloud deprecation机制
  8. c struct 对齐_C中的struct大小| 填充,结构对齐
  9. Eclipse-无法执行现有代码,依旧执行以前的代码
  10. BZOJ2259[Oibh] 新型计算机
  11. hp 服务器 sd卡作用,用4张卡聊一聊SD卡规范
  12. socket通信显示连接被拒绝问题总结
  13. 格力董明珠还想再赌五年 雷军:可以试一下
  14. 微信开发流程总结(基于微信平台)
  15. 【项目】前端实习——知识库项目总结
  16. 【诗词】曹雪芹:红豆词
  17. 图片验证码识别程序全面分析
  18. Codeforces 549F Yura and Developers
  19. 提问的智慧!高手如何成长为高手,高手原来也是像我一样的菜鸟!
  20. Siri之父下周将推出全新AI机器人VIV

热门文章

  1. Android系统 T-Mobile G1手机全面解读
  2. python 读取excel的所有sheet_names
  3. 程序员在北京 所有QQ群 集结帖
  4. JAVA 工程师的成长路程
  5. 机器学习框架及评估指标详解
  6. 塔望食品品牌策划:品牌就是生意,功能饮料行业如何正确品牌定位
  7. 一边学计算机一边上班累的说说,上班累心酸的句子
  8. 从1家店到200家,从单店月入3k到30w,只是选择了天猫店群
  9. Windows中Java服务怎么打开防火墙
  10. 仿今日头条“关注”按钮的实现