Feed the monkey_dp_递推

本文介绍了一个经典的编程竞赛问题——喂猴策略问题。该问题要求在考虑猴子对不同水果连续食用天数限制的情况下,计算出所有可能的喂食方案数量。通过动态规划的方法,有效地解决了这一问题,并提供了一个具体的代码实现案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Feed the monkey

Time Limit: 2000 msMemory Limit: 131072 KiB
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.

Sample Input
1
2 1 1 1 1 1
Sample Output
6
Hint

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

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


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1e9+7;

const int N=200;
ll dp[N][N][N][3];

int n1,n2,n3,d1,d2,d3;


int main()
{
    //freopen("in.txt","r",stdin);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        cin>>n1>>n2>>n3>>d1>>d2>>d3;
        memset(dp,0,sizeof(dp));

        for(int i=n1;i>=0;i--)
        for(int j=n2;j>=0;j--)
        for(int k=n3;k>=0;k--){
            for(int s=1;s<=min(d1,i);s++){
                if(i==n1&&j==n2&&k==n3){
                    dp[i-s][j][k][0]=1;
                }else{
                    dp[i-s][j][k][0]=((dp[i-s][j][k][0]+dp[i][j][k][1])%mod+dp[i][j][k][2])%mod;
                }
            }
            for(int s=1;s<=min(d2,j);s++){
                if(i==n1&&j==n2&&k==n3){
                    dp[i][j-s][k][1]=1;
                }else{
                    dp[i][j-s][k][1]=((dp[i][j-s][k][1]+dp[i][j][k][0])%mod+dp[i][j][k][2])%mod;
                }
            }
            for(int s=1;s<=min(d3,k);s++){
                if(i==n1&&j==n2&&k==n3){
                    dp[i][j][k-s][2]=1;
                }else{
                    dp[i][j][k-s][2]=((dp[i][j][k-s][2]+dp[i][j][k][1])%mod+dp[i][j][k][0])%mod;
                }
            }
        }

        ll ans=((dp[0][0][0][0]+dp[0][0][0][1])%mod+dp[0][0][0][2])%mod;
        cout<<ans<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值