Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 17749 | Accepted: 9342 |
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.
Source
题意:给一个n*m的矩阵,上面1表示肥沃的土地,0表示贫瘠的土地,然后要让牛必须站在肥沃的土地上,又不能相邻(可以整个都不站牛)
题解:那就用二进制来表示整个土地的样子,状态转移是dp[i][j] = sum(dp[i-1][k]) 表示第i行的站法是由i-1行所有可能的站法并且两行不能冲突的和
技巧:
1.判断一个数字是否有相邻的两个1 :x&(x<<1)=0,则表示没有,否则有。
2.两行是否有上下相邻的:若x&y=0,则没有,否则有。
3. 判断当前站位是否满足土地条件:若maps[i]=maps[i] | x,则x情况下的站位满足土地条件。
代码:
//#include "bits/stdc++.h"
#include "cstdio"
#include "map"
#include "set"
#include "cmath"
#include "queue"
#include "vector"
#include "string"
#include "cstring"
#include "time.h"
#include "iostream"
#include "stdlib.h"
#include "algorithm"
#define db double
#define ll long long
//#define vec vector<ll>
#define Mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define rep(i, x, y) for(int i=x;i<=y;i++)
const int N = 1e5 + ;
const int mod = 1e9 + ;
const int MOD = mod - ;
const db eps = 1e-;
const db PI = acos(-1.0);
using namespace std;
int maps[],ok[N];
int f[][N];
int n,m;
bool cal1(int i){
return i&(i<<);//检查相邻的1
}
bool cal2(int x,int y){
return maps[x]==(maps[x]|ok[y]);//检查合理的站位与土地条件是否冲突
}
int main()
{
while(scanf("%d%d",&n,&m)==&&n!=)
{
memset(maps,, sizeof(maps));
memset(f,, sizeof(f));
for(int i=;i<n;i++)
{
for(int j=;j<m;j++){
int x;
ci(x);
if(x!=) maps[i]+=(<<j);//更新第i行的土地
}
}
int cnt=;
for(int i=;i<(<<m);i++){
if(!cal1(i)) ok[cnt++]=i;//保存合理的站位
}
for(int i=;i<cnt;i++){
if(cal2(,i)) f[][i]=;//处理出第一行的合理站位
}
ll ans=;
for(int i=;i<n;i++){
for(int j=;j<cnt;j++){
if(!cal2(i,j)) continue;//检查对于第i行是否合理
for(int k=;k<cnt;k++){//检查与i-1行是否冲突
if(!(ok[j]&ok[k])) f[i][j]+=f[i-][k];
}
}
}
for(int i=;i<cnt;i++) ans+=f[n-][i],ans%=;
pl(ans);
}
return ;
}