SDUT 3565 Feed the monkey【Dp】

本文解析了一道经典的算法题——喂猴子。题目要求在给定各种水果数量的情况下,设计喂食方案,确保不会连续超过特定天数喂同一种水果。文章详细介绍了使用动态规划解决该问题的方法。

Feed the monkey

Time Limit: 2000MS  Memory Limit: 131072KB
Problem Description

Alice has a monkey, she must feed fruit to the monkey every day.She has three kinds of fruits, bananas, 
peaches and apples. Every day, she chooses one in three, and pick one of this to feed the monkey. 
But the monkey is picky, it doesn’t want bananas for more than D1 consecutive days, peaches for more than D2 
consecutive days, or apples for more than D3 consecutive days. Now Alice has N1 bananas, N2 peaches and N3 
apples, please help her calculate the number of schemes to feed the monkey.

Input

 Multiple test cases. The first line contains an integer T (T<=20), indicating the number of test case.
Each test case is a line containing 6 integers N1, N2, N3, D1, D2, D3 (N1, N2, N3, D1, D2, D3<=50).

Output

 One line per case. The number of schemes to feed the monkey during (N1+N2+N3) days.
The answer is too large so you should mod 1000000007.

Example Input
1
2 1 1 1 1 1
Example Output
6
Hint

 Answers are BPBA, BPAB, BABP, BAPB, PBAB, and ABPB(B-banana P-peach A-apple)

Author
 “浪潮杯”山东省第七届ACM大学生程序设计竞赛

题目大意:

一共有三种食物,每一种食物的数量已知,每天吃任意一个食物,每一种的食物不能连续吃D1.D2.D3天,问可行的方案数有多少。


思路:


1、统计方案数,考虑dp.设定Dp【i】【j】【k】【l】【3】:

①Dp【i】【j】【k】【l】【0】:表示到第i天,第一种食物剩余j个,第二种食物剩余k个,第三种食物剩余l个,并且这天是吃了第一种食物的方案数。

②Dp【i】【j】【k】【l】【1】:表示到第i天,第一种食物剩余j个,第二种食物剩余k个,第三种食物剩余l个,并且这天是吃了第二种食物的方案数。

③Dp【i】【j】【k】【l】【2】:表示到第i天,第一种食物剩余j个,第二种食物剩余k个,第三种食物剩余l个,并且这天是吃了第三种食物的方案数。


2、那么有状态转移方程:


①Dp【i】【a】【b】【c】【0】+=dp【j】【a+cnt】【b】【c】【1】这里j<i,并且两个天数差小于等于D1.

①Dp【i】【a】【b】【c】【0】+=dp【j】【a+cnt】【b】【c】【2】这里j<i,并且两个天数差小于等于D1.

②Dp【i】【a】【b】【c】【1】+=dp【j】【a】【b+cnt】【c】【0】这里j<i,并且两个天数差小于等于D2.

②Dp【i】【a】【b】【c】【1】+=dp【j】【a】【b+cnt】【c】【2】这里j<i,并且两个天数差小于等于D2.

③Dp【i】【a】【b】【c】【2】+=dp【j】【a】【b】【c+cnt】【0】这里j<i,并且两个天数差小于等于D3.

③Dp【i】【a】【b】【c】【2】+=dp【j】【a】【b】【c+cnt】【1】这里j<i,并且两个天数差小于等于D3.


3、注意取模问题。

Ac代码:


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll ;
const ll MOD = 1e9+7;
ll dp[165][52][52][52][3];
int main(){
    ll t;
    scanf("%lld",&t);
    while(t--)
    {
        ll n1,n2,n3,d1,d2,d3;
        scanf("%lld%lld%lld%lld%lld%lld",&n1,&n2,&n3,&d1,&d2,&d3);
        memset(dp,0,sizeof(dp));
        for(ll i=1;i<=n1+n2+n3;i++)
        {
            ll cnt=0;
            for(ll j=i-1;j>=max(i-d1,0ll);j--)
            {
                cnt++;
                for(ll a=0;a<=n1;a++)
                {
                    for(ll b=0;b<=n2;b++)
                    {
                        ll c=n1+n2+n3-i-a-b;
                        if(c<=n3&&c>=0)
                        {
                            if(a+cnt>n1)continue;
                            if(j==0&&dp[i][a][b][c][0]==0)dp[i][a][b][c][0]+=1;
                            if(a+cnt<=n1)dp[i][a][b][c][0]=(dp[i][a][b][c][0]+dp[j][a+cnt][b][c][1])%MOD;
                            if(a+cnt<=n1)dp[i][a][b][c][0]=(dp[i][a][b][c][0]+dp[j][a+cnt][b][c][2])%MOD;
                        }
                    }
                }
            }
            cnt=0;
            for(ll j=i-1;j>=max(i-d2,0ll);j--)
            {
                cnt++;
                for(ll a=0;a<=n1;a++)
                {
                    for(ll b=0;b<=n2;b++)
                    {
                        ll c=n1+n2+n3-i-a-b;
                        if(c<=n3&&c>=0)
                        {
                            if(b+cnt>n2)continue;
                            if(j==0&&dp[i][a][b][c][1]==0)dp[i][a][b][c][1]+=1;
                            if(b+cnt<=n2)dp[i][a][b][c][1]=(dp[i][a][b][c][1]+dp[j][a][b+cnt][c][0])%MOD;
                            if(b+cnt<=n2)dp[i][a][b][c][1]=(dp[i][a][b][c][1]+dp[j][a][b+cnt][c][2])%MOD;
                        }
                    }
                }
            }
            cnt=0;
            for(ll j=i-1;j>=max(i-d3,0ll);j--)
            {
                cnt++;
                for(ll a=0;a<=n1;a++)
                {
                    for(ll b=0;b<=n2;b++)
                    {
                        ll c=n1+n2+n3-i-a-b;
                        if(c<=n3&&c>=0)
                        {
                            if(c+cnt>n3)continue;
                            if(j==0&&dp[i][a][b][c][2]==0)dp[i][a][b][c][2]+=1;
                            if(c+cnt<=n3)dp[i][a][b][c][2]=(dp[i][a][b][c][2]+dp[j][a][b][c+cnt][0])%MOD;
                            if(c+cnt<=n3)dp[i][a][b][c][2]=(dp[i][a][b][c][2]+dp[j][a][b][c+cnt][1])%MOD;
                        }
                    }
                }
            }
        }
        ll output=0;
        output=(output+dp[n1+n2+n3][0][0][0][0])%MOD;
        output=(output+dp[n1+n2+n3][0][0][0][1])%MOD;
        output=(output+dp[n1+n2+n3][0][0][0][2])%MOD;
        printf("%lld\n",output);
    }
}














评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值