Frist Blood poj3254(状态压缩+DP)

本文介绍了一道关于在限定条件下选择牧场格子进行种植的问题,并通过状态压缩动态规划的方法给出了详细的解决方案。该方法利用二进制表示每行种植状态,通过递推找出所有可能的种植方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Corn Fields
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 7049 Accepted: 3749

Description

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.

题意:有个人有一块N*M的牧场。每个格子可以放一个牛,当然是有条件的。相邻的格子不能同时放牛,还有些格子土地是不允许放牛的,用0表示can't。1表示ok。问有多少种放牛的方法。

题解:很好的状态压缩DP题目。把每行放牛的状态用数字二进制表示出来,总共状态是2^M次方。1表示放牛0表示不放。所以相邻的1的状态需要排除。也就是X&(1<<X)为0就可以加入状态。然后逐行递推。下一行的状态又不能跟第一行有相邻的1,并且有些格子是不能放牛的,我们可以用一个很好的技巧存储每一行的陷阱(也就是不能放牛的地方).cas[i]表示第i行不能放牛的地方,1表示不能放牛。需要了解每个数字的二进制状态就很好理解了。最后只需要统计所有状态最后一行的和即是总数解。

#include <cstring>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;    //1表示有牛,0表示没牛 
const int mod=100000000;
int n,m,l;
int sta[1<<15];
int dp[1<<15][15];      //dp[i][j]第i个状态第j行有多少种 
int cas[15];
int ok(int x)
{
	if (x & (x<<1)) return 0;  //判断当前x状态是否满足相邻是否有为1。 
	return 1;
}
void cls()
{
	l=0;
	for (int i=0;i<1<<m;i++)
		if (ok(i)) sta[++l]=i;
}
int pd(int x,int k)
{
	if(x & cas[k]) return 0;
	return 1;
}
int main()
{
	int i,j,k;
	scanf("%d%d",&n,&m);
	cls();
	for (i=1;i<=n;i++)
	{
		cas[i]=0;     //表示第i行第哪些位置不能放(牛) 
		for (j=1;j<=m;j++)
		{
			scanf("%d",&k);
			if (k==0) cas[i]+=1<<(m-j);
		}
	}
	memset(dp,0,sizeof(dp));
	for (i=1;i<=l;i++)         //l表示可行的状态总数(也就是说没有相邻的牛)
		if (pd(sta[i],1))
			dp[i][1]=1;
	for (i=2;i<=n;i++)
		for (j=1;j<=l;j++)    //表示第i行第j个状态 
		{
			if (!pd(sta[j],i)) continue;
			for (k=1;k<=l;k++)  //表示第i-1行第k个状态 
			{
				if (!pd(sta[k],i-1)) continue;
				if (sta[j] & sta[k]) continue;
				dp[j][i]=(dp[j][i]+dp[k][i-1]) % mod;
			}
		}
	int ans=0;
	for (i=1;i<=l;i++)
		ans=(ans+dp[i][n]) % mod;
	cout<<ans<<endl;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值