HDU 1143 Tri Tiling

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1143

Tri Tiling

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 2799 Accepted Submission(s): 1585

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

Source

University of Waterloo Local Contest 2005.09.24

Recommend

Eddy

大意——3*n的长方形方格,用2*1的骨牌铺满。问:给定一个n,求解总的方案数。

思路——显然这是一道递推题目。很明显地可以发现,当n为奇数时,是无解的。而且f(0)=1,f(2)=3,现在我们考察最后两列:如果最后两列铺满,则有3种方案,所以方案数为3*f(n-2);如果最后两列不铺满,则一定是后面四列组合,有2种方案,所以方案数为f(n-4)。以此类推,当n>=4时,f(n)=3*f(n-2)+2*f(n-4)+2*f(n-6)+···+2*f(0)。令2*m=n,则f(m)=3*f(m-1)+2*f(m-2)+···+2*f(0),f(m-1)=3*f(m-2)+2*f(m-3)+···+2*f(0),两式相减并整理得到,f(m)=4*f(m-1)-f(m-2)。从而,f(n)=4*f(n-2)-f(n-4),n>=4,并且n为偶数。又因为n最大为30,则直接递推即可。

复杂度分析——时间复杂度:O(n),空间复杂度:O(n)

附上AC代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <iomanip>
#include <ctime>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
typedef unsigned int UI;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
const double PI = 3.14159265;
const double E = 2.71828182846;
LL num[31] = {1, 0, 3, 0};

void init();

int main()
{
	ios::sync_with_stdio(false);
	init();
	short n;
	while (cin >> n && n != -1)
	{
		cout << num[n] << endl;
	}
	return 0;
}

void init()
{
	for (int i=4; i<31; i++)
		num[i] = 4*num[i-2]-num[i-4];
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-27 18:26:06

HDU 1143 Tri Tiling的相关文章

HDU 1143 Tri Tiling (递推)

Tri Tiling Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2118    Accepted Submission(s): 1211 Problem Description In how many ways can you tile a 3xn rectangle with 2x1 dominoes? Here is a sa

HDU 1143 Tri Tiling(递归)

意甲冠军:一些现有的1*2小盒子.求拼3*n多少个长方形的拼写. 思考: 因为它是一个递归式.肯定会遇到层的关系.仔细观察,研究发现,每层应设置2一层.(奇数层不能是矩形)而从显卡好最后一层的最后一战,可以发现.只有两种结果(). 即: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxNDU2OTU5OA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Cent

HDU 1143 Tri Tiling(递推)

题意:现有一些1*2的小方块,求拼成3*n的矩形有多少种拼法. 思路: 既然是递推式,肯定要遇上一层发生关系.仔细观察,发现每一层应该设为2层,(奇数层不可能是矩形)而从上一次拼好的图形中的最后一层可以发现,只有两种结果(对称的也先算一种). 即:.结果二可以==上一层的结果一和结果二两种结果(很明显,不多说,用笔画一下便知).结果一可以==2*(上一层的结果一和结果二)以及结果一.为什么呢?上一层的两种结果都可以分别衍生出1和2两种情况,即2*(上一层的结果一和结果二),而如果上一层为结果一的

HDU 1143 Tri Tiling 递归问题

将一个3*n的矩形用1*2的矩形填充,n为奇数时一定不能被填满,n*3%2==1 接下来处理这个问题我们要从简单的情况开始考虑,所谓递归就是要能将问题的规模不断减小,通过小问题的解决最后将复杂问题解决.如果是3*2的情况仅有3种可能 那么我们来考虑3*4的矩形是否一些就是有这两个3*2的矩形拼接在一起的多种组合,当然还包含一种特殊的情况即如下图所示的拼接方法 把 4, 6, 8.... 看成一整块,就有下图两种情况(正着,倒着) 那么我们可以将其排成的各种情况分成如下两大类 由自身单独构成的矩形

Tri Tiling(hdu1143)

Tri Tiling Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2731    Accepted Submission(s): 1547 Problem Description In how many ways can you tile a 3xn rectangle with 2x1 dominoes? Here is a sa

POJ 2663 Tri Tiling

Tri Tiling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10306   Accepted: 5237 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

I - Tri Tiling

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status 1 #include<cstdio> 2 using namespace std; 3 int f(int n) 4 { 5 if(n==0) 6 return 1; 7 if(n==2) 8 return 3; 9 else 10 return 4*f(n-2)-f(n-4); 11 } 12 int m

Tri Tiling

Tri Tiling 三瓷砖 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2676    Accepted Submission(s): 1511 Problem Description In how many ways can you tile a 3xn rectangle with 2x1 dominoes? Here is

poj2663 Tri Tiling

Tri TilingTime Limit: 1000MSMemory Limit: 65536KTotal Submissions: 5843Accepted: 3128 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 case