SDUT 3565 Feed the monkey(DP)

题目地址:点击打开链接


题意:给你三种水果的数量和每一个水果不能连续的数量,问有多少中组合方式。


思路:记忆化搜索,dp[num1][num2][num3][pre][con],num1 num2 num3表示三种水果剩余数,pre表示上一个放的

是哪个水果,con表示上一个水果已经连续放了多少天。


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 55;
const int mod = 1e9+7;
int dp[maxn][maxn][maxn][3][maxn];
int n1, n2, n3, d1, d2, d3;

int dfs(int num1, int num2, int num3, int pre, int con)
{
    if(num1 < 0 || num2 < 0 || num3 < 0) return 0;
    if((pre==0&&con>d1) || (pre==1&&con>d2) || (pre==2&&con>d3)) return 0;
    if(!num1 && !num2 && !num3) return 1;
    if(dp[num1][num2][num3][pre][con] != -1) return dp[num1][num2][num3][pre][con];
    int ans = 0;
    if(pre == 0)
    {
        ans = (ans+dfs(num1-1, num2, num3, 0, con+1))%mod;
        ans = (ans+dfs(num1, num2-1, num3, 1, 1))%mod;
        ans = (ans+dfs(num1, num2, num3-1, 2, 1))%mod;
    }
    else if(pre == 1)
    {
        ans = (ans+dfs(num1-1, num2, num3, 0, 1))%mod;
        ans = (ans+dfs(num1, num2-1, num3, 1, con+1))%mod;
        ans = (ans+dfs(num1, num2, num3-1, 2, 1))%mod;
    }
    else
    {
        ans = (ans+dfs(num1-1, num2, num3, 0, 1))%mod;
        ans = (ans+dfs(num1, num2-1, num3, 1, 1))%mod;
        ans = (ans+dfs(num1, num2, num3-1, 2, con+1))%mod;
    }
    return dp[num1][num2][num3][pre][con] = ans;
}

int main(void)
{
    int t;
    cin >> t;
    while(t--)
    {
        memset(dp, -1, sizeof(dp));
        scanf("%d%d%d%d%d%d", &n1, &n2, &n3, &d1, &d2, &d3);
        int ans = 0;
        ans = (ans+dfs(n1-1, n2, n3, 0, 1))%mod;
        ans = (ans+dfs(n1, n2-1, n3, 1, 1))%mod;
        ans = (ans+dfs(n1, n2, n3-1, 2, 1))%mod;
        printf("%d\n", ans);
    }
    return 0;
}



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)



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值