首页 > 代码库 > poj 3254 状压dp

poj 3254 状压dp

E - Corn Fields
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status Practice POJ 3254

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 31 1 10 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.
 
转自:http://blog.csdn.net/lenleaves/article/details/7974849
第一道状态压缩DP,虽然不难,但是。。。
递推方程 dp[s][i] =Σ dp[s‘][i-1]   dp[s][i]表示第i行状态为s时一共有多少种方案,从第一行开始向下递推。
这里要注意 可以用一个数组先把每一行中符合要求的情况全部记录,这样可以有效减少状态,和循环。另外题目要求我们取模,这里要注意。
 
 

 

  1 #include<iostream>  2 #include<cstring>  3 #include<cstdlib>  4 #include<cstdio>  5 #include<algorithm>  6 #include<cmath>  7 #include<queue>  8 #include<map>  9  10 #define N 15 11 #define M 15 12 #define mod 100000000 13 #define mod2 100000000 14 #define ll long long 15 #define maxi(a,b) (a)>(b)? (a) : (b) 16 #define mini(a,b) (a)<(b)? (a) : (b) 17  18 using namespace std; 19  20 int n,m; 21 int a[N]; 22 int dp[N][ (1<<12)+10]; 23 int ans; 24  25 void ini() 26 { 27     int i,j; 28     int re; 29     ans=0; 30     memset(dp,0,sizeof(dp)); 31     memset(a,0,sizeof(a)); 32     for(i=1;i<=n;i++){ 33         for(j=1;j<=m;j++){ 34             scanf("%d",&re); 35             a[i]=(a[i]<<1) + re; //把二进制压缩成一个十进制数字 36         } 37     } 38  39     //for(i=1;i<=n;i++){ 40     //    printf(" %d\n",a[i]); 41     //} 42 } 43  44 int ok(int i,int j) 45 { 46     if( (a[i] & j) !=j) return 0;  //判断下那一行种是否能够找出flag这个状态 47     while(j) 48     { 49         if(j%2==1 && (j/2)%2==1 ) return 0; 50         j/=2; 51     } 52     return 1; 53 } 54  55 void solve() 56 { 57     int i,j; 58     i=1; 59     for(j=0;j<=a[i];j++){ 60         if(ok(i,j)==0) continue; 61         dp[i][j]++; 62     } 63     for(i=2;i<=n;i++){ 64         for(j=0;j<=a[i];j++){ 65             if(ok(i,j)==0) continue; 66             for(int k=0;k<=a[i-1];k++){ 67                 if(ok(i-1,k)==0) continue; 68                 if( (j&k)!=0) continue; 69                 dp[i][j]+=dp[i-1][k]; 70                 dp[i][j]%=mod; 71             } 72         } 73     } 74  75     //for(i=1;i<=n;i++){ 76     //    for(j=0;j<=a[i];j++){ 77     //        printf(" %d %d dp=%d\n",i,j,dp[i][j]); 78     //    } 79    // } 80     for(j=0;j<=a[n];j++){ 81         ans+=dp[n][j]; 82         ans%=mod; 83         //printf(" %d %d %d\n",j,dp[n][j],ans); 84     } 85 } 86  87 int main() 88 { 89     //freopen("data.in","r",stdin); 90    // scanf("%d",&T); 91    // for(int cnt=1;cnt<=T;cnt++) 92     //while(T--) 93     while(scanf("%d%d",&n,&m)!=EOF) 94     { 95         ini(); 96         solve(); 97         printf("%d\n",ans); 98     } 99 100     return 0;101 }
View Code

 

poj 3254 状压dp