博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
uva 10118
阅读量:4577 次
发布时间:2019-06-08

本文共 3532 字,大约阅读时间需要 11 分钟。

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 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
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 21bool vis[MAXN];int n;int p[5][MAXN * 2];int d[MAXN * 2][MAXN * 2][MAXN * 2][MAXN * 2];int dp(int top[], int t){ if(d[top[1]][top[2]][top[3]][top[4]] != -1) return d[top[1]][top[2]][top[3]][top[4]]; if(t == 5) return d[top[1]][top[2]][top[3]][top[4]] = 0; int maxn = 0; repu(i, 1, 5) if(top[i] <= n) { if(!vis[p[i][top[i]]]) { vis[p[i][top[i]]] = true; top[i]++; maxn = max(maxn, dp(top, t + 1)); top[i]--; vis[p[i][top[i]]] = false; } else { vis[p[i][top[i]]] = false; top[i]++; maxn = max(maxn, dp(top, t - 1) + 1); top[i]--; vis[p[i][top[i]]] = true; } } return d[top[1]][top[2]][top[3]][top[4]] = maxn;}int main(){ while(~scanf("%d", &n) && n) { repu(i, 1, n + 1) scanf("%d%d%d%d", &p[1][i], &p[2][i], &p[3][i], &p[4][i]); _cle(vis, false); _cle(d, -1); int top[] = { 1, 1, 1, 1, 1}; printf("%d\n", dp(top, 0)); } return 0;}
View Code

 

转载于:https://www.cnblogs.com/sunus/p/4498879.html

你可能感兴趣的文章
Ural 1517. Freedom of Choice 后缀数组
查看>>
【转载】Maven入门实践
查看>>
1-4-03:奇偶数判断
查看>>
【SQL Server备份恢复】提高SQL Server备份速度
查看>>
命令行简介(附加参考资料)
查看>>
从0开始整合SSM框架-1.mybatis
查看>>
移位操作的疑问
查看>>
UILabel常用属性小结
查看>>
gitlab 邮件服务器配置
查看>>
Python 循环语句(while, for)
查看>>
深入理解JavaScript原型链
查看>>
LinearGradient类来实现图片的渐变效果
查看>>
Golang关键字—— if/else
查看>>
数据清洗
查看>>
PHP&MySQL(三)——数组
查看>>
各种语法解释及用法
查看>>
UVA 1388 Graveyard
查看>>
Eclipse使用技巧
查看>>
网络请求之get与post异步请求
查看>>
堆和栈的区别
查看>>