码迷,mamicode.com
首页 > 其他好文 > 详细

hdu2563(递推dp)

时间:2014-11-24 13:18:41      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   for   

 题目链接: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);


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

bubuko.com,布布扣
#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

 

hdu2563(递推dp)

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:http://www.cnblogs.com/lienus/p/4118363.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!