hdu2563(递推dp)

 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2563

解题思路:
要分两种情况来考虑,a(n)为向上,b(n)为向左跟向右,f(n)为当前方案数。
a(n) = a(n-1) + b(n-1);因为向上只有一个方向。
b(n) = a(n-1) * 2 + b(n-1);因为之前的向上可以走两个方向,而之前的向左或者
向右只能继续按照原来的方向走,因为走过的路会消失。
f(n) = a(n) + b(n);
所以可以推出:
f(n) = f(n-1) * 2 + a(n-1) = f(n-1) * 2 + f(n-2);


启发:
    对主问题的切割,要灵活!

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#define LL long long
using namespace std;
int f[25];
void init()
{
    f[1]=3;f[2]=7;
    for(int i=3;i<=20;i++)f[i]=2*f[i-1]+f[i-2];
}
int main()
{
    int t,n;
    scanf("%d",&t);
    init();
    while(t--)
    {
        scanf("%d",&n);
        printf("%d\n",f[n]);
    }
}
View Code

 

转载于:https://www.cnblogs.com/lienus/p/4118363.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值