hdu1143 Tri Tiling

来源:互联网 发布:风速仪上位机软件 编辑:程序博客网 时间:2024/05/22 12:22

Tri Tiling

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3056    Accepted Submission(s): 1720


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
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
2812-1
 

Sample Output
31532131

参考了牛牛们的思路。

因为牌的大小为2,大矩形宽为3,所以当n为奇数时,大矩形面积为奇数,所以当n为奇数时可能的组合为0.

当n = 2时有三种

当n = 4时,可以拆成(2, 2)和(4, 0)两种,

n = 6时,拆分成(2, 4)和 (6, 0)

有规律: f(n) = 3*f(n - 2) + 2*f(n - 4) + 2*f(n - 6) + ····· + 2*f(0),f(0) = 1;

即每次把n划为一块不可以用竖直线分割的小矩形,(2, n-2) ,(4, n - 4), (6, n - 6).....

2不可以再细分,4、6..(n > 2的偶数)是一个不可细分的整体(如上图4),如果细分则会造成重复计算

用 f(n) - f(n - 2) ,可化简为:f(n) = 4*f(n - 2) - f(n - 4);

以上是本人理解,欢迎拍砖。

代码:

#include <iostream>using namespace std;int a[35] = {0};int f(int n){    if (n > 3)        return a[n] = 4*f(n - 2) - f(n - 4);    else if (n == 0)        return 1;    return 3;}int main(){    int n;    a[0] = 1;    a[2] = 3;    f(30);    while (cin >> n && n != -1)    {        cout << a[n] << endl;    }    return 0;}


1 0
原创粉丝点击