poj3254 Corn Fields 状态压缩

来源:互联网 发布:哈尔滨淘宝代卖货 编辑:程序博客网 时间:2024/06/07 01:53
Corn Fields
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 5038 Accepted: 2662

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 31 1 10 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.

Source

USACO 2006 November Gold
在几块地上放牛,只有标号1的才可以放牛而且每头牛的相邻位置不得有牛,问总共有多少种方法。最多12行12列,可以用二进制1 0表示一个位置有牛和没牛,每行最多2^12个状态,可以用状态压缩
#include<stdio.h>int n,m,a[13][13];int i,j,tem;struct len{int num;int s[6000];}g[13];void  var(){int k,t=0;for(k=0;k<(1<<n);k++)if(!(tem&k))//说明没有选择贫瘠的土地{if(k&(k<<1))//排除同一行奶牛相邻的情况continue;g[i].s[t++]=k;//记录满足条件的状态}g[i].num=t;}int main(){int k,sum,d[13][1024];while(scanf("%d%d",&m,&n)!=EOF){for(i=0;i<m;i++){tem=0;for(j=0;j<n;j++){scanf("%d",&a[i][j]);tem=tem<<1;tem+=1-a[i][j];//将图用二进制存起来0表示可以放牛的土地1表示贫瘠的土地,图和是否有牛表示方法是相反的}var();}for(i=0;i<g[0].num;i++)d[0][i]=1;//第一行不受其他影响,满足题意即是一种方法,所以赋值为1for(i=1;i<m;i++){for(k=0;k<g[i].num;k++){d[i][k]=0;for(j=0;j<g[i-1].num;j++)if(!(g[i].s[k]&g[i-1].s[j]))//前后不相邻{d[i][k]+=d[i-1][j];//累加}}}sum=0;for(i=0;i<g[m-1].num;i++)sum+=d[m-1][i];printf("%d\n",sum%100000000);//题目要求啊,开始没注意WA了几次}return 0;}