(简单) POJ 3254 Corn Fields,状压DP。

时间:2021-05-10 15:20:40

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.

  题目就是求方案数,一看数据范围,差不多就是状态压缩啥的,然后DP就好。

  dp[i][j] 表示前i列,第i列的分布状态为j时的方案数。

代码如下:

// ━━━━━━神兽出没━━━━━━
// ┏┓ ┏┓
// ┏┛┻━━━━━━━┛┻┓
// ┃ ┃
// ┃ ━ ┃
// ████━████ ┃
// ┃ ┃
// ┃ ┻ ┃
// ┃ ┃
// ┗━┓ ┏━┛
// ┃ ┃
// ┃ ┃
// ┃ ┗━━━┓
// ┃ ┣┓
// ┃ ┏┛
// ┗┓┓┏━━━━━┳┓┏┛
// ┃┫┫ ┃┫┫
// ┗┻┛ ┗┻┛
//
// ━━━━━━感觉萌萌哒━━━━━━ // Author : WhyWhy
// Created Time : 2015年07月21日 星期二 09时43分34秒
// File Name : 3254.cpp #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h> using namespace std; const int mod=; int N,M;
int map1[][];
int rem[];
long long dp[][]; int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); long long ans; while(~scanf("%d %d",&N,&M))
{
for(int i=;i<=N;++i)
for(int j=;j<=M;++j)
scanf("%d",&map1[i][j]); for(int i=;i<=M;++i)
{
rem[i]=; for(int j=;j<=N;++j)
if(!map1[j][i])
rem[i]|=(<<(j-));
} memset(dp,,sizeof(dp)); for(int j=;j<(<<N);++j)
if(!(j&rem[] || j&(j<<)))
++dp[][j]; for(int i=;i<=M;++i)
for(int j=;j<(<<N);++j)
{
if(j&(j<<) || j&rem[i])
continue; for(int k=;k<(<<N);++k)
{
if(j&k)
continue; (dp[i][j]+=dp[i-][k])%=mod;
}
} ans=; for(int j=;j<(<<N);++j)
(ans+=dp[M][j])%=mod; printf("%lld\n",ans);
} return ;
}