POJ 炮兵阵地 (状压dp)

link

中文题意哦~

思路:跟矩阵相关的转压dp,相当于暴力枚举,但是不是一个一个的枚举,而是一个状态一个状态的枚举,也可以理解成一个集合一个集合的枚举,这题限制条件有点多,我们设dp[ i ][ j ][ k ]表示枚举到第i行,前一行的状态是j,前前行的状态是k的最多炮兵数,如果直接枚举时间复杂度是不ok的,我们可以预处理出行的合法的所有状态,然后取枚举。

代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;
char s[12];
int sta[1<<12],dp[2][1<<11][1<<11];
int t[102];
int n,m;

int count(int x)
{
	int tot = 0;
	for(int i=0;i<m;i++)
	if((1<<i&x))++tot;
	return tot;
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++)
	{
		scanf("%s",s);
		for(int j=0;j<m;j++)
		if(s[j] == 'H')t[i] += (1<<j);
	}
	
	int cnt = 0;
	for(int i=0;i<1<<m;i++)
	if((i<<1&i)==0 && (i<<2&i)==0 && (i>>1&i)==0 && (i>>2&i)==0)sta[++cnt] = i;
	int ans = 0,cur = 0;
	for(int i=1;i<=cnt;i++)
	    if((t[0]&sta[i])==0)
		{
			dp[0][sta[i]][0] = count(sta[i]);
			ans = max(ans,dp[0][sta[i]][0]);
		}
	
	for(int i=1;i<n;i++)
	{
		cur^=1;
		for(int s=1;s<=cnt;s++)
		{
			if(sta[s]&t[i])continue;
			for(int s0=1;s0<=cnt;s0++)
			{
				if(sta[s]&sta[s0])continue;
				for(int k=1;k<=cnt;k++)
				{
					if((sta[s]&sta[k]) || (sta[s0]&sta[k]))continue;
					dp[cur][sta[s]][sta[s0]] = max(dp[cur][sta[s]][sta[s0]] , dp[cur^1][sta[s0]][sta[k]] + count(sta[s]));
					ans = max(ans,dp[cur][sta[s]][sta[s0]]);
				}
			}
		}
	}
	cout<<ans<<'\n';
	
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值