POJ2663 Tri Tiling

vjudge传送


这道题有两种做法。


我相信第一种做法人人都会:状压dp。
\(dp[i][S]\)表示到第\(i\)列,且第\(i\)列状态为\(S\)时的方案数。其中\(S\)\(0\)~\(2^3-1\),即三个二进制位,0表示这一格平的,1表示这一格是突出来的,即占了下一列。
接下来考虑转移,对于状态\(S_1\)能转移到\(S_2\),当且仅当:
1.\(S_1\)是1的格子\(S_2\)不能填,即1->0,所以需要\(S_1\)&\(S_2 = 0\)
2.如果\(S_2\)想要竖着填,需要\(S_1\)这两位都是0,那么\(S_1 | S_2\)的结果必须是有连续偶数个0.
第二条预处理出合法的数(只有1,4,7).

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<queue>
#include<assert.h>
#include<ctime>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
#define forE(i, x, y) for(int i = head[x], y; ~i && (y = e[i].to); i = e[i].nxt)
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
//const int maxn = ;
In ll read()
{
	ll ans = 0;
	char ch = getchar(), las = ' ';
	while(!isdigit(ch)) las = ch, ch = getchar();
	while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
	if(las == '-') ans = -ans;
	return ans;
}
In void write(ll x)
{
	if(x < 0) x = -x, putchar('-');
	if(x >= 10) write(x / 10);
	putchar(x % 10 + '0');
}
In void MYFILE()
{
#ifndef mrclr
	freopen(".in", "r", stdin);
	freopen(".out", "w", stdout);
#endif
}
int n, eve[8];
int dp[32][8];


int main()
{
//	MYFILE();
	eve[1] = eve[4] = eve[7] = 1;
	while(scanf("%d", &n) && ~n)
	{
		dp[0][0] = 1;
		for(int i = 1; i <= n; ++i)
			for(int j = 0; j < 8; ++j)
			{
				dp[i][j] = 0;
				for(int k = 0; k < 8; ++k)
					if(!(j & k) && eve[j | k]) dp[i][j] += dp[i - 1][k];
			}
		write(dp[n][0]), enter;
	}
	return 0;	
}

另一种做法是教练说他从一个外国人那里看到的。
\(f(n)\)表示答案,\(g(n)\)表示这一列有一个角没填上的方案数,考虑转移:
1.\(f(n)\):如果三张骨牌都横着放,那么从\(f(n-2)\)转移;否则只能一张横着放一张竖着放,从两个\(g(n-1)\)转移,即\(f(n)=f(n-2)+g(n-1)+g(n-1)\)
2.\(g(n)\):一张骨牌竖着放,从\(f(n-1)\)转移;或是三张骨牌横着放,只不过靠外的一张错开了,于是从\(g(n-2)\)转移,即\(g(n)=f(n-1)+g(n-2)\)
初始化:\(f(0)=1,f(1)=0,g(0)=0,g(1)=1\)
这样就变成了一个线性递推。

posted @ 2020-10-18 23:20  mrclr  阅读(60)  评论(0编辑  收藏  举报