POJ 3254 状压DP(基础题)

Corn Fields
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 17749 Accepted: 9342

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表示肥沃的土地,0表示贫瘠的土地,然后要让牛必须站在肥沃的土地上,又不能相邻(可以整个都不站牛)

题解:那就用二进制来表示整个土地的样子,状态转移是dp[i][j] = sum(dp[i-1][k]) 表示第i行的站法是由i-1行所有可能的站法并且两行不能冲突的和

技巧:

1.判断一个数字是否有相邻的两个1 :x&(x<<1)=0,则表示没有,否则有。

2.两行是否有上下相邻的:若x&y=0,则没有,否则有。

3. 判断当前站位是否满足土地条件:若maps[i]=maps[i] | x,则x情况下的站位满足土地条件。

代码:

 1 //#include "bits/stdc++.h"
 2 #include "cstdio"
 3 #include "map"
 4 #include "set"
 5 #include "cmath"
 6 #include "queue"
 7 #include "vector"
 8 #include "string"
 9 #include "cstring"
10 #include "time.h"
11 #include "iostream"
12 #include "stdlib.h"
13 #include "algorithm"
14 #define db double
15 #define ll long long
16 //#define vec vector<ll>
17 #define Mt  vector<vec>
18 #define ci(x) scanf("%d",&x)
19 #define cd(x) scanf("%lf",&x)
20 #define cl(x) scanf("%lld",&x)
21 #define pi(x) printf("%d\n",x)
22 #define pd(x) printf("%f\n",x)
23 #define pl(x) printf("%lld\n",x)
24 #define rep(i, x, y) for(int i=x;i<=y;i++)
25 const int N   = 1e5 + 5;
26 const int mod = 1e9 + 7;
27 const int MOD = mod - 1;
28 const db  eps = 1e-10;
29 const db  PI  = acos(-1.0);
30 using namespace std;
31 int maps[20],ok[N];
32 int f[20][N];
33 int n,m;
34 bool cal1(int i){
35     return i&(i<<1);//检查相邻的1
36 }
37 bool cal2(int x,int y){
38     return maps[x]==(maps[x]|ok[y]);//检查合理的站位与土地条件是否冲突
39 }
40 int main()
41 {
42     while(scanf("%d%d",&n,&m)==2&&n!=0)
43     {
44         memset(maps,0, sizeof(maps));
45         memset(f,0, sizeof(f));
46         for(int i=0;i<n;i++)
47         {
48             for(int j=0;j<m;j++){
49                 int x;
50                 ci(x);
51                 if(x!=0) maps[i]+=(1<<j);//更新第i行的土地
52             }
53         }
54         int cnt=0;
55         for(int i=0;i<(1<<m);i++){
56             if(!cal1(i)) ok[cnt++]=i;//保存合理的站位
57         }
58         for(int i=0;i<cnt;i++){
59             if(cal2(0,i)) f[0][i]=1;//处理出第一行的合理站位
60         }
61         ll ans=0;
62         for(int i=1;i<n;i++){
63             for(int j=0;j<cnt;j++){
64                 if(!cal2(i,j)) continue;//检查对于第i行是否合理
65                 for(int k=0;k<cnt;k++){//检查与i-1行是否冲突
66                     if(!(ok[j]&ok[k])) f[i][j]+=f[i-1][k];
67                 }
68             }
69         }
70         for(int i=0;i<cnt;i++) ans+=f[n-1][i],ans%=100000000;
71         pl(ans);
72     }
73     return 0;
74 }

 

 

转载于:https://www.cnblogs.com/mj-liylho/p/8392278.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值