UVA 1638(动态规划)

27 篇文章 0 订阅
9 篇文章 0 订阅

有高为1到n的杆子各一根排成一行,从左往右能看见l根,从右往左能看见r根,问可能的方案数。

dp[i][j][k]表示i根高度不同的杆子从左往右能看见j根,从右往左能看见k根,根据高度最低的那根杆子分类。


#include <stdio.h>
#include <string.h>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 30 + 6;
ll f[maxn][maxn][maxn];/*f[i][l][r]表示i根棍子从左边能看见l根从右边能看见r根*/
/*转移为f[i][l][r] = f[i-1][l-1][r] + f[i-1][l][r-1] + (i-2)*f[i-1][l][r]*/
const int mod = 10056;
ll dfs(int tot,int l,int r)
{
    ll & res = f[tot][l][r];
    if(res != -1)return res;
    if(tot==1&&l==r&&r==1)return res = 1;
    else if(tot==1)return res = 0;
    res = dfs(tot-1,l-1,r) + dfs(tot-1,l,r-1) + (tot-2)*dfs(tot-1,l,r);
    return res;
}
int main(int argc, char const *argv[])
{
    int T; scanf("%d",&T);
    memset(f,-1,sizeof f);
    for(int cas = 1,n; cas <= T; ++cas) {
        int a,b;
        scanf("%d%d%d",&n,&a,&b);
        printf("%lld\n", dfs(n,a,b));
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值