题目描述:
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
题目分类:递推,DP
因为我对DP不熟,所以我用数学递推,由于我不是能一眼就看出规律,所以只能用最笨的方法——画。
这道题的意思是:有一个3n的矩形,先给你12的小矩形(个数不限),问你要用小矩形把大矩形填满,有多少种方案。
个人方法:
这道题如果画出前面几个你就会发现,如果n是奇数的话是不可能填满大矩形的,所以方案为0;当n为偶数时根据画图的方案数可以推出规律。
综上所述:递推公式为f(n) = 4 * f(n-2) - f(n-4).
特别的:f(0) = 1,f(1) = 0,f(2) = 3,f(3) = 0.
参考代码:
#include <iostream>
using namespace std;
int result(int n) {
int res;
if (n == 0) {
res = 1;
}
else if (n == 1) {
res = 0;
}
else if (n == 2) {
res = 3;
}
else if (n == 3) {
res = 0;
}
else {
res = 4 * result(n-2) - result(n-4);
}
return res;
}
int main() {
int n;
while (cin >> n && n != -1) {
int res = result(n);
cout << res << endl;
}
return 0;
}
方法不是特别好,请谅解。