POJ 3254 - Corn Fields - [状压DP水题]

题目链接:http://poj.org/problem?id=3254

Time Limit: 2000MS Memory Limit: 65536K

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.

Input

Line 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)

Output

Line 1: One integer: the number of ways that FJ can choose the squares modulo 100,000,000.

Sample Input

2 3
1 1 1
0 1 0

Sample Output

9

Hint

Number the squares as follows:
1 2 3
  4  

There are four ways to plant only on one squares (1, 2, 3, or 4), three ways to plant on two squares (13, 14, or 34), 1 way to plant on three squares (134), and one way to plant on no squares. 4+3+1+1=9.

题解:

怎么说呢,类似的状压DP题解写多了真的挺无聊的,如果你已经撸完了:

http://www.cnblogs.com/dilthey/p/7623028.html

http://www.cnblogs.com/dilthey/p/7604432.html

这两道题目的话,再来看本题,真的是很水的,所以就懒得写题解了,反正就是差不多的思路;

AC代码:

 #include<cstdio>
#include<cstring>
int m,n,mp[];
int dp[][<<];
int main()
{
while(scanf("%d%d",&m,&n)!=EOF)//m行n列
{
for(int i=,tmp;i<=m;i++)
{
mp[i]=;
for(int j=;j<=n;j++)
{
scanf("%d",&tmp);
tmp=!tmp;
mp[i]|=tmp;
if(j!=n) mp[i]<<=;
}
} memset(dp,,sizeof(dp));
for(int i=;i<(<<n);i++)//初始化第一行
{
if( i&(i<<) || i&mp[] ) continue;
dp[][i]=;
}
for(int r=;r<=m;r++)//遍历第2~m行
{
for(int i=;i<(<<n);i++)//枚举第r行状态
{
if( i&(i<<) || i&mp[r] ) continue;
for(int j=;j<(<<n);j++)//枚举第r-1行状态
{
if( j&(j<<) || j&mp[r-] || i&j ) continue;
dp[r][i]+=dp[r-][j];
}
}
} int ans=;
for(int i=;i<(<<n);i++)
{
if( i&(i<<) || i&mp[m] ) continue;
ans+=dp[m][i];
}
printf("%d\n",ans%);
}
}

PS.哦对,有一点倒是值得提醒,需要对100000000取模,因为忘记了这个WA了一发,羞愧脸。

上一篇:Corn Fields(POJ 3254状压dp)


下一篇:NET Core应用中如何记录和查看日志