假设在当前状态我们第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. Spring Cloud--搭建Eureka注册中心服务

    使用RestTemplate远程调用服务的弊端: Eureka注册中心: Eureka原理: 搭建Eureka服务 引pom 启动类: 启动类上要加上@EnableEurekaServer注解: 配置 ...

  2. Lucene-如何编写Lucene程序

    Lucene版本:7.1 使用Lucene的关键点 创建文档(Document),添加文件(Field),保存了原始数据信息: 把文档加入IndexWriter: 使用QueryParser.pars ...

  3. 一键部署joomla开源内容管理平台

    https://market.azure.cn/Vhd/Show?vhdId=10896&version=12949 产品详情 产品介绍Joomla是一套自由.开放源代码的内容管理系统,以PH ...

  4. Python3+Selenium3+webdriver学习笔记13(js操作应用:弹出框无效如何处理)

    #!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记13(js操作应用:弹出框无效如何处理)'''from sel ...

  5. jmeter参考网址

    http://blog.csdn.net/dongdong9223/article/details/49248979 http://blog.csdn.net/hjh00/article/detail ...

  6. MYSQL内置函数总结

    时间转换为字符串: SELECT date_format(Date, '%Y-%m-%d %H:%i:%s' ) AS TimeFROMtable o convert函数转换为字符串的时候不存在类型为 ...

  7. cookie和session是否可以保存对象

    session看了一下,是可以保存对象的.语法很普通,但是cookie的话本身是只能保存string类型的信息的,这就需要先序列化,然后接收的页面反序列化后形成对象调用,为了防止乱码,需要在数据传输的 ...

  8. [论文理解] Rapid-Object-Detection-using-a-Boosted-cascade-of-simple-features

    Rapid-Object-Detection-using-a-Boosted-cascade-of-simple-features 简介 文章是2001年发表的,是一篇很经典的Object Detec ...

  9. 手写IOC框架

    1.IOC框架的设计思路 ① 哪些类需要我们的容器进行管理 ②完成对象的别名和对应实例的映射装配 ③完成运行期对象所需要的依赖对象的依赖

  10. Python中Numpy mat的使用

    前面介绍过用dnarray来模拟,但mat更符合矩阵,这里的mat与Matlab中的很相似.(mat与matrix等同) 基本操作 >>> m= np.mat([1,2,3]) #创 ...