您的位置:首页 > 其它

poj3254 Corn Fields 状态压缩

2013-07-16 10:06 274 查看
Corn Fields

Time Limit: 2000MSMemory Limit: 65536K
Total Submissions: 5038Accepted: 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 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.
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;//第一行不受其他影响,满足题意即是一种方法,所以赋值为1
for(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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: