poj 3254 Corn Fields

http://poj.org/problem?id=3254ios

Corn Fields
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 9118   Accepted: 4843

Descriptionide

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.spa

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.code

Inputblog

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)

Outputthree

Line 1: One integer: the number of ways that FJ can choose the squares modulo 100,000,000.

Sample Inputip

2 3
1 1 1
0 1 0

Sample Outputget

9

Hintstring

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.

Sourceit

 
 
分析;
狀態壓縮dp 。

農夫FJ有一塊n行m列的矩形土地, 有的土地是肥沃的,能夠在這些土地上放牛(用1表示),有的土地不能放牛(用0表示),並且相鄰的能夠放牛的格子不能同時有牛,問FJ一共有多少种放牛的方案(一頭牛都不放也是一種方案)。

分析:以樣例爲例,咱們能夠一行一行的考慮。假如對於每一行,用1表示放牛,0表示不放牛,

第一行的狀態爲:000001010011(捨棄) 100101110(捨棄)111(捨棄)

符合題意的狀態只有5個,因此第一行有5種方案。

第二行的狀態爲:000010

可是第二行中的010和第一行中的010衝突,因此若是第二行狀態爲010時,共有4種方案;狀態爲000時,有5種方案,因此一共有4+5=9種方案。

分析完咱們會發現,對於每一行,均可以一個01串來表示這一行的狀態,而這個狀態能夠用一個十進制整數來代替,也就是說,把這個狀態壓縮成了一個十進制整數,因此稱爲是狀態壓縮。

本題中,dp[i][j] 就表示第i行狀態爲j時的方案數。

狀態壓縮dp中最多見的就是位運算,由於位運算能夠很方便的判斷當前狀態是否合法。

例如本題中判斷第i行是否是有兩塊相鄰的土地同時都有牛,假設當前狀態爲X,那麼只須要判斷X&(X>>1)或者X&(X<<1)的結果是否是0,

若是是0,說明沒有相鄰的,不然就說明有相鄰的。

 
 
 
AC代碼1:
 
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<vector>
 4 using namespace std;
 5 #define mod 100000000
 6 const int N = 1<<12 + 4;
 7 int dp[15][N], Map[15][15];
 8 int n, m;
 9 vector<int> vec[14];
10 int Pow(int a, int x) //2的X次方
11 {
12     int s = 1;
13     for(int i = 1; i <= x; i++)
14         s <<= 1;
15     return s;
16 }
17 int fun(int x) //求第X行的土地狀態,0表示能夠放牛,1表示不能放牛
18 {
19     int s = 0;
20     for(int i = 1; i <= m; i++)
21         s += (!Map[x][i]) * Pow(2, m-i);
22     return s;
23 }
24 int main()
25 {
26     int i, j;
27     while(~scanf("%d%d",&n,&m))
28     {
29         memset(dp, 0, sizeof(dp));
30         memset(vec, 0, sizeof(vec));
31         for(i = 1; i <= n; i++)
32             for(j = 1; j <= m; j++)
33                 scanf("%d",&Map[i][j]);
34         vec[0].push_back(0);
35         int k = 1<<m;
36         for(i = 0; i < k; i++)
37             dp[0][i] = 1;
38         for(i = 1; i <= n; i++)
39         {
40             int tmp = fun(i); //當前行的狀態
41             for(j = 0; j < k; j++)
42             {
43                 if(j & (j>>1)) continue; //j的二進制表示中有兩個相鄰的1
44                 if(j & tmp) continue; //排除在當前行不符合條件的
45                 vec[i].push_back(j);
46             }
47             for(j = 0; j < vec[i].size(); j++) //排除和上一行衝突的
48             {
49                 int u = vec[i][j];
50                 for(int z = 0; z < vec[i-1].size(); z++)
51                 {
52                     int v = vec[i-1][z];
53                     if(u & v) continue;
54                     dp[i][u] = (dp[i][u] + dp[i-1][v]) % mod;
55                 }
56             }
57         }
58         int ans = 0;
59         for(i = 0; i < k; i++)
60             ans = (ans + dp[n][i]) % mod;
61         printf("%d\n",ans);
62     }
63     return 0;
64 }
View Code

 

 

AC代碼2:

 

 1 /*   dp[i][j] 表示第i行狀態爲j時的合法狀態數量 */
 2 
 3 #include <cstdio>
 4 #include <cstring>
 5 const int N = 13;
 6 #define mod 100000000
 7 int dp[N][1<<N];
 8 int beg[N];
 9 
10 bool checkA(int x) {  //判斷本行是否合法
11     return !(x & (x >> 1));
12 }
13 
14 bool checkB(int a, int b) { //判斷和上一行是否衝突
15     return !(a & b);
16 }
17 
18 int main() {
19     int n, m, t;
20     while(~scanf("%d%d", &n, &m)) {
21         memset(dp, 0, sizeof(dp));
22         memset(beg, 0, sizeof(beg));
23         for(int i = 0; i < n; i++) {
24             for(int j = 0; j < m; j++) {
25                 scanf("%d", &t);
26                 if(t) beg[i] = beg[i] | (1 << j);
27             }
28         }
29 
30         for(int i = 0; i < (1<<m); i++) //求出第一行的合法狀態數目
31             if((beg[0]|i) == beg[0] && checkA(i))
32                 dp[0][i] = 1; 
33                 
34         for(int i = 1; i < n; i++) {
35             for(int j = 0; j < (1<<m); j++) {  //枚舉本行狀態
36                 if(((beg[i]|j) == beg[i]) && checkA(j)) {
37                     for(int k = 0; k < (1<<m); k++) { //枚舉上一行的狀態
38                         if(checkB(j, k)) //根據上一行遞推出本行
39                             dp[i][j] = (dp[i][j] + dp[i-1][k]) % mod;
40                     }
41                 }
42             }
43         }
44         
45         int ans = 0;
46         for(int i = 0; i < (1<<m); i++)
47             ans = (ans + dp[n-1][i]) % mod;
48         printf("%d\n", ans);
49     }
50     return 0;
51 }
View Code

 

 

AC代碼3:
 
 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<string.h>
 4 using namespace std;
 5 int n,m;
 6 int a[15][15],dp[15][1<<12],v[1<<12];
 7 const int mod=100000000;
 8 void init()
 9 {
10     int i,l=0;
11     for(i=0;i<(1<<12);i++)
12     {
13         if((i&i<<1)==0)//選出全部不相鄰的狀態
14             v[l++]=i;
15     }
16 }
17 int check(int x,int p)
18 {
19     for(int i=1;i<=m;i++)
20         if((p&1<<(i-1))&&!a[x][i])//不能放的地方放了
21             return 1;
22     return 0;
23 }
24 int main()
25 {
26     int i,j,k;
27     init();
28     while((scanf("%d %d",&n,&m))!=EOF)
29     {
30         for(i=1;i<=n;i++)
31             for(j=1;j<=m;j++)
32                 scanf("%d",&a[i][j]);
33         memset(dp,0,sizeof(dp));
34         for(i=1;i<=n;i++)
35         {
36             for(j=0;v[j]<(1<<m);j++)
37             {
38                 if(check(i,v[j]))
39                     continue;
40                 if(i==1)
41                 {
42                     dp[i][j]=1;
43                     continue;
44                 }
45                 for(k=0;v[k]<(1<<m);k++)
46                 {
47                     if((v[j]&v[k])==0)//與上一行沒有相鄰的
48                         dp[i][j]+=dp[i-1][k];
49                 }
50             }
51         }
52         __int64 ans=0;
53         for(i=0;v[i]<(1<<m);i++)
54             ans=(ans+dp[n][i])%mod;
55         printf("%I64d\n",ans);
56     }
57     return 0;
58 }
相關文章
相關標籤/搜索