Tri Tiling (poj2663)

DescriptionIn
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 Input2
8
12
-1
Sample Output3
153
2131
思路:
①a[0]=1;
②把2块看成一个整体,两块的情况下只有3种情况,a[2]=3;
③4块的时候:如果都是2块2块可分割的,那么a[4]=3a[2]=9;但是如果这4块不可分割,就有2种情况,a[4]=a[4]+2a[0]=11;
④6块的时候:加上的这两块有3种情况,如果和另外4块是可分割的,那么a[6]=3a[4]=33,如果这两块和靠近它们的那两块是不可分割的,也就是4块都不可分割,两种情况,最左边还有个3种情况的a[2],所以a[6]=a[6]+2a[2]=39,如果6块都不可分割,又多了2种情况,即a[6]=a[6]+2a[0]=41;
这样递推下去式子就出来了:a[n]=3
a[n-2]+2*(a[n-4]+a[n-6]+…+a[0]);
代码:

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    int n;
    int a[31];
    while(1){
    cin>>n;
    memset(a,0,sizeof(a));
    if(n==-1){return 0;}
    a[0]=1;
    for(int i=2;i<=n;i+=2)
    {
        a[i]=3*a[i-2];
        for(int j=i-4;j>=0;j-=2){
            a[i]+=2*a[j];
        }
    }
    cout<<a[n]<<endl;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值