---
title: "POJ 2663 Tri Tiling"
author: "hellobin"
date: "2013-05-02T17:54:05Z"
tags: []
categories: ["编程语言"]
slug: "poj-2663-tri-tiling"
draft: false
lastmod: "2013-05-02T17:54:05Z"
id: 1858862
url: "https://hellobinfeng.iteye.com/blog/1858862"
---

Description

In how many ways can you tile a 3xn rectangle with 2x1 dominoes?   
Here is a sample tiling of a 3x12 rectangle.   
![](http://poj.org/images/2663_1.jpg)

Input

Input consists of several test cases followed by a line containing -1. Each test case is a line containing an integer 0 &lt;= n &lt;= 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
```

Source

[Waterloo local 2005.09.24](http://poj.org/searchproblem?field=source&key=Waterloo%20local%202005.09.24)

 

动态规划的一道题

根据Stanford CS97SI课件

![](http://dl.iteye.com/upload/attachment/0083/9492/357a7c58-4794-3504-af60-dee984a53868.png)  
 

![](http://dl.iteye.com/upload/attachment/0083/9494/bb34f587-498e-338d-ada7-dc6696b27de9.png)  
   
![](http://dl.iteye.com/upload/attachment/0083/9496/505b5625-27be-384e-a017-4f87373e3a52.png)  
   
![](http://dl.iteye.com/upload/attachment/0083/9498/c737a5f9-089b-361b-8421-33b861f8f148.png)  
   
![](http://dl.iteye.com/upload/attachment/0083/9500/a31c2f1c-35fd-3135-87e3-6a6f47d3fb94.png)  
   
![](http://dl.iteye.com/upload/attachment/0083/9502/bb37aaed-cdc0-3bfd-8ac0-714fe28e0b2e.png)

根据本图，我们可以列出 **D(n)=D(n-2)+2\*A(n-1)**   
   
![](http://dl.iteye.com/upload/attachment/0083/9504/a0b082e9-7c87-37eb-9da9-9b8e2623cad3.png)  
 

 然后我们再分析 A(n)的情况： 

![](http://dl.iteye.com/upload/attachment/0083/9508/95fb1daf-5b7a-3bde-be3f-a5863a3a1117.png)  
 可以得到：有两种排列方式

**A(n)=D(n-1)+C(n-1)**

 

 再分析C(n):

![](http://dl.iteye.com/upload/attachment/0083/9521/1a21ab82-3c73-3931-a5b9-d27253d247e0.png)

可得：

**C(n)=A(n-1)**  
 

所以，我们有3个公式：

D(n)=D(n-2)+2\*A(n-1) 

A(n)=D(n-1)+C(n-1)

C(n)=A(n-1)

 

化简第一个公式得：

D(n)=D(n-2)+2\*A(n-1) 

= D(n-2)+2\*(D(n-2)+C(n-2))

= D(n-2)+2\*(D(n-2)+A(n-3))

= 3\*D(n-2)+2\*A(n-3)

= 3\*D(n-2)+2\*(D(n-4)+C(n-4))

= 3\*D(n-2)+2\*D(n-4)+2\*C(n-4)

= 3\*D(n-2)+2\*D(n-4)+2\*A(n-5)

= 3\*D(n-2)+2\*D(n-4)+2\*(D(n-6)+C(n-6))

= 3\*D(n-2)+2\*D(n-4)+2\*D(n-6)+2\*C(n-6)

= 。。。

 

其中base case有：

D(0)=1, D(2)=3

 

```
#define RUN
#ifdef RUN

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <vector>
#include <list>
#include <cctype> 
#include <algorithm>
#include <utility>
#include <math.h>

using namespace std;


int n;
int buf[31];

int main(){

	memset(buf, sizeof(buf), 0);
	buf[0] = 1;
	buf[2] = 3;

	while(scanf("%d",&n)==1 && n!=-1){
		if(n%2 != 0){
			printf("0\n");
			continue;
		}

		for(int i=4; i<=n; i+=2){
			int tmp = 3*buf[i-2];
			for(int j=4; j<=i; j+=2){
				tmp += 2*buf[i-j];
			}
			buf[i] = tmp;
		}

		printf("%d\n", buf[n]);
	}
	
}


#endif
```