Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 10658 | Accepted: 5602 |
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列的矩阵,矩阵里的元素由0和1组成,1代表肥沃的土地可以种植作物,
0则不可以种植作物,并且相邻的土地不能同时种植作物,问你有多少种种植方案。
定义dp[i][j]表示第i行在j状态时,前i行种植的方法数;
状态转移方程:
dp[i][j] += dp[i-1][k]; //正确性,dp[0][0] = 1,边界;每一个dp[n][j]都是一个根;向下的分支总数就是方案总数;
他们的分支已经包含了到当前位置的所有情况;
答案:
sum += dp[m][j];
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<set>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<cmath>
#include<cctype>
#include<stack>
#include<iomanip>
using namespace std;
typedef pair<int,int> pr;
typedef long long ll;
const int maxn = ;
const ll mod = 1e8;
const ll inf = 0x3f3f3f3f;
ll dp[][<<];
ll mp[], valid[<<], vz;
bool ok(ll x,ll i)
{
if((x&mp[i])!=x) return false;// 在x二进制表示,不是mp[i]的子集,则不行;
return true;
}
void getvalid(ll m)//预处理有效的,把一行中存在相邻的状态清除;
{
vz = ;
ll t = (<<m);
for(ll i = ; i < t; i++){
if(((i<<)&i)==){
valid[vz++] = i;
}
}
}
int main()
{
ll n, m;
while(scanf("%lld%lld",&n,&m)==)
{
ll e;
//从(1,1)开始好;
for(ll i = ; i <= n; i++){
mp[i] = ;
for(ll j = ; j <= m; j++) {
scanf("%lld",&e);
mp[i] = (mp[i]<<)+e;
}
}
getvalid(m);
memset(dp,,sizeof(dp));
dp[][] = ;//不种植一棵时,方案数为1;
for(ll i = ; i <= n; i++){
for(ll j = ; j < vz; j++){
if(!ok(valid[j],i)) continue;
for(ll k = ; k < vz; k++){
if(valid[j]&valid[k]) continue;//上下两行不可以有相邻的;
dp[i][valid[j]] = (dp[i][valid[j]]+dp[i-][valid[k]])%mod;
}
}
}
ll sum = ;
for(ll j = ; j < vz; j++){
sum = (sum+dp[n][valid[j]])%mod;
}
printf("%lld\n",sum);
}
return ;
}