http://poj.org/problem?id=2663
题目
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
思路
推导出公式,因为输入n不大,可以提前求出所有结果保存下来。 手写的推导过程,写的不太整齐-_-||。注意本题n=0时需要输出1。

代码
#include <iostream>
int main() {
const int MAX_COUNT = 31;
int result[MAX_COUNT];
memset(result, 0, sizeof(result));
result[0] = 1;
int cum = 0;
for (int i = 2; i < MAX_COUNT; i += 2) {
result[i] = 3 * result[i - 2];
result[i] += cum * 2;
cum += result[i - 2];
//printf("%d\n", result[i]);
}
int n = 0;
while (scanf("%d", &n) != EOF && n != -1) {
printf("%d\n", result[n]);
}
return 0;
}
这篇博客介绍了如何利用动态规划解决一个经典的数学问题:用2x1的多米诺骨牌铺满3xn形状的矩形。作者给出了输入输出示例,并展示了代码实现,通过预计算所有可能的结果并存储,然后根据给定的n值输出对应的铺砌方式数量。代码中包含了关键的递推公式推导和优化细节。

被折叠的 条评论
为什么被折叠?



