Free Candies

题意:

有4堆东西,每堆有n个每个有一个颜色,现在有一个篮子最多能装5个不同的颜色的东西,每次都从堆顶拿,当篮子出现两个相同颜色,可以获得这两个东西,求获得的最大数量

分析:

因为就4推,可以把各堆的取得状态表示出来,用记忆化搜索,因为最多可以装5个作为转移的状态。

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
const ll  INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod =  1000000007;
int pile[4][45],top[4],dp[45][45][45][45],n;
int dfs(int num,int color[]){
    if(dp[top[0]][top[1]][top[2]][top[3]]!=-1)return dp[top[0]][top[1]][top[2]][top[3]];
    if(num==5)
        return dp[top[0]][top[1]][top[2]][top[3]]=0;
        int maxv=0;
    for(int i=0;i<4;++i){
        if(top[i]==n)continue;
        int tmp=pile[i][top[i]];
        top[i]+=1;
        if(color[tmp]){
            color[tmp]=0;
            maxv=max(maxv,dfs(num-1,color)+1);
            color[tmp]=1;
        }
        else{
            color[tmp]=1;
            maxv=max(maxv,dfs(num+1,color));
             color[tmp]=0;
        }
        top[i]-=1;
    }
    return dp[top[0]][top[1]][top[2]][top[3]]=maxv;
}
int main()
{
    int color[21];
    while(~scanf("%d",&n)&&n){
        for(int i=0;i<n;++i)
            for(int j=0;j<4;++j)
            scanf("%d",&pile[j][i]);
        memset(dp,-1,sizeof(dp));
        memset(top,0,sizeof(top));
        memset(color,0,sizeof(color));
       printf("%d\n",dfs(0,color));
    }
return 0;
}
时间: 2024-10-10 08:38:39

Free Candies的相关文章

poj 3159 Candies

题目链接:http://poj.org/problem?id=3159 Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 22516   Accepted: 6047 Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought th

LeetCode解题思路:575. Distribute Candies

Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You need to distribute these candies equally in number to brother and sister.

hzau 1207 Candies

1207: Candies Time Limit: 2 Sec  Memory Limit: 1280 MBSubmit: 223  Solved: 31[Submit][Status][Web Board] Description Xiao Ming likes those N candies he collects very much. There are two kinds of candies, A and B. One day, Xiao Ming puts his candies i

poj3159 Candies(差分约束)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Candies Time Limit: 1500MS   Memory Limit: 131072K Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse’

poj 2886 Who Gets the Most Candies? (线段树单点更新应用)

poj 2886 Who Gets the Most Candies? Description N children are sitting in a circle to play a game. The children are numbered from 1 to N in clockwise order. Each of them has a card with a non-zero integer on it in his/her hand. The game starts from t

poj---(2886)Who Gets the Most Candies?(线段树+数论)

Who Gets the Most Candies? Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 10373   Accepted: 3224 Case Time Limit: 2000MS Description N children are sitting in a circle to play a game. The children are numbered from 1 to N in clockwise

poj 2886 Who Gets the Most Candies?(线段树+约瑟夫环+反素数)

Who Gets the Most Candies? Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 9934   Accepted: 3050 Case Time Limit: 2000MS Description N children are sitting in a circle to play a game. The children are numbered from 1 to N in clockwise o

Zepto Code Rush 2014——Dungeons and Candies

题目链接 题意: k个点,每个点都是一个n * m的char型矩阵.对与每个点,权值为n * m或者找到一个之前的点,取两个矩阵对应位置不同的字符个数乘以w.找到一个序列,使得所有点的权值和最小 分析: 首先,这个图是一个无向图.求权值和最小,每个权值对应的是一条边,且每个点只能有一个权值即一条边,一个k个边,和生成树很像,但是需要证明不能有环形.不妨假设现在有三个点,每个点的最小边成环,这时候是不能找到一个序列使得每个点都取到它的最小边值的,所以,k个点k个边不能有环且边值和最小,就是最小生成

poj3159 Candies

POJ - 3159 Candies 题目大意: 给n个小孩发糖,有m个条件 每个条件给出a,b,c,使得v[b]-v[a]<=c 求v[n]-v[1]的最大值 Sample Input 2 2 1 2 5 2 1 4 Sample Output 5 /* 差分约束裸题. 差分约束的基本模式为,给出若干个形如a-b<=c的不等式,问你x-y的最大值是多少 那么对每个不等式a-b<=c,连一条由b指向a权值为c的有向边,所有的不等式构成一个图 那么答案就是y到x的最短路 */ #inclu

[LeetCode] Distribute Candies 分糖果

Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You need to distribute these candies equally in number to brother and sister.