POJ2663——Tri Tiling

175 篇文章 0 订阅
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.

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,具体的资料大家可以去问百度,比我说的详细多了(我只是粗略讲一下)

#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;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值