[学习][poj3254]状压DP Corn Field

来源:互联网 发布:中国2016 m2最新数据 编辑:程序博客网 时间:2024/06/08 00:33

状压DP的简介
在我看来,状压DP就是在状态比较多的时候将dp数组存状态的维度减少,用某进制的每一位的数表示状态。语文不好,还是看看别人怎么说吧:
我们知道,用DP解决一个问题的时候很重要的一环就是状态的表示,一般来说,一个数组即可保存状态。但是有这样的一些题目,它们具有DP问题的特性,但是状态中所包含的信息过多,如果要用数组来保存状态的话需要四维以上的数组。于是,我们就需要通过状态压缩来保存状态,而使用状态压缩来保存状态的DP就叫做状态压缩DP。——Blithe的博客

状压DP的实现
emmmm,想保持之前讲解的格式好难啊,我不知道怎么讲啊
举个例子吧,第一行5个格子,要记录每个格子都放了东西的状态,如果是普通dp就需要dp[第一行][第一格放了][第二格放了][第三格放了][第四格放了][第五格放了],而如果利用状压DP,就可以这样:dp[第一行][(11111)2](二进制,1表示放了,0表示没放),一下子就少了很多维是不是。
啊,水完了......

例题
题目背景
poj3254

题目描述
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.

题目大意
农夫有一块地,被划分为m行n列大小相等的格子,其中一些格子是可以放牧的(用1标记),农夫可以在这些格子里放牛,其他格子则不能放牛(用0标记),并且要求不可以使相邻格子都有牛。现在输入数据给出这块地的大小及可否放牧的情况,求该农夫有多少种放牧方案可以选择(注意:任何格子都不放也是一种选择,不要忘记考虑!
——来自我学习状压dp的地方:外出散步的博客

输入格式
第1行:两个用空格隔开的整数M和N
第2行到M+1行:每行N个数字表示该块地能否用来放牧(1可以、0不行)

输出格式
1行:一个整数表示FJ可以选择的方式种数,对100000000取模。

样例数据
输入

2 3
1 1 1
0 1 0

输出
9

备注
【样例说明】
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.

分析:状压DP模板,如果不会也请参阅外出散步的博客,注释简直良心!

代码

#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<string>#include<ctime>#include<cmath>#include<algorithm>#include<cctype>#include<iomanip>#include<queue>#include<set>using namespace std;int getint(){    int sum=0,f=1;    char ch;    for(ch=getchar();(ch<'0'||ch>'9')&&ch!='-';ch=getchar());    if(ch=='-')    {        f=-1;        ch=getchar();    }    for(;ch>='0'&&ch<='9';ch=getchar())        sum=(sum<<3)+(sum<<1)+ch-48;    return sum*f;}const int mo=1e8;int n,m,top;int sta[600],num[110],r[20];int dp[20][600];bool check(int x){    if(x&(x<<1))        return false;    return true;}void pre(){    top=0;    int tot=(1<<m);    for(int i=0;i<tot;++i)        if(check(i)) sta[++top]=i;}bool check2(int x,int k){    if(x&r[k])        return false;    return true;}int main(){    freopen("corn.in","r",stdin);    freopen("corn.out","w",stdout);    while(scanf("%d%d",&n,&m)!=EOF)    {        pre();        memset(dp,0,sizeof(dp));        int x;        for(int i=1;i<=n;++i)        {            r[i]=0;            for(int j=1;j<=m;++j)            {                x=getint();                if(x==0) r[i]+=(1<<(m-j));            }        }        for(int i=1;i<=top;++i)            if(check2(sta[i],1))                dp[1][i]=1;        for(int i=2;i<=n;++i)            for(int k=1;k<=top;++k)            {                if(!check2(sta[k],i)) continue;                for(int j=1;j<=top;++j)                {                    if(!check2(sta[j],i-1)) continue;                    if(sta[k]&sta[j]) continue;                    dp[i][k]=(dp[i][k]+dp[i-1][j])%mo;                }             }        int ans=0;        for(int i=1;i<=top;++i)            ans=(ans+dp[n][i])%mo;        printf("%d\n",ans);    }    return 0;}

本题结。

原创粉丝点击