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. CUBRID学习笔记 30 复制表结构 cubrid教程

    语法 CREATE {TABLE | CLASS} <new_table_name> LIKE <old_table_name> 如下 CREATE TABLE a_tbl( ...

  2. 可以考虑使用SublimeText编辑器替代notepad++了

    大概是去年吧,这款编辑器神一般的出现在我面前,经过我小心翼翼的试用后发现并不是那么太顺手,插件配置都不太成熟,如Package Control. 最喜欢用它的zencoding还得专门开个小窗:ang ...

  3. SLAM reference

    Technical website: OpenSlam: http://openslam.org/ MRPT: http://www.mrpt.org/ Monocular SLAM: https:/ ...

  4. 问题: unrecognized selector sent to class 0x10affab20

    今天遇到了一个问题, 害我足足找了半个小时 问题: 明明可以跳进 方法 的实现里面, 但是程序运行的时候, 就是报错: 发送一个未识别消息, 而且程序直接跳出, 停止执行 原因: 项目的编译文件列表里 ...

  5. Windows Internals学习笔记(五)Synchronization

    参考资料: 1. <Windows Internals> 2. 自旋锁spinlock剖析与改进 3. Lock指令前缀 4. Lock指令前缀(二) 5. Kernel Dispatch ...

  6. Quartz.Net 调度框架配置介绍

    在平时的工作中,估计大多数都做过轮询调度的任务,比如定时轮询数据库同步,定时邮件通知等等.大家通过windows计划任务,windows服务等都实现过此类任务,甚至实现过自己的配置定制化的框架.那今天 ...

  7. jdk和eclipse位数不一致出错

    32位的eclipse无法打开:找不64位jdk6的jvm.dll文件(64位的没有这个文件).网上说法可以通过设置eclipse初始化文件xxx.ini改变方式: 直接换成了同位数的了,没去试了.

  8. Python IDE专用编辑器PyCharm下载及配置安装过程(Ubuntu环境)

    这几天在折腾Python环境,显示把笔记本安装Ubuntu Linux环境系统,然后基本的Python环境都安装完毕之后需要安装传说中在其平台中最好的代码编辑和管理工具PyCharm,于是就根据网上的 ...

  9. (十一)C语言中内存堆和栈的区别

    在计算机领域,堆栈是一个不容忽视的概念,我们编写的C语言程序基本上都要用到.但对于很多的初学着来说,堆栈是一个很模糊的概念. 堆栈:一种数据结构.一个在程序运行时用于存放的地方,这可能是很多初学者的认 ...

  10. android设备休眠机制

    如果一开始就对Android手机的硬件架构有一定的了解,设计出的应用程序通常不会成为待机电池杀手,而要设计出正确的通信机制与通信协议也并不困难.但如果不去了解而盲目设计,可就没准了. 首先Androi ...