POJ-3254-Corn Fields(状压DP)

Corn Fields

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 22668 Accepted: 11855

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.

题意:农夫有一块地,被划分为m行n列大小相等的格子,其中一些格子是可以放牧的(用1标记),农夫可以在这些格子里放牛,其他格子则不能放牛(用0标记),并且要求不可以使相邻格子都有牛。现在输入数据给出这块地的大小及可否放牧的情况,求该农夫有多少种放牧方案可以选择(注意:任何格子都不放也是一种选择,不要忘记考虑!)
题意分析:在博客:传送门写的挺好的,可以去看,我就在那里学的,我在他的基本上对每句话进行了更深的解释,每句话后面都有注释,可以看完前面的题解后,结合两篇代码的解释看

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#define MOD 100000000
using namespace std;
int a[30],num[1005],f[30][1005];
int n,m,total,k=0,x,ans;
bool ok(int x)//找每一行可以存在的状态
{
  if(x&(x<<1))return false;
  //先把x向左移一位,那么就得到旁边的格子的状态,如果两个格子都是1,那么1&1=1,那么就不行
  //它并不是最后一位推了,而是所有的前面的整体往前移动,所以不止最后两位比较,每相邻的那两位都比较
  else return true;
}
void init(int m)
{
  total=(1<<m);//2^m种状态,每个格子有两种状态,m个格子
  for(int i=0;i<total;i++)//0~2^m-1个数,每个数代表一种状态
  {
    if(ok(i))
    {
      k++;
      num[k]=i;//一行中可以满足条件的状态我就记录下来
    }
  }
}
bool check(int u,int v)
{
  if(u&a[v])return false;
  //u代表理想中的该行的状态,a代表有数据限制下的状态,数据限制下的不能种(a[v])的地方为1,那么只有我理想状态下为0(不种),我才能满足条件
  else return true;
}
int main()
{
  while(~scanf("%d%d",&n,&m))
  {
    ans=0;
    k=0;
    total=0;
    memset(a,0,sizeof(a));
    memset(f,0,sizeof(f));
    memset(num,0,sizeof(num));
    init(m);
    for(int i=1;i<=n;i++)
    {

      for(int j=1;j<=m;j++)
      {
        scanf("%d",&x);
        if(x==0)
          {
            a[i]+=(1<<(m-j));
            // cout<<a[i]<<" ";
          }
        //如果该格为0,那么我就把他标记为1
        //因为与的条件是有0,则0,那么我在与num比较时,num表示放的位置为1,如果我标记为1,表示这块地不能放,
        //只有我那一个选择不种草,我才会满足为0的条件,而当我保持0的时候,我的那里判断不管我种不种草我最后都是0了,所以不能标记为0
        //肯定又有人会想改为或条件,那么之前的num中的1又要变为0了,麻烦,而我这里修改只要修改一部分
        //a数组用来储存在该行现在已知条件下的可以存在的状态
      }

    }
    for(int i=1;i<=k;i++)//预处理第一行
    {
      if(check(num[i],1))
      {
        f[1][i]=1;
        //在第一行的k种情况中,在数据所给的第一行限制下任然可以存活情况就标记为1
      }
    }
    for(int i=2;i<=n;i++)//枚举从第2层~第n层
    {
      for(int j=1;j<=k;j++)//枚举该层上的状态
      {
        if(!check(num[j],i))continue;//如果该层的状态,与该层当前的数据规定情况不同,那么就continue
        for(int l=1;l<=k;l++)//枚举上一层的状态
        {
          if(!check(num[l],i-1))continue;//如果上一层的状态与上一层的数据规定状态不同,那么也继续continue
          if(num[j]&num[l])continue;//如果该层与上一层只满足自身和数据情况的状态下,上下两层又有相邻的,那么又继续continue
          f[i][j]=(f[i-1][l]+f[i][j])%MOD;
        }
      }
    }
    for(int i=1;i<=k;i++)
      ans=(ans+f[n][i])%MOD;//最后一层有k种情况,每种情况对应的方案数
    printf("%d\n",ans);
  }
  return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值