Time Limit: 2000MS | Memory Limit: 65536K |
---|
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.ios
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.数组
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)markdown
Line 1: One integer: the number of ways that FJ can choose the squares modulo 100,000,000.ide
2 3
1 1 1
0 1 0ui
9spa
Number the squares as follows:code
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.three
USACO 2006 November Goldcoffeescript
题意:在一个n*m的草场上,每一块草场均可以放一只牛,有的草场是贫瘠的,因此不能放牛。因为这些牛比较孤僻,因此不喜欢在本身吃草的地方周围有其余的牛(上下左右),问总共有多少种放法。ip
思路:在图上的每个点均可能放牛(除去荒地),因此搜索的时间复杂度很高。
- 假设咱们知道矩阵的n和m,咱们用二进制表示在一行的放牛的状态(不考虑荒地),则全部的状态为[0,1 << m),状态是否符合能够根据(x&(x<<1)),返回值为0表示没有相邻的1,则符合条件,不然不符合条件,这样咱们就记录在草场宽为m时的全部能够放的状况,记录在Sta数组中。
- 因为有荒地,因此对于每一行的第j个草地若是为荒地则为1,不然为零,这样就能够每一行有一个数表示他们的草地的状态,存在Map数组中,若是要判断在符合状况的方式中是否是有矛盾的,能够用Map[i]&Sta[j]==0(表示第i行草地与第j种方式),若是等于零则符合,不等于零则不符合(在纸上写写看看为何)。
- 首先将第一行的因此状态初始化即Dp[1][j]=(Map[1]&Sta[j])==0?1:0,而后根据第一行的全部状态去枚举第二行看是否符合,即
for i=2 -> n { for j=0 -> num { if Map[i]&Sta[j] == 1 { continue; } for k=0 -> num { if Map[i-1]&Sta[k] == 1 { continue; } if Sta[j]&Sta[k]==0 { Dp[i][j]+=Dp[i-1][k] } } } }
具体状况见代码
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
const int Max = 1<<13;
const int Mod = 100000000;
int Dp[15][Max];
int Map[15];
int Sta[Max];
bool Judge(int x)
{
return (x&(x<<1));
}
bool Dec(int x,int y)
{
return (Map[x]&Sta[y]);
}
int main()
{
int n,m;
while(~scanf("%d %d",&n,&m))
{
memset(Map,0,sizeof(Map));
memset(Dp,0,sizeof(Dp));
memset(Sta,0,sizeof(Sta));
int data;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%d",&data);
if(data==0)
{
Map[i]+=(1<<(j-1)); //每一行的状况用一个数来表示
}
}
}
int num = 0 ;
for(int i=0;i<(1<<m);i++)//全部符合方式
{
if(!Judge(i))
{
Sta[num++] = i;
}
}
for(int i=0;i<num;i++)//第一行的全部状态初始化
{
if(!Dec(1,i))
{
Dp[1][i]=1;
}
}
for(int i=2;i<=n;i++)
{
for(int j=0;j<num;j++)
{
if(Dec(i,j))// 是否符合
{
continue;
}
for(int k=0;k<num;k++)
{
if(Dec(i-1,k))//i-1行是否符合第k种状况
{
continue;
}
if(!(Sta[j]&Sta[k])) //上下行之间也互相的知足条件
{
Dp[i][j]+=Dp[i-1][k];
}
}
}
}
int ans =0;
for(int i=0;i<num;i++)
{
ans+=Dp[n][i];
ans%=Mod;
}
printf("%d\n",ans);
}
return 0;
}