Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 10923 | Accepted: 5723 |
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
状态压缩,将一行的可能出现的可行状态打表
判断每一行与上一行的关系
dp[i][j] += dp[i-1][k]; 0 <k< 1<<m
/*************************************************
Author :SunMing
Created Time :2016年01月26日 星期二 08时18分21秒
File Name :A.cpp
************************************************ */
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int mod = 1e8;
int n,m;
int dp[15][1<<12], state[1<<12], Map[15][1<<12];
void Init()
{
int top = 0;
for(int i = 0; i < (1<<12); i++)
if(!(i & (i<<1)))
state[top++] = i;
}
bool Judge(int x,int y)
{
for(int i=1;i<=m;i++)
if((y & 1<<(i-1)) && !Map[x][i])
return false;
return true;
}
int main()
{
Init();
while(~scanf("%d%d",&n,&m))
{
memset(dp, 0, sizeof(dp));
memset(Map, 0, sizeof(Map));
for(int i=1;i<=n;i++)
for(int j=1; j<=m;j++)
scanf("%d",&Map[i][j]);
for(int i=1; i<=n;i++)
{
for(int j=0; state[j] <(1<<m); j++)
{
if(Judge(i, state[j]))
{
if(i == 1)
{
dp[i][j] = 1;
continue;
}
for(int k=0; state[k] < (1<<m); k++)
{
if((state[j] & state[k]) == 0 )
dp[i][j] += dp[i-1][k];
}
}
}
}
long long ans = 0;
for(int i=0; state[i] < (1<<m); i++)
ans = (ans + dp[n][i]) % mod;
cout<<ans<<endl;
}
return 0;
}