poj Corn Fields 状态压缩dp。

Corn Fields
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 5763   Accepted: 3052

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

 

题意:在n*m的矩形里放东西,要求相邻的不能同时放。问有几种方式?

思路:用状态压缩,典型例题。

下面的书写,时间复杂度更高。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 using namespace std;
 6 int INF = 100000000;
 7 
 8 int n,m;
 9 int f[14];
10 int dp[14][1<<14];
11 
12 bool panduan(int a,int b)
13 {
14     int i;
15     if( (f[a]&b) != b)//这个b不存在。
16         return false;
17     int x=3;
18     for(i=1; i<m; i++)
19     {
20         if( (b&x) ==x)//相邻的存在,矛盾了。
21             return false;
22         x=x<<1;
23     }
24     return true;
25 }
26 int main()
27 {
28     int i,j,x,k,s;
29     while(scanf("%d%d",&n,&m)>0)
30     {
31         for(i=1; i<=n; i++)
32         {
33             f[i]=0;
34             for(j=1; j<=m; j++)
35             {
36                 scanf("%d",&x);
37                 f[i]=(f[i]<<1)+x;
38             }
39         }
40         k=1<<m;
41         memset(dp,0,sizeof(dp));
42         dp[0][0]=1;
43         for(i=1; i<=n; i++) //枚举每一行
44         {
45             for(j=0; j<k; j++)//该行的每一个状态。
46             {
47                 if(panduan(i,j))//状态是否合法!!!
48                 {
49                     for(s=0; s<k; s++)//枚举上一行的状态。
50                     {
51                         if( (j&s)>0 )continue;//是否合法。
52                         dp[i][j]=dp[i][j]+dp[i-1][s];
53                         if(dp[i][j]>=INF)
54                             dp[i][j]-=INF;
55                     }
56                 }
57             }
58         }
59         int num=0;
60         for(i=0; i<k; i++)
61             num=(num+dp[n][i])%INF;
62         printf("%d\n",num);
63     }
64     return 0;
65 }

 可以优化,先预处理一下。

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 
 5 int state[380],len;
 6 int a[13];
 7 int dp[13][380];
 8 void prepare()//预处理
 9 {
10     int i,k=1<<12;
11     len=0;
12     for(i=0;i<k;i++)
13     {
14         if( (i&(i<<1)) || (i&(i>>1)) );
15         else state[len++]=i;
16     }
17 }
18 void solve(int n,int m)
19 {
20     int i,j,s;
21     memset(dp,0,sizeof(dp));
22     dp[0][0]=1;
23     for(i=1;i<=n;i++)
24     {
25         for(j=0;j<len;j++)
26         {
27             if( (a[i]&state[j])==state[j] )
28             for(s=0;s<len;s++)
29             {
30                 if( (state[j]&state[s])>0 );
31                 else
32                 {
33                     dp[i][j]=(dp[i][j]+dp[i-1][s])%100000000;
34                 }
35             }
36         }
37     }
38     for(j=0,i=0;i<len;i++)
39         if((state[i]&a[n])==state[i])
40             j=(j+dp[n][i])%100000000;
41     printf("%d\n",j);
42 
43 }
44 int main()
45 {
46     int n,m;
47     int i,j,x;
48     prepare();
49     while(scanf("%d%d",&n,&m)>0)
50     {
51         memset(a,0,sizeof(a));
52         for(i=1;i<=n;i++)
53         {
54             for(j=1;j<=m;j++)
55             {
56                 scanf("%d",&x);
57                 a[i]=(a[i]<<1)+x;
58             }
59         }//
60         solve(n,m);
61     }
62     return 0;
63 }

 

posted @ 2013-11-12 17:12  芷水  阅读(223)  评论(0编辑  收藏  举报