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;
}

 

转载于:https://www.cnblogs.com/zsf123/p/4909426.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值