您的位置:首页 > 其它

HDU 2046 骨牌铺方格

2015-08-15 10:24 375 查看
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2046

骨牌铺方格
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 35779 Accepted Submission(s): 17390

Problem Description

在2×n的一个长方形方格中,用一个1× 2的骨牌铺满方格,输入n ,输出铺放方案的总数.

例如n=3时,为2× 3方格,骨牌的铺放方案有三种,如下图:



Input

输入数据由多行组成,每行包含一个整数n,表示该测试实例的长方形方格的规格是2×n(0<n<=50)。

Output

对于每个测试实例,请输出铺放方案的总数,每个实例的输出占一行。

Sample Input

1

3

2

Sample Output

1

3

2

Author

lcy

Source

递推求解专题练习(For Beginner)

Recommend

lcy

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

思路——显然f(1)=1,f(2)=2,f(3)=3,现在我们考察最后一列:如果最后一列是竖着放的,那么方案数为f(n-1);如果最后一列是横着放的,那么必是两个横着放,此时方案数为f(n-2).因此,f(n)=f(n-1)+f(n-2),n>2,即为Fibonacci数。又因为n最大为50,所以直接递推即可。

复杂度分析——时间复杂度: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;

int main()
{
	ios::sync_with_stdio(false);
	LL num[51] = {1, 1, 2};
	for (int i=3; i<51; i++)
		num[i] = num[i-1]+num[i-2];
	int n;
	while (cin >> n)
	{
		cout << num
 << endl;
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: