hdu1143 Tri Tiling
来源:互联网 发布:风速仪上位机软件 编辑:程序博客网 时间:2024/05/22 12:22
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
2812-1
Sample Output
31532131
参考了牛牛们的思路。
因为牌的大小为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;}
1 0
- HDU1143:Tri Tiling
- Tri Tiling HDU1143
- hdu1143 Tri Tiling
- Tri Tiling(hdu1143)
- hdu1143 Tri Tiling
- hdu1143 Tri Tiling
- hdu1143 Tri Tiling 递推
- HDU1143 - Tri Tiling(插头DP)
- HDU1143 Tri Tiling 递推
- hdu1143 Tri Tiling(数学:递推)
- hdu1143 Tri Tiling(递推)
- Tri Tiling
- Tri Tiling
- Tri Tiling
- Tri Tiling
- Tri Tiling
- Tri Tiling Problem
- 1121. Tri Tiling
- Swift - 使用导航条和导航条控制器来进行页面切换
- 8.Oracle BBED解析数据块——BBED基本使用五
- 9.Oracle BBED解析数据块——BBED基本案例一改变数据
- 使用Jquery获取Bootstrap Radio Group当前选中的单选框的值
- textView输入无效
- hdu1143 Tri Tiling
- opengl es画凹多边形问题:切割凹多边形为三角形
- c语言俄罗斯方块
- linux运行时动态调整内核参数工具sysctl学习小结
- 互联网名人巨头演讲,会议。
- JAVA基础笔记3--JDBC入门
- 设计模式简介
- 蓝桥杯:基础练习 十六进制转十进制
- 解决layer层圆角引发的性能问题