CF 342D

CF 342D

题意:给出一个N*3的方块,要求用2*1的方砖填到只剩下一个1*1的空位,要求空位四周至少有一个方砖用长度为1的面对着它,求方案数。好恶心,英语渣打完一遍才发现看错题了。

状态压缩+容斥原理。

如果只要求填满,那么就很简单了,随便找个模板套上去就差不多了,但是后面一个要求有点麻烦,首先假设空位的左边有一块符合条件的方砖,那么就把他自身跟左边两个格子都填上不能放置的标志,然后其他地方就用简单状态压缩去做,右面,上下两面都是这样处理,但是这样求出来的方案数有重复,所以用容斥原理求出同时有两个/三个/四个方向都有符合条件的方砖,再做几次状态压缩,搞定。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include
#include

using namespace std;

#define N 10005
#define L(x) x<<1
#define R(x) x<<1|1
#define M(x,y) (x + y)>>1
#define MOD 1000000007
#define MODD 1000000006
#define inf 0x7fffffff
#define llinf 0x7fffffffffffffff
#define LL long long

LL s[N][5];
LL dp[N][10];
LL bin[10][4] = {
    {0,0,0,0},{0,1,0,0},{0,0,1,0},{0,1,1,0},{0,0,0,1},{0,1,0,1},{0,0,1,1},{0,1,1,1}};
LL b[4] = {0,1,2,4};
LL i_e[10],num;
LL comb[5];

void dfs(LL x,LL y,LL th,LL len)
{
	if(th == 4)
	{
		dp[len][y] = (dp[len][y] + dp[len - 1][x])%MOD;
		return ;
	}
	if(bin[y][th] == 1 && bin[x][th] == 0)
		return ;
	if(bin[y][th] == 1 && bin[x][th] == 1)
		dfs(x,y,th + 1,len);
	if(bin[y][th] == 0 && bin[x][th] == 0)
		dfs(x,y + b[th],th + 1,len);
	if(bin[y][th] == 0 && bin[x][th] == 1)
		dfs(x,y,th + 1,len);
	if(th > 1 && bin[y][th] + bin[y][th - 1] == 0 && bin[x][th] + bin[x][th - 1] == 2)
		dfs(x,y + b[th] + b[th - 1],th + 1,len);
}

LL cntdp(LL n)
{
	memset(dp,0,sizeof(dp));
	dp[0][7] = 1;
	for(LL i = 1;i <= n;i++)
	{
		for(LL j = 0;j < 8;j++)
		{
			if(dp[i - 1][j] == 0)
				continue;
			LL cnt = 0;
			for(LL k = 1;k <= 3;k++)
			{
				if(s[i][k] != 0)
				{
					cnt += b[k];
				}
			}
			dfs(j,cnt,1,i);
		}
	}
	return dp[n][7];
}

void updata(LL n,LL x,LL y)
{
	if(n == 1)
	{
		s[y - 1][x] = s[y - 2][x] = 1;
	}
	else if(n == 2)
	{
		s[y + 2][x] = s[y + 1][x] = 1;
	}
	else if(n == 3)
	{
		s[y][1] = s[y][2] = 1;
	}
	else
	{
		s[y][2] = s[y][3] = 1;
	}
}

void dowmdata(LL n,LL x,LL y)
{
	if(n == 1)
	{
		s[y - 1][x] = s[y - 2][x] = 0;
	}
	else if(n == 2)
	{
		s[y + 2][x] = s[y + 1][x] = 0;
	}
	else if(n == 3)
	{
		s[y][1] = s[y][2] = 0;
	}
	else
	{
		s[y][2] = s[y][3] = 0;
	}
}

LL dfs1(LL n,LL m,LL x,LL y,LL number)
{
	if(m > n)
	{
		for(LL i = 1;i <= n;i++)
		{
			updata(comb[i],x,y);
		}
		LL sum = cntdp(number);
		for(LL i = 1;i <= n;i++)
		{
			dowmdata(comb[i],x,y);
		}
		return sum;
	}
	LL sum = 0;
	for(LL i = 1;i <= num;i++)
	{
		if(comb[m - 1] >= i_e[i] && m != 1)
			continue;
		comb[m] = i_e[i];
		sum = (sum + dfs1(n,m + 1,x,y,number))%MOD;
	}
	return sum;
}

int main()
{
	LL i,j,k,l;
	LL n,x,y;
	while(cin>>n)
	{
		for(i = 1;i <= 3;i++)
		{
			for(j = 1;j <= n;j++)
			{
				char c;
				cin>>c;
				if(c == '.')
					s[j][i] = 0;
				else if(c == 'X')
					s[j][i] = 1;
				else
				{
					y = j;
					x = i;
					s[j][i] = 1;
				}
			}
		}
		num = 0;
		LL ans = 0;
		if(y > 2 && s[y - 2][x] + s[y - 1][x] == 0)
		{
			i_e[++num] = 1;
		}
		if(y < n - 2 && s[y + 2][x] + s[y + 1][x] == 0)
		{
			i_e[++num] = 2;
		}
		if(x == 3 && s[y][1] + s[y][2] == 0)
		{
			i_e[++num] = 3;
		}
		if(x == 1 && s[y][2] + s[y][3] == 0)
		{
			i_e[++num] = 4;
		}
		for(i = 1;i <= num;i++)
		{
			ans = (ans + dfs1(i,1,x,y,n)*(i%2 == 0?-1:1))%MOD;
		}
		ans = (ans + MOD)%MOD;
		cout<


你可能感兴趣的:(ACM)