【SSL1383】车II

96 篇文章 0 订阅
10 篇文章 0 订阅

Description

有一个nm的棋盘(n、m≤80,nm≤80)要在棋盘上放k(k≤20)个棋子,使得任意两个棋子不相邻。求合法的方案总数。

Input

n,m,k

Output

方案总数

Sample Input

3 3 2

Sample Output

24

思路:

状压DP
我们先枚举出所有的状态,用 d f s dfs dfs来枚举,即:

void VL_dfs(int ans, int pos, int flag)
{
	if(pos>n)
	{
		s[++num]=ans;
		c[num]=flag;
		return;
	}
	VL_dfs(ans, pos+1, flag);
	VL_dfs(ans+(1<<pos-1), pos+2, flag+1);
}

接下来我们就开始DP
我们用两个变量来枚举状态,分别为当前状态和转移状态,要想它们之间能够互相转移,就必须有一个为0,然后在枚举放的棋子个数

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<cstring>
using namespace std;

int n, m, k, num;
int s[10010], c[10010], f[82][1<<9][21];

void VL_dfs(int ans, int pos, int flag)
{
	if(pos>n)
	{
		s[++num]=ans;
		c[num]=flag;
		return;
	}
	VL_dfs(ans, pos+1, flag);
	VL_dfs(ans+(1<<pos-1), pos+2, flag+1);
}
int main(){
	scanf("%d%d%d", &n, &m, &k);
	if(n>m)swap(n, m);
	VL_dfs(0 ,1, 0);
	for(int i=1; i<=num; i++)
		f[1][s[i]][c[i]]=1;
	for(int i=2; i<=m; i++)
		for(int j=1; j<=num; j++)
			for(int r=1; r<=num; r++)
				if(!(s[j]&s[r]))
				{
					for(int l=0; l<=k; l++)
						if(l>=c[j])
							f[i][s[j]][l]+=f[i-1][s[r]][l-c[j]];
				}
	long long ans=0;
	for(int i=1; i<=num; i++)
		ans+=f[m][s[i]][k];
	printf("%lld", ans);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值