HDU 1143 Tri Tiling 【递推】

来源:互联网 发布:网络拓扑图生成 编辑:程序博客网 时间:2024/05/21 12:05

题目描述

Problem Description

In how many ways can you tile a 3xn rectangle with 2x1 dominoes? Here is a sample tiling of a 3x12 rectangle.


输入

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

输出

For each test case, output one integer number giving the number of possible tilings.

示例输入

2 8 12 -1

示例输出

3 153 2131

首先只有是偶数才非零,只看偶数就可以了。来一个数n,它比上一个数差2,而2能摆出三种情况,这样如果这个2和前面是分开的,这种情况是 f[n-2] * 3,剩下就要考虑不分开的情况,这也是关键。

与前面相连接。连接的方案有 2 * (a[i-2] + a[i-3] +......+a[0]);

解释一下,2*a[i-X]是最右边有X列类似以下的结构的情况:

X=4列的情况:;X=6列的情况;等等

以上情况可以上下颠倒,故每种情况又有两种表示,所以需要乘以2。而以上的情况从4开始,然后每次递增2,所以递推式中这部分从i-4开始(如果大等于0的话),每次递减2。

综上,a[i] = 3*a[i-1] + 2*(a[i-2] + a[i-3] +......+a[0]);a[i-1] = 3*a[i-2] + 2*(a[i-3] + a[i-3] +......+a[0]);a[i-1] - a[i-2] = 2*(a[i-2] + a[i-3] +......+a[0]);

ps: here I  regard each 2 columns to one

 

可解得a[i] = a[i-1] * 4 - a[i-2];

 
 
#include <stdio.h>int main(){int n;int f[31];f[0]=1;f[2]=3;int i;for(i=1;i<=30;i=i+2)f[i]=0;for (i=4;i<=30;i=i+2)f[i]=f[i-2]*4-f[i-4];while (scanf("%d",&n)&&n!=-1)printf("%d\n",f[n]);return 0;}

0 0
原创粉丝点击