hdu1143 Tri Tiling

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
  
  
2 8 12 -1
 

Sample Output
  
  
3 153 2131

参考了牛牛们的思路。

因为牌的大小为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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值