首页 > 代码库 > hdu2563(递推dp)
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 longusing 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]); }}
hdu2563(递推dp)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。