码迷,mamicode.com
首页 > 其他好文 > 详细

洛谷P1879 [USACO06NOV]玉米田Corn Fields【状压DP】题解+AC代码

时间:2018-02-25 20:39:18      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:main   建议   sigma   长方形   c代码   sid   get   vector   string   

题目描述

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.

农场主John新买了一块长方形的新牧场,这块牧场被划分成M行N列(1 ≤ M ≤ 12; 1 ≤ N ≤ 12),每一格都是一块正方形的土地。John打算在牧场上的某几格里种上美味的草,供他的奶牛们享用。

遗憾的是,有些土地相当贫瘠,不能用来种草。并且,奶牛们喜欢独占一块草地的感觉,于是John不会选择两块相邻的土地,也就是说,没有哪两块草地有公共边。

John想知道,如果不考虑草地的总块数,那么,一共有多少种种植方案可供他选择?(当然,把新牧场完全荒废也是一种方案)

输入格式:

第一行:两个整数M和N,用空格隔开。

第2到第M+1行:每行包含N个用空格隔开的整数,描述了每块土地的状态。第i+1行描述了第i行的土地,所有整数均为0或1,是1的话,表示这块土地足够肥沃,0则表示这块土地不适合种草。

输出格式:

一个整数,即牧场分配总方案数除以100,000,000的余数。

输入样例

2 3
1 1 1
0 1 0

输出样例

9


题目分析

这里我们同样以一个二进制数表示每个状态
1表示种了草,0表示没有
dp[i][j]表示只考虑前i行
第i行状态为j时可行的总方案数

有dp数组定义得出递推式
if(j合法 && k合法 && j,k不冲突)
dp[i][j]+=dp[i-1][k];

则最后ans等于sigma (dp[n][j]) 其中 0 <= j <= (1<< n)-1

关于状态x是否合法的判断

if( (( x&(x<<1) )==0)  && (( x&(x>>1) )==0) ) return true;
else return false;

即将x分别左移和右移
若x二进制中某相邻两位皆为1
则左/右移后两位重合,&运算必定返回1,表示不合法

(这里括号比较多,如果不是特别确定位运算优先级,建议将括号都打上,特别注意==比位运算优先级高)

如果实在觉得乱,可以这样写

bool check(int x)
{
    if( x & (x<<1) ) return false;
    if( x & (x>>1) ) return false;
    return true;
}

接下来还要判断状态x是否与土地情况冲突
我们在读入时记录f[i]表示第i行的土地情况
注意我们用1表示不能种,0表示能种
这样我们判断状态x是否合法可以这样

if((j&f[i])==0 )return true;
else return false;

因为我们只关心不能种草的位是否合法
所以这么做能种草的地方一定返回0
而如果状态x在不能种草的地方种了草(该位为1)
则位运算必定返回1

其他的就是基本dp操作啦
********************************

#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;

int read()
{
    int f=1,x=0;
    char ss=getchar();
    while(ss<‘0‘||ss>‘9‘){if(ss==‘-‘)f=-1;ss=getchar();}
    while(ss>=‘0‘&&ss<=‘9‘){x=x*10+ss-‘0‘;ss=getchar();}
    return f*x;
}

const int mod=1e8;
int m,n;
int f[110];
int dp[50][10010];
int ans;

bool check(int x)
{
    if( (( x&(x<<1) )==0)  && (( x&(x>>1) )==0) ) return true;
    else return false;
}

int main()
{
    m=read();n=read();
    
    for(int i=1;i<=m;i++)
    for(int j=1;j<=n;j++)
    {
        int k=read();
        f[i]=(f[i]<<1)+(k^1);
    }//处理草地情况
    
    for(int i=0;i<=(1<<n)-1;i++)
    if( check(i) && (i&f[1])==0 ) dp[1][i]=1;
    //预处理第一行草地情况
    
    for(int i=2;i<=m;i++)//从第二行开始枚举每一行
    {
        for(int j=0;j<=(1<<n)-1;j++)//枚举第i行的状态
        {
            if( check(j) && (j&f[i])==0 )//判断状态是否合法
            for(int k=0;k<=(1<<n)-1;k++)//枚举第i-1行的状态
            if((j&k)==0) dp[i][j]+=dp[i-1][k]%mod,dp[i][j]%=mod;
            //若合法则更新dp数组
        }
    }
    
    for(int i=0;i<=(1<<n)-1;i++)
    ans+=dp[m][i]%mod,ans%=mod;
    
    cout<<ans;
    return 0;
}

洛谷P1879 [USACO06NOV]玉米田Corn Fields【状压DP】题解+AC代码

标签:main   建议   sigma   长方形   c代码   sid   get   vector   string   

原文地址:https://www.cnblogs.com/niiick/p/8470128.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!