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. xml读写文件实例

    在某个通讯中需要向服务器发送请求xml,格式例子如下: <?xml version="1.0" encoding="UTF-8"?> <ROO ...

  2. hdu 3934&&poj 2079 (凸包+旋转卡壳+求最大三角形面积)

    链接:http://poj.org/problem?id=2079 Triangle Time Limit: 3000MS   Memory Limit: 30000K Total Submissio ...

  3. STORM_0010_Message passing implementation/消息传递的实现

    下面是0.8.0之前的表述,之后的已经基于Disruptor改造过了 这个文章演示了发射和转移tuple是怎么在storm中工作的   Worker为消息传递负责 当zk中的任务出现了变化或者每个ta ...

  4. js的小随笔

    1.在js中{  }中的块级语句没有独立的作用域 var i = 5;for(; i < 8; i++){ console.log(i); } //输出 5 6 7 //全局设置的变量i在for ...

  5. nodejs学习笔记<二>简单的node服务器

    在环境搭建好后,就可以开始动手架设(node驱动)一个简单的web服务器. 首先,nodejs还是用js编写.先来看一段node官网上的实例代码. var http = require('http') ...

  6. Android手机分辨率基础知识(DPI,DIP计算)二

    dp = dip : device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA.HVGA和QVGA 推荐使用这个,不 ...

  7. ios 开发之 Xcode6 No valid signing identities (i.e. certificate and private key pair) matching...

    之前的项目用证书真机测试过,我想再无证书Build,出现下面的报错提示! 下面的team我无法改成None!一点击None选的还是Unhonw name(JPGE28K3W9)这个是报错的关键 最后由 ...

  8. hdu 2604 Queuing(dp递推)

    昨晚搞的第二道矩阵快速幂,一开始我还想直接套个矩阵上去(原谅哥模板题做多了),后来看清楚题意后觉得有点像之前做的数位dp的水题,于是就用数位dp的方法去分析,推了好一会总算推出它的递推关系式了(还是菜 ...

  9. git相关资料

    Git教程http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/git - 简明指南ht ...

  10. 数据字典 dba_free_space及相对文件号RELATIVE_FNO 小结

    1.1 dba_free_space 1.1.1 概述 SQL> desc dba_free_space; Name Type Nullable Default Comments ------- ...