10118 - Free Candies

Time limit: 30.000 seconds

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

此题状态很容易想到,但是不得不说我的记忆化搜索真的很渣,写完之后改了好久,问题主要出在递归上,我不知道该怎么表示了。由于记忆化搜索理解的不好,我这题几乎是半猜半蒙弄出来的,待我把记忆化搜索弄清楚再回来看它。

#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define ll long long
#define _cle(m, a) memset(m, a, sizeof(m))
#define repu(i, a, b) for(int i = a; i < b; i++)
#define MAXN 21
bool vis[MAXN];
int n;
int p[][MAXN * ];
int d[MAXN * ][MAXN * ][MAXN * ][MAXN * ]; int dp(int top[], int t)
{
if(d[top[]][top[]][top[]][top[]] != -)
return d[top[]][top[]][top[]][top[]];
if(t == ) return d[top[]][top[]][top[]][top[]] = ;
int maxn = ;
repu(i, , ) if(top[i] <= n) {
if(!vis[p[i][top[i]]]) {
vis[p[i][top[i]]] = true;
top[i]++;
maxn = max(maxn, dp(top, t + ));
top[i]--;
vis[p[i][top[i]]] = false;
}
else {
vis[p[i][top[i]]] = false;
top[i]++;
maxn = max(maxn, dp(top, t - ) + );
top[i]--;
vis[p[i][top[i]]] = true;
}
}
return d[top[]][top[]][top[]][top[]] = maxn;
} int main()
{
while(~scanf("%d", &n) && n)
{
repu(i, , n + )
scanf("%d%d%d%d", &p[][i], &p[][i], &p[][i], &p[][i]);
_cle(vis, false);
_cle(d, -);
int top[] = {, , , , };
printf("%d\n", dp(top, ));
}
return ;
}

uva 10118的更多相关文章

  1. uva 10118(DP)

    UVA 10118 题意: 有4堆糖果,每堆有n(最多40)个,有一个篮子,最多装5个糖果,我们每次只能从某一堆糖果里拿出一个糖果, 如果篮子里有两个相同的糖果,那么就可以把这两个(一对)糖果放进自己 ...

  2. Uva 10118 免费糖果

    题目链接:https://uva.onlinejudge.org/external/101/10118.pdf 参考:http://www.cnblogs.com/kedebug/archive/20 ...

  3. D - Free Candies UVA - 10118

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

  4. uva 10118,记忆化搜索

    这个题debug了长达3个小时,acm我不能放弃,我又回来了的第一题! 一开始思路正确,写法不行,结果越改越乱 看了网上某神的代码,学习了一下 coding+debug:4小时左右,记忆化搜索+dp类 ...

  5. UVA - 10118 Free Candies 记忆化搜索经典

    思路:d[a][b][c][d]表示从已经第一个篮子取了a颗糖,第二个取了b颗糖,第三个取了c颗糖,第四个取了d颗糖最多还能够获得多少糖果.首先明白一个问题:如果能分别取a,b,c,d个,不论如何取, ...

  6. UVA 10118 Free Candies

    https://vjudge.net/problem/UVA-10118 题目 桌上有4堆糖果,每堆有$N$($N\leqslant 40$)颗.有个熊孩子拿了个可以装5颗糖的篮子,开始玩无聊的装糖游 ...

  7. UVa 10118 免费糖果(记忆化搜索+哈希)

    https://vjudge.net/problem/UVA-10118 题意: 桌上有4堆糖果,每堆有N颗.佳佳有一个最多可以装5颗糖的小篮子.他每次选择一堆糖果,把最顶上的一颗拿到篮子里.如果篮子 ...

  8. UVa 10118 Free Candies (记忆化搜索+哈希)

    题意:有4堆糖果,每堆有n(最多40)个,有一个篮子,最多装5个糖果,我们每次只能从某一堆糖果里拿出一个糖果,如果篮子里有两个相同的糖果, 那么就可以把这两个(一对)糖果放进自己的口袋里,问最多能拿走 ...

  9. UVa 10118 记忆化搜索 Free Candies

    假设在当前状态我们第i堆糖果分别取了cnt[i]个,那么篮子里以及口袋里糖果的个数都是可以确定下来的. 所以就可以使用记忆化搜索. #include <cstdio> #include & ...

随机推荐

  1. 精通正则表达式(第三版)——Mastering Regular Expressions,3rd Edition——读书笔记1

    基础知识介绍: 子表达式匹配 环视 引号内的字符串:"(^")*" 12小时制:(1[0123]|[1-9]):[0-5][0-9]*(am|pm) 24小时制:(([0 ...

  2. [SAP ABAP开发技术总结]Form(subroutine)、Function参数传值传址

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  3. 02~ 一步一步教你使用 SVN之SVN 的介绍

    SVN的介绍 一.学习SVN的主要内容的介绍 1.基本操作:学习SVN有哪些基本操作 2.服务器端的安装配置:SVN中必不可少的2个环境之一,会在下面的课程中介绍服务器如何去安装和配置 3.客户端软件 ...

  4. FLASH CC 2015 CANVAS (三) flash中写JS调用html中JS的函数,变量

    注意 此贴 为个人边“开荒”边写,所以不保证就是最佳做法,也难免有错误! 正式教程会在后续开始更新 首先我们在HTML里的JS里面添加几行代码 我们在FLASH中新建一个元件,并拖入到舞台,在属性面板 ...

  5. 基于Spark ALS构建商品推荐引擎

    基于Spark ALS构建商品推荐引擎   一般来讲,推荐引擎试图对用户与某类物品之间的联系建模,其想法是预测人们可能喜好的物品并通过探索物品之间的联系来辅助这个过程,让用户能更快速.更准确的获得所需 ...

  6. CnPlugin 1.5.400

    本软件CnPlugin是PL/SQL Developer工具插件,支持PL/SQL Developer 7.0以上版本.增加了PL/SQL Developer工具本身所没有的一些小功能,功能基本一些已 ...

  7. C++——使用类

    一.运算符重载 1.运算符重载 C++允许将运算符重载扩展到用户定义的类型. 要重载运算符,需使用被称为运算符函数的特殊函数形式.运算符函数的格式如下: operatorop(argument lis ...

  8. 转:C语言中的static变量和C++静态数据成员(static member)

    转自:C语言中的static变量和C++静态数据成员(static member) C语言中static的变量:1).static局部变量        a.静态局部变量在函数内定义,生存期为整个程序 ...

  9. scala调用java的方法,返回了一个对象链表List<Student>,在scala中遍历该链表获取指定Student的名字name

    假设Student类如下: class Student { private int no; private String name; public int getNo() { return no; } ...

  10. Hbase之缓存扫描加快读取速度

    import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; impo ...