洛谷P1879 [USACO06NOV]玉米田Corn Fields【状压DP】

时空限制 1000ms / 128MB

题目描述

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的余数。


题目分析

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

d p [ i ] [ j ] = ∑ d p [ i − 1 ] [ k ] dp[i][j]=\sum dp[i-1][k] dp[i][j]=dp[i1][k]其中 j , k j,k j,k两状态都合法
则最后 a n s = ∑ d p [ n ] [ j ] ans=\sum dp[n][j] ans=dp[n][j]其中 0 &lt; = j &lt; = ( 1 &lt; &lt; n ) − 1 0 &lt;= j &lt;= (1&lt;&lt; n)-1 0<=j<=(1<<n)1 j j j合法

关于状态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是否与土地情况冲突
我们在读入时记录 s t [ i ] st[i] st[i]表示第 i i i行的土地情况
我们用1表示能种,0表示不能种
这样我们判断状态x是否合法可以这样

if((~st[1])&rem[i]) return 0

s t st st每位取反,那么1表示不能种
&运算后返回不等于0说明状态x一定在不合法的位置种了草

考虑到状态转移的时候每次都要枚举 2 m 2^m 2m个状态
其中有些状态本身就不合法(即相邻位置种了草),这样会造成无用枚举
所以可以一开始先预处理出只考虑不相邻的所有合法方案,大大减少枚举量
并且可以把 d p [ ] [ j ] dp[][j] dp[][j]重新定义为第 j j j个合法方案,大幅减少空间消耗


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

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=100000000;
const int maxn=1010;
int n,m,ans;
int st[20],rem[maxn],cnt;
int dp[20][maxn];

void check()
{
    for(int x=0;x<=(1<<m)-1;++x)
    if( (x&(x<<1)) || (x&(x>>1)) ) continue;
    else rem[++cnt]=x;
}

int main()
{
    n=read();m=read();
    for(int i=1;i<=n;++i)
    for(int j=1;j<=m;++j)
    {
    	int x=read();
    	if(x==1) st[i]|=(1<<j-1);
    }
    
    check();
    
    for(int i=1;i<=cnt;++i)
    if((~st[1])&rem[i]) continue;
    else dp[1][i]=1;
    
    for(int i=2;i<=n;++i)
    for(int j=1;j<=cnt;++j)
    {
        if((~st[i])&rem[j]) continue;
        for(int k=1;k<=cnt;++k)
        {
            if(((~st[i-1])&rem[k])||(rem[j]&rem[k])) continue;
            dp[i][j]=(dp[i][j]+dp[i-1][k])%mod;
        }
    }
    
    for(int i=1;i<=cnt;++i)
    ans=(ans+dp[n][i])%mod;
    printf("%d",ans);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值