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

 

posted on 2014-11-24 12:16  lienus  阅读(186)  评论(0编辑  收藏  举报

导航