[USACO06NOV] Corn Fields

https://www.luogu.org/problem/show?pid=1879

题目描述

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:

2 3
1 1 1
0 1 0

输出样例#1:

9

状压DP
#include<cstdio>
#define mod 1000000000
using namespace std;
int n,m;
int poor[13],separate[6001],cnt,dp[13][6001];
bool not_adjoin_lr(int i)
{
    if(i&i<<1) return false;
    return true;
}
bool not_adjoin_ud(int i,int j)
{
    if(i&j) return false;
    return true;
}
bool can(int i,int h)
{
    if(i&poor[h]) return false;
    return true;
}
int main()
{
    int x;
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
     for(int j=0;j<m;j++)
      {
           scanf("%d",&x);
           if(!x) poor[i+1]+=1<<j;
      }
    int tot=1<<m;
    for(int i=0;i<tot;i++)
     if(not_adjoin_lr(i))
      separate[++cnt]=i;
    for(int i=1;i<=cnt;i++)
     if(can(separate[i],1)) dp[1][i]=1;
    for(int i=2;i<=n;i++)
    {
        for(int j=1;j<=cnt;j++)
        {
            if(!can(separate[j],i-1)) continue;
            for(int k=1;k<=cnt;k++)
            {
                if(!can(separate[k],i)) continue;
                if(!not_adjoin_ud(separate[j],separate[k])) continue;
                dp[i][k]=(dp[i][k]+dp[i-1][j])%mod;
            }
        }
    }
    int ans=0;
    for(int i=1;i<=cnt;i++) ans=(ans+dp[n][i])%mod;
    printf("%d",ans);
}
				
时间: 2024-08-11 03:37:08

[USACO06NOV] Corn Fields的相关文章

[USACO06NOV]Corn Fields(状压DP)

https://www.luogu.com.cn/problem/P1879 题目描述 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 o

洛谷 P1879 [USACO06NOV]玉米田Corn Fields

P1879 [USACO06NOV]玉米田Corn Fields 题目描述 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

P1879 [USACO06NOV]玉米田Corn Fields

P1879 [USACO06NOV]玉米田Corn Fields 这道题跟互不侵犯差不多,但是数据范围更大了 对于状态压缩dp,提前枚举出转移是一种很好的优化 然后对于转移也需要仔仔细细的分析,如同期望dp #include<cstdio> #include<algorithm> #include<iostream> const int mod=1e8; int map[20]; long long f[2][1<<12]; int b[400],tot;

Poj 3254 Corn Fields(状态压缩)

Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8291   Accepted: 4409 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 yumm

BZOJ1725: [Usaco2006 Nov]Corn Fields牧场的安排

1725: [Usaco2006 Nov]Corn Fields牧场的安排 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 400  Solved: 290[Submit][Status] Description Farmer John新买了一块长方形的牧场,这块牧场被划分成M列N行(1<=M<=12; 1<=N<=12),每一格都是一块正方形的土地.FJ打算在牧场上的某几格土地里种上美味的草,供他的奶牛们享用.遗憾的是,有些土地相当的贫

POJ 3254 Corn Fields 状态压缩DP (C++/Java)

http://poj.org/problem?id=3254 题目大意: 一个农民有n行m列的地方,每个格子用1代表可以种草地,而0不可以.放牛只能在有草地的,但是相邻的草地不能同时放牛, 问总共有多少种方法. 思路: 状态压缩的DP. 可以用二进制数字来表示放牧情况并判断该状态是否满足条件. 这题的限制条件有两个: 1.草地限制. 2.相邻限制. 对于草地限制,因为输入的时候1是可以种草地的. 以"11110"草地分析,就只有最后一个是不可以种草的.取反后得00001  .(为啥取反

POJ3254 Corn Fields 状态压缩DP

题目大意是在一块M行N列的农场上种谷物,但是不希望彼此相邻(共用一条边),并且有些地方不能种植谷物,给定M,N(范围都不超过12)以及一些不能种谷物的位置,求出一共有多少种方法种谷物. 状态压缩DP,设dp(i, k) 为种到第i行时,第i行状态为k的总共方案数,可以知道dp(i, k) = ∑dp(i -1, k'),其中我们要判断彼此相邻的情况以及不能种植的情况即可. #include <stdio.h> #include <vector> #include <math.

【BZOJ1725】[Usaco2006 Nov]Corn Fields牧场的安排 状压DP

[BZOJ1725][Usaco2006 Nov]Corn Fields牧场的安排 Description Farmer John新买了一块长方形的牧场,这块牧场被划分成M列N行(1<=M<=12; 1<=N<=12),每一格都是一块正方形的土地.FJ打算在牧场上的某几格土地里种上美味的草,供他的奶牛们享用.遗憾的是,有些土地相当的贫瘠,不能用来放牧.并且,奶牛们喜欢独占一块草地的感觉,于是FJ不会选择两块相邻的土地,也就是说,没有哪两块草地有公共边.当然,FJ还没有决定在哪些土地

POJ 3254 Corn Fields(状态压缩DP)

Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4739   Accepted: 2506 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 yumm