您的位置:首页 > 其它

【POJ 2663】Tri Tiling(dp|递推)

2016-10-27 20:26 573 查看
Tri Tiling

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9912 Accepted: 5072
Description

In how many ways can you tile a 3xn rectangle with 2x1 dominoes? 

Here is a sample tiling of a 3x12 rectangle. 



Input

Input consists of several test cases followed by a line containing -1. Each test case is a line containing an integer 0 <= n <= 30.
Output

For each test case, output one integer number giving the number of possible tilings.
Sample Input
2
8
12
-1

Sample Output
3
153
2131

Source

Waterloo local 2005.09.24
[Submit]   [Go Back]   [Status]  
[Discuss]


Home Page   

Go
Back  

To top

[题意][用1×2的小矩阵填充3×n,一共有多少种方法]
【题解】【递推】

【明明是dp,却硬生生的写成了递推 】

【完整的2*3矩形有3种拼法,所以f
+=3*f[n-2]。 


  当突出两块时:突出的两块时上侧两个:发现往后拼只有一种方法。突出的两块在下侧时同样一个,于是用这种突出的方法拼出f
有两种(就是以三个一组,两组并在一起时,将中间的两个竖放的改为横着的)。即f
+=2*(f[n-4]+f[n-6]+..+f[0]).

【于是f
=3*f[n-2]+2*(f[n-4]+..+f[0]),求通项得:f
=4*f[n-2]-f[n-4].】

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int f[40];
int main()
{
int i,j;
f[0]=1; f[2]=3;
for (i=4;i<=30;i+=2)
f[i]=4*f[i-2]-f[i-4];
while (scanf("%d",&j)==1&&j!=-1)
printf("%d\n",f[j]);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dp poj