poj3254 Corn Fields 利用状态压缩求方案数;

Corn Fields
2015-11-25 13:42:33
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 10658   Accepted: 5602

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

 
poj3254 Corn Fields 利用状态压缩d求方案数;
 
 
/**
题意:就是你给一个n行m列的矩阵,矩阵里的元素由0和1组成,1代表肥沃的土地可以种植作物,
0则不可以种植作物,并且相邻的土地不能同时种植作物,问你有多少种种植方案。
定义dp[i][j]表示第i行在j状态时,前i行种植的方法数;
状态转移方程:
dp[i][j] += dp[i-1][k]; //正确性,dp[0][0] = 1,边界;每一个dp[n][j]都是一个根;向下的分支总数就是方案总数;
他们的分支已经包含了到当前位置的所有情况;
答案:
sum += dp[m][j];
*/
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<string>
 5 #include<set>
 6 #include<algorithm>
 7 #include<vector>
 8 #include<map>
 9 #include<queue>
10 #include<cmath>
11 #include<cctype>
12 #include<stack>
13 #include<iomanip>
14 using namespace std;
15 typedef pair<int,int> pr;
16 typedef long long ll;
17 const int maxn = 1005;
18 const ll mod = 1e8;
19 const ll inf = 0x3f3f3f3f;
20 ll dp[13][1<<12];
21 ll mp[13], valid[1<<12], vz;
22 bool ok(ll x,ll i)
23 {
24     if((x&mp[i])!=x) return false;// 在x二进制表示,不是mp[i]的子集,则不行;
25     return true;
26 }
27 void getvalid(ll m)//预处理有效的,把一行中存在相邻的状态清除;
28 {
29     vz = 0;
30     ll t = (1<<m);
31     for(ll i = 0; i < t; i++){
32         if(((i<<1)&i)==0){
33             valid[vz++] = i;
34         }
35     }
36 }
37 int main()
38 {
39     ll n, m;
40     while(scanf("%lld%lld",&n,&m)==2)
41     {
42         ll e;
43         //从(1,1)开始好;
44         for(ll i = 1; i <= n; i++){
45             mp[i] = 0;
46             for(ll j = 1; j <= m; j++) {
47                 scanf("%lld",&e);
48                 mp[i] = (mp[i]<<1)+e;
49             }
50         }
51         getvalid(m);
52         memset(dp,0,sizeof(dp));
53         dp[0][0] = 1;//不种植一棵时,方案数为1;
54         for(ll i = 1; i <= n; i++){
55             for(ll j = 0; j < vz; j++){
56                 if(!ok(valid[j],i)) continue;
57                 for(ll k = 0; k < vz; k++){
58                     if(valid[j]&valid[k]) continue;//上下两行不可以有相邻的;
59                     dp[i][valid[j]] = (dp[i][valid[j]]+dp[i-1][valid[k]])%mod;
60                 }
61             }
62         }
63         ll sum = 0;
64         for(ll j = 0; j < vz; j++){
65             sum = (sum+dp[n][valid[j]])%mod;
66         }
67         printf("%lld
",sum);
68     }
69     return 0;
70 }
View Code
 

原文地址:https://www.cnblogs.com/xiaochaoqun/p/4994394.html