CUC-SUMMER-5-B

B - Tri Tiling
HDU - 1143

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


題意:瓷磚問題,3 x n的區(qū)域用3 x 1的瓷磚鋪一共有多少種方案。

解法:dp算法,遞推,有三種基礎(chǔ)排法,所以f(n)=3f(n-2),但還要考慮與前面相連的情況,所以f(n)=3f(n-2)+2f(n-4)+2f(n-6)+······+2f(2),所以得到f(n)=4*f(n-2)-f(n-4)

代碼:

#include<iostream>
using namespace std;
int main()
{
    int a[31]={0,};
    a[0]=1,a[2]=3;
    for(int i=4;i<=30;i+=2)
        a[i]=4*a[i-2]-a[i-4];
    int n;
    while(cin>>n&&n!=-1)
        cout<<a[n]<<endl;
    return 0;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容