补题日记-POJ 3254 Corn Fields

Corn Fields
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 23644   Accepted: 12375

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

 
解题思路:
这道题是按行来考虑,用二进制来表示每一行的状态,然后来进行dp
据说是状压dp的入门题,理解是挺好理阶的,但是写起来还是有点点繁琐的,但是因为网上资源比较多
所以也还好
 
代码如下:
 1 #include<iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 
 6 const int mod = 100000000;
 7 int n,m,tot;
 8 
 9 int dp[20][600];
10 int state[1<<15];
11 int real[20];
12 
13 bool check(int x)
14 {
15     if(x&x<<1) return false;
16     return true;
17 }
18 
19 void init()
20 {
21     tot=0;
22     int total=1<<n;
23     for(int i=0;i<total;++i)
24     {
25         if(check(i))
26             state[++tot]=i;
27     }
28 }
29 
30 bool fit(int x,int y)
31 {
32     if(x&real[y]) return false;
33     else return true;
34 }
35 
36 int main()
37 {
38 
39     while(cin>>m>>n)
40     {
41         init();
42         memset(dp,0,sizeof(dp));
43         for(int i=1;i<=m;++i)
44         {
45             real[i]=0;
46             for(int j=1;j<=n;++j)
47             {
48                 int a;
49                 cin>>a;
50                 if(a==0)
51                     real[i]+=(1<<(n-j));
52             }
53         }
54 
55         for(int i=1;i<=tot;++i)
56         {
57             if(fit(state[i],1))
58                 dp[1][i]=1;
59         }
60 
61         //状态转移部分
62         for(int i=2;i<=m;++i)
63         {
64             for(int j=1;j<=tot;++j)
65             {
66                 if(!fit(state[j],i))
67                     continue;
68                 for(int k=1;k<=tot;++k)
69                 {
70                     if(!fit(state[k],i-1)) continue;
71                     if(state[j]&state[k]) continue;
72                     dp[i][j]=(dp[i][j]+dp[i-1][k])%mod;
73                 }
74             }
75         }
76 
77         int ans=0;
78         for(int i=1;i<=tot;++i)
79         {
80             ans=(ans+dp[m][i])%mod;
81         }
82         cout<<ans<<endl;
83     }
84 
85    return 0;
86 }

猜你喜欢

转载自www.cnblogs.com/bethebestone/p/12118279.html