状压DP入门 POJ - 3254

POJ - 3254 状压DP

Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yummy corn for the cows on a number of squares. Regrettably, some of the squares are infertile and can’t be planted. Canny FJ knows that the cows dislike eating close to each other, so when choosing which squares to plant, he avoids choosing squares that are adjacent; no two chosen squares share an edge. He has not yet made the final choice as to which squares to plant.

Being a very open-minded man, Farmer John wants to consider all possible options for how to choose the squares for planting. He is so open-minded that he considers choosing no squares as a valid option! Please help Farmer John determine the number of ways he can choose the squares to plant.

Input
Line 1: Two space-separated integers: M and N
Lines 2… M+1: Line i+1 describes row i of the pasture with N space-separated integers indicating whether a square is fertile (1 for fertile, 0 for infertile)
Output
Line 1: One integer: the number of ways that FJ can choose the squares modulo 100,000,000.
Sample Input
2 3
1 1 1
0 1 0
Sample Output
9
Hint
Number the squares as follows:
1 2 3
4

There are four ways to plant only on one squares (1, 2, 3, or 4), three ways to plant on two squares (13, 14, or 34), 1 way to plant on three squares (134), and one way to plant on no squares. 4+3+1+1=9.

大致的题意:给出了长和宽固定的农场,0代表不可以放牛,1代表可以放牛,但是还有一个要求就是任意两头牛不可以相邻,这就牵扯到了是在同一行相邻还是同一列相邻的问题

例子中的可以的放牛策略是:
1: 1 2: 2 3: 3 4: 4 5:1 3 6:1 4 7: 3 4
8: 1 3 4 9: 所有地都不放牛

定义状态dp【i】【j】,第 i 行状态为 j 的时候放牛的种数。j 的话我们转化成二进制,从低位到高位依次 1 表示放

牛0表示没有放牛,就可以表示一行所有的情况。

那么转移方程 dp【i】【j】=sum(dp【i-1】【k】)

状态压缩dp关键是处理好位运算。

这个题目用到了 & 这个运算符。

用 x & (x<<1)来判断一个数相邻两位是不是同时为1,假如同时为 1 则返回一个值,否则返回 0 ,这样就能优化掉一些状态

用 x & y 的布尔值来判断相同为是不是同时为1。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 13;
const int M = 1<<N;
const int mod = 100000000;
int st[M],map[M];  ///分别存每一行的状态和给出地的状态
int dp[N][M];  //表示在第i行状态为j时候可以放牛的种数
bool judge1(int x) { //判断二进制有没有相邻的1
	return (x&(x<<1));
}
bool judge2(int i,int x) {
	return (map[i]&st[x]);
}
int main() {
	int n,m,x;
	while(~scanf("%d%d",&n,&m)) {
		memset(st,0,sizeof(st));
		memset(map,0,sizeof(map));
		memset(dp,0,sizeof(dp));
		for(int i=1; i<=n; i++) {
			for(int j=1; j<=m; j++) {
				cin>>x;
				if(x==0)
					map[i]+=(1<<(j-1));
			}

		}
		int k=0;
		for(int i=0; i<(1<<m); i++) {
			if(!judge1(i))
				st[k++]=i;
		}
		for(int i=0; i<k; i++) {
			if(!judge2(1,i))
				dp[1][i]=1;
		}
		for(int i=2; i<=n; i++) {
			for(int j=0; j<k; j++) {
				if(judge2(i,j))  //判断第i行 假如按状态j放牛的话行不行。
					continue;
				for(int f=0; f<k; f++) {
					if(judge2(i-1,f))   //剪枝 判断上一行与其状态是否满足
						continue;
					if(!(st[j]&st[f]))
						dp[i][j]+=dp[i-1][f];
				}
			}
		}
		int ans=0;
		for(int i=0; i<k; i++) {
			ans+=dp[n][i];
			ans%=mod;
		}
		cout<<ans<<endl;
	}
	return 0;
}

通过judge2函数的判断就可以知道当前的这种放牛的方式在当前给定行的土地上是否可以实行,map[i]+=(1<<(j-

1))通过这一步的操作就把当前行的牧场按位取反得到的结果了,例如1 0 1的牧场,那么按照这个操作完之后的

map实际的值是010的十进制值也就是2存储在map数组的相应位置下,然后再通过judge2函数的再判断,看与该

行所存下的数字进行“与”运算是否为0,即可判断,为0那么就可以在该行进行如下的布置,若不懂可以自己找几个

二进制“与”操作的例子来自己进行更深一步的思考。最后就是还要在相邻两行之间也进行judge2函数的类似操作,

因为两行之间相同列的位置是不可以进行放牛操作的,应该在与当前行进行完judge2的判断之后再进行与上一行

的判断,只有两个条件都满足那么才可以安排当前行的放牛策略。然后在这个操作的时候进行相应的动态规划的

过程用dp数组来记录当前行的当前状态下的可以满足题意的放牛策略,最后for循环遍历相加最后一行的各种状态

下对应的值就是最后该牧场下的最大的可能的放牛的策略的数量。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

协奏曲❤

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值