| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 8191 | Accepted: 4365 |
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
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
Sample Input
2 3 1 1 1 0 1 0
Sample Output
9
Hint
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.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int mod = 1000000000;
int dp[13][1<<13];//第i行
int map[1<<13];//记录田地状态,每行一个值
int a[1<<13];//记录所有不存在相邻两块土地的值
int main()
{
int m,n;
while(~scanf("%d%d",&m,&n))
{
memset(a,0,sizeof(a));
memset(map,0,sizeof(map));
memset(dp,0,sizeof(dp));
for(int i=1;i<=m;i++)
{
for(int j=0;j<n;j++)
{
int temp;
scanf("%d",&temp);
if(temp==0)
map[i]+=(1<<j);//map[i]存的是所有的贫瘠土地
}
}
int cur=0;
for(int i=0;i<(1<<n);i++)
{
if(!(i&(i<<1)))//判断相邻是否存在相邻两块土地
{
a[cur++]=i;
}
}
for(int i=0;i<cur;i++)
{
if(!(map[1]&a[i]))//所有贫瘠土地都不取
dp[1][i]=1;
}
for(int i=2;i<=m;i++)//第i行
{
for(int j=0;j<cur;j++)//枚举 所有不存在相邻两块土地的方案
{
if(map[i]&a[j])//如果取到了贫瘠土地,跳出
continue;
for(int k=0;k<cur;k++)//枚举上一层
{
if(map[i-1]&a[k])//如果取到了贫瘠土地
continue;
if(!(a[j]&a[k]))//与上层不会与在同一列的情况
{
dp[i][j]+=dp[i-1][k];
}
}
}
}
long long ans=0;
for(int i=0;i<cur;i++)
{
ans=(ans+dp[m][i])%mod;
}
cout<<ans<<endl;
}
return 0;
}
农民约翰在购买了一块由M x N个矩形地块组成的牧场后,面临着如何选择哪些地块种植玉米以供奶牛食用的问题。他考虑了地块的肥沃程度和奶牛之间的距离因素,同时考虑了不种植任何地块的可能。本文详细介绍了计算所有可能种植组合的方法。
815

被折叠的 条评论
为什么被折叠?



