HOJ 2124 &POJ 2663Tri Tiling(动态规划)
2016-03-04 07:34
309 查看
Tri Tiling
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9016 Accepted: 4684
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
之前也写过这种堆方块,就是找递推的方程,还是比较简单的题目。但是这道题目却是一个升级,因为他的递推方程需要变形,所以以后遇到这类题目就又多了一点见识。
dp
=3*dp[n-2]+2*dp[n-4]+2*dp[n-6]+……2*dp[0];
如果只拿这个方程去解肯定麻烦或者还可能超时
变形
dp[n-2]=3*dp[n-4]+2*(dp[n-6]+dp[n-8]+…..dp[0]);
dp[n-2]-dp[n-4]=2*(dp[n-4]+dp[n-6]+dp[n-8]+….dp[0]);
dp
=4*dp[n-2]-dp[n-4];
就搞定了,
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9016 Accepted: 4684
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
之前也写过这种堆方块,就是找递推的方程,还是比较简单的题目。但是这道题目却是一个升级,因为他的递推方程需要变形,所以以后遇到这类题目就又多了一点见识。
dp
=3*dp[n-2]+2*dp[n-4]+2*dp[n-6]+……2*dp[0];
如果只拿这个方程去解肯定麻烦或者还可能超时
变形
dp[n-2]=3*dp[n-4]+2*(dp[n-6]+dp[n-8]+…..dp[0]);
dp[n-2]-dp[n-4]=2*(dp[n-4]+dp[n-6]+dp[n-8]+….dp[0]);
dp
=4*dp[n-2]-dp[n-4];
就搞定了,
#include <iostream> #include <string.h> #include <stdlib.h> #include <math.h> #include <algorithm> using namespace std; int dp[35]; int n; int main() { dp[0]=1; dp[1]=0; dp[2]=3; for(int i=3;i<=30;i++) { if(i&1) dp[i]=0; else dp[i]=dp[i-2]*4-dp[i-4]; } while(scanf("%d",&n)!=EOF) { if(n==-1) break; printf("%d\n",dp ); } return 0; }
相关文章推荐
- HOJ 2124 &POJ 2663Tri Tiling(动态规划)
- 来北京的第四天
- 59. Spiral Matrix II
- srs之防盗链配置及使用
- 自动化运维工具puppet安装配置
- HOJ 2133&POJ 2964 Tourist(动态规划)
- HOJ 2133&POJ 2964 Tourist(动态规划)
- Java中的多态的详细讲解
- DDOS--DNS Query Flood
- [LeetCode] Meeting Rooms 会议室
- OSChina 周五乱弹 ——论HR的日常心塞
- 3309: DZY Loves Math 莫比乌斯反演
- java8新特性2-方法引用
- redis 安装配置
- 夺命雷公狗---微信开发39----微信语言识别接口1
- 54. Spiral Matrix
- 基于RxJava、RxAndroid的EventBus实现
- 准备 KVM 实验环境 - 每天5分钟玩转 OpenStack(3)
- Handmade Hero全程直播游戏引擎开发
- android调用第三方库——第一篇 (转载)