向前走莫回头❤

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

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

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top


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

【题解】【递推】

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

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

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

【于是f[n]=3*f[n-2]+2*(f[n-4]+..+f[0]),求通项得:f[n]=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;
}


posted @ 2016-10-27 20:27  lris0-0  阅读(85)  评论(0编辑  收藏  举报
过去的终会化为美满的财富~o( =∩ω∩= )m