假设在当前状态我们第i堆糖果分别取了cnt[i]个,那么篮子里以及口袋里糖果的个数都是可以确定下来的。

所以就可以使用记忆化搜索。

 #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = ;
const int maxm = ; int n; int candy[maxn][maxm];
int d[maxn][maxn][maxn][maxn]; struct State
{
int tot; //篮子里糖果的数量
int cnt[]; //第i堆糖果取了cnt[i]个
int kind[]; //篮子里第i中糖果有ind[i]个
}; int dp(State s)
{
int& ans = d[s.cnt[]][s.cnt[]][s.cnt[]][s.cnt[]];
if(ans >= ) return ans;
if(s.tot == ) return ans = ; ans = ;
for(int i = ; i < ; i++)
{
if(s.cnt[i] == n) continue;
int color = candy[s.cnt[i]][i];
s.cnt[i]++;
if(s.kind[color])
{
s.tot--;
s.kind[color] = ;
ans = max(ans, dp(s) + );
s.tot++;
s.kind[color] = ; }
else
{
s.tot++;
s.kind[color] = ;
ans = max(ans, dp(s));
s.tot--;
s.kind[color] = ;
}
s.cnt[i]--;
}
return ans;
} int main()
{
while(scanf("%d", &n) == && n)
{
for(int i = ; i < n; i++)
for(int j = ; j < ; j++) scanf("%d", &candy[i][j]); State s;
s.tot = ;
for(int i = ; i < ; i++) s.cnt[i] = ;
for(int i = ; i <= ; i++) s.kind[i] = ;
memset(d, -, sizeof(d));
printf("%d\n", dp(s));
} return ;
}

代码君

UVa 10118 记忆化搜索 Free Candies的更多相关文章

  1. uva 707(记忆化搜索)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21261 思路:此题需要记忆化搜索,dp[x][y][t]表示当前状 ...

  2. UVa 10400 记忆化搜索

    #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> us ...

  3. Substring Uva 11468_记忆化搜索 + AC自动机

    Code: #include<cstdio> #include<cstring> #include<queue> using namespace std; cons ...

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

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

  5. UVA - 10118Free Candies(记忆化搜索)

    题目:UVA - 10118Free Candies(记忆化搜索) 题目大意:给你四堆糖果,每一个糖果都有颜色.每次你都仅仅能拿随意一堆最上面的糖果,放到自己的篮子里.假设有两个糖果颜色同样的话,就行 ...

  6. uva 10118,记忆化搜索

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

  7. uva 10581 - Partitioning for fun and profit(记忆化搜索+数论)

    题目链接:uva 10581 - Partitioning for fun and profit 题目大意:给定m,n,k,将m分解成n份,然后依照每份的个数排定字典序,而且划分时要求ai−1≤ai, ...

  8. UVA - 10917 - Walk Through the Forest(最短路+记忆化搜索)

    Problem    UVA - 10917 - Walk Through the Forest Time Limit: 3000 mSec Problem Description Jimmy exp ...

  9. UVa 10285 Longest Run on a Snowboard - 记忆化搜索

    记忆化搜索,完事... Code /** * UVa * Problem#10285 * Accepted * Time:0ms */ #include<iostream> #includ ...

随机推荐

  1. 洛谷 P1969 积木大赛

    题目描述 春春幼儿园举办了一年一度的“积木大赛”.今年比赛的内容是搭建一座宽度为n的大厦,大厦可以看成由n块宽度为1的积木组成,第i块积木的最终高度需要是hi. 在搭建开始之前,没有任何积木(可以看成 ...

  2. SPRING代理模式

    1.静态代理 主题对象:Student public interface Student { public String add(); } 目标对象:RealStudent public class ...

  3. 2017年2月28日-----------乱码新手自学.net 之特性与验证

    现在看asp.net MVC5自学已经到了第六章:数据注解与验证. 话得从以前看MVC music store(音乐商店项目)的源码说起, 最初看music store源码完全就是一脸懵逼,整个程序, ...

  4. WPF 模拟Button按钮事件触发

    this.Submit.AddHandler(Button.ClickEvent, new RoutedEventHandler(this.Submit_Click)); //这种是无效的方法 thi ...

  5. 【js类库AngularJs】学习angularJs的指令(包括常见表单验证,隐藏等功能)

    [js类库AngularJs]学习angularJs的指令(包括常见表单验证,隐藏等功能) AngularJS诞生于2009年,由Misko Hevery 等人创建,后为Google所收购.是一款优秀 ...

  6. C语言的time函数和localtime函数

    1.获取当前时间,并获取当前时间(即系统时间)距离1970年1月1日的时间间隔,以秒为单位. 2.获取指定时间距离1970年1月1日的时间间隔,以秒为单位.

  7. 洛谷 P3353 在你窗外闪耀的星星

    题目描述 飞逝的的时光不会模糊我对你的记忆.难以相信从我第一次见到你以来已经过去了3年.我仍然还生动地记得,3年前,在美丽的集美中学,从我看到你微笑着走出教室,你将头向后仰,柔和的晚霞照耀着你玫瑰色的 ...

  8. UVA 1614 - Hell on the Markets 奇怪的股市(贪心,结论)

    先证明一个结论吧,对于1≤ai≤i+1,前面ai个数一定可以凑出1~sum[i]中的任意一个数. 对于i=1显然成立, 假设对于i=k结论成立,那么对于i=k+1来说,只要证明sum[k]+i,1≤i ...

  9. 八数码问题(一) 暴力BFS + STL

    八数码问题是一个经典的人工智能问题.具体问题不累述了. 思路:由于存在多组测试数据,可以考虑“打表法“.所谓打表法,即枚举所有的初始情况,记录其到达终点的路径.而在这个题目中,顺序打表会调用很多次BF ...

  10. FZOJβ #31.字符串

    http://1572m36l09.iask.in:30808/problem/31 首先转化为保留尽量少的段使得字典序最大.考虑逐字符确定,显然我们可以将相同的连续字符缩在一起.注意到字典序最大的字 ...