Tri Tiling
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7915 | Accepted: 4148 |
Description
In how many ways can you tile a 3xn rectangle with 2x1 dominoes?
Here is a sample tiling of a 3x12 rectangle.
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
Waterloo local 2005.09.24
水的状压dp,仔细看n最大可以到三十,所以把行和列交换一下
我们把连续两行的放置状态看成1个状态,设0表示当前位置不放,1表示当前位置放
竖着放置设置为上一列为0,当前列为1,
所以,如果当前列是0,那么上一列只能是1,如果当前列是1,上一列可以是0,;如果上一列也是1,那么当前行的下一列一定是1,上一行的下一列也一定是1,具体的资料大家可以去问百度,比我说的详细多了(我只是粗略讲一下)
水的状压dp,仔细看n最大可以到三十,所以把行和列交换一下
我们把连续两行的放置状态看成1个状态,设0表示当前位置不放,1表示当前位置放
竖着放置设置为上一列为0,当前列为1,
所以,如果当前列是0,那么上一列只能是1,如果当前列是1,上一列可以是0,;如果上一列也是1,那么当前行的下一列一定是1,上一行的下一列也一定是1,具体的资料大家可以去问百度,比我说的详细多了(我只是粗略讲一下)
#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int dp[100][100];
struct node
{
int cur, last;
}status[100];
int m, res;
void dfs(int l, int cur, int last)
{
if (l > m)
{
return ;
}
if (l == m)
{
status[res].cur = cur;
status[res++].last = last;
return ;
}
dfs( l + 1, (cur << 1), (last << 1 | 1) );
dfs( l + 1, (cur << 1 | 1), (last << 1) );
dfs( l + 2, (cur << 2 | 3), (last << 2 | 3));
}
int main()
{
while (~scanf("%d", &m) && m != -1)
{
int n = 3;
if (m & 1)
{
printf("0\n");
continue;
}
if (m > n)
{
swap(n, m);
}
res = 0;
dfs(0, 0, 0);
memset ( dp, 0, sizeof(dp) );
dp[0][(1 << m) - 1] = 1;
for (int i = 1; i <= n; i++)
{
for (int j = 0; j < res; j++)
{
dp[i][status[j].cur] += dp[i - 1][status[j].last];
}
}
printf("%d\n", dp[n][(1 << m) - 1]);
}
return 0;
}