poj 3254 Corn Fields(状态压缩dp)
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.
Source
USACO 2006 November Gold
题意:给出一个n行m列的草地,1表示肥沃,0表示贫瘠,现在要把一些牛放在肥沃的草地上,但是要求所有牛不能相邻,问你有多少种放法。
分析:假如我们知道第 i-1 行的所有的可以放的情况,那么对于第 i 行的可以放的一种情况,我们只要判断它和 i - 1 行的所有情况的能不能满足题目的所有牛不相邻,如果有种中满足,那么对于 i 行的这一中情况有 x 中放法。
前面分析可知满足子状态,我们我们确定可以用dp来解决。
但是我们又发现,状态是一种放法,不是我们平常dp的简单的状态,所以要用状态压缩!
但是什么是状态压缩呢?
比如前面的情况,一种放法是最多由12个 0 或者 1 组成的,那么我们很容易想到用二进制,用二进制的一个数来表示一种放法。
定义状态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。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 #define N 16 6 #define M 1<<N 7 #define MOD 100000000 8 int mp[M];//记录每一行中,不能种植的就将状态记为1 9 int state[M];//记录1<<m中不与别的数相邻的所有状态 10 int dp[N][M]; 11 bool judge(int x){ 12 return x&(x<<1); 13 } 14 bool judge2(int i,int j){ 15 return mp[i]&state[j]; 16 } 17 int main() 18 { 19 int n,m; 20 while(scanf("%d%d",&n,&m)==2){ 21 22 memset(mp,0,sizeof(mp)); 23 memset(state,0,sizeof(state)); 24 memset(dp,0,sizeof(dp)); 25 26 for(int i=1;i<=n;i++){ 27 for(int j=1;j<=m;j++){ 28 int x; 29 scanf("%d",&x); 30 if(x==0){ 31 mp[i]+=(1<<(j-1)); 32 } 33 } 34 } 35 36 int k=0; 37 for(int i=0;i<(1<<m);i++){ 38 if(judge(i)==0){ 39 state[k++]=i; 40 } 41 } 42 43 for(int i=0;i<k;i++){ 44 if(judge2(1,i)==0){ 45 dp[1][i]=1; 46 } 47 } 48 49 for(int i=2;i<=n;i++){ 50 for(int j=0;j<k;j++){ 51 if(judge2(i,j)) continue; 52 for(int l=0;l<k;l++){ 53 if(judge2(i-1,l)) continue; 54 if((state[j]&state[l])==0){ 55 dp[i][j]+=dp[i-1][l]; 56 } 57 } 58 } 59 } 60 int ans=0; 61 for(int i=0;i<k;i++){ 62 ans=ans+dp[n][i]; 63 ans%=MOD; 64 } 65 printf("%d\n",ans); 66 } 67 return 0; 68 }