您的位置:首页 > 其它

poj3254 Corn Fields (状压dp)

2016-07-15 20:20 323 查看
Corn Fields

Time Limit: 2000MSMemory Limit: 65536K
Total Submissions: 12463Accepted: 6548
DescriptionFarmer 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.
InputLine 1: Two space-separated integers: M and N

Lines 2..M+1: Line i+1 describes row i of the pasture with N space-separated integers indicating whether a square is fertile (1 for fertile, 0 for infertile)OutputLine 1: One integer: the number of ways that FJ can choose the squares modulo 100,000,000.Sample Input2 3
1 1 1
0 1 0Sample Output9

就是一个状压dp

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int mod=(int)1e8;

int n,m;
int a[13];
int dp[13][1<<13];

bool J(int i,int j) {
if((a[i]&j)^j)return false;
if((j<<1)&j)return false;
return true;
}
int main() {
while(~scanf("%d%d",&n,&m)) {
memset(a,0,sizeof(a));
for(int i=1; i<=n; ++i) {
for(int j=1; j<=m; ++j) {
int num;
scanf("%d",&num);
a[i]=(a[i]<<1)+num;
}
}
memset(dp,0,sizeof(dp));
dp[0][0]=1;
int ans=0;
for(int i=1; i<=n; ++i) {
for(int j=0; j<(1<<m); ++j) {
if(!J(i,j))continue;
for(int k=0; k<(1<<m); ++k) {
if(j&k)continue;
dp[i][j]+=dp[i-1][k];
dp[i][j]%=mod;
}
}
}
for(int i=0; i<(1<<m); ++i) {
(ans+=dp
[i])%=mod;
}
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: