Tiling_easy version


Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5856    Accepted Submission(s): 4601



Problem Description


有一个大小是 2 x n 的网格,现在需要用2种规格的骨牌铺满,骨牌规格分别是 2 x 1 和 2 x 2,请计算一共有多少种铺设的方法。


 



Input


输入的第一行包含一个正整数T(T<=20),表示一共有 T组数据,接着是T行数据,每行包含一个正整数N(N<=30),表示网格的大小是2行N列。


 



Output


输出一共有多少种铺设的方法,每组数据的输出占一行。


 



Sample Input


3 2 8 12


 



Sample Output


3 171 2731


 



Source


《ACM程序设计》短学期考试_软件工程及其他专业
题目分析;第n个位置有两种方法装满,一种是通过n-2的装满情况通过1*2的砖平着叠放,或者一个2*2的块直接放置,也可以通过n-1然后用1*2竖着放,所以转移即可

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#define MAX 37

using namespace std;

typedef long long LL;

int t,n;
LL dp[MAX];

int main ( )
{
    scanf ( "%d" , &t );
    while ( t-- )
    {
        scanf ( "%d" , &n );
        memset ( dp , -1 , sizeof ( dp ) );
        dp[0] = 1;
        for ( int i = 1 ; i <= n ; i++ )
            for ( int j = n ; j >= 1 ; j-- )
                if ( j - 2 >= 0 )
                    dp[j] = max ( dp[j] , dp[j-1] + 2*dp[j-2] );
                else if ( j - 1 >= 0 )
                    dp[j] = max ( dp[j] , dp[j-1] );
        printf ( "%lld\n" , dp[n] );
    }
}