解题思路:考虑到m比较小并且每块地有两种状态,故可以将状态压缩,用一个数表示一行的状态。从上一行到下一行转移时有两个限制,第一,同一行相邻的地不能同时种,上下两行相邻的地不能同时种。
例如样例,第一行有5中状态,000,001,010,100,101.第二行有两种状态000,010.
那么第二行中000可以由第一行的5中转移有5中,010不能有第一行的010转移故有4种,共九种。
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
using namespace std;
typedef long long LL;
const LL INF = 100000000000;
const int mod = 1e8;
const double EPS = 1e-9;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
int dcmp(double x, double y){
if(fabs(x - y) < EPS) return 0;
return x > y ? 1 : -1;
}
int m, n;
int a[20][20];
int dp[20][1 << 13];
bool ok(int x, int r){
for(int i = 0; i < m; ++i){
if((1 << i) & x && (1 << (i + 1)) & x) return false;
if((1 << i) & x && !a[r][i]) return false;
}
return true;
}
void solve(){
int s = (1 << m) - 1;
memset(dp, 0, sizeof dp);
for(int i = 1; i < s; ++i){
if(ok(i, 0)) {
dp[0][i] = 1;
}
}
for(int i = 1; i < n; ++i){
for(int j = 0; j < s; ++j){
if(ok(j, i)){
if(j) dp[i][j] = 1;
for(int k = 0; k < s; ++k){
if(!(j & k)) dp[i][j] = (dp[i][j] + dp[i - 1][k]) % mod;
}
}
}
}
int ans = 0;
for(int i = 0; i < s; ++i){
ans = (ans + dp[n - 1][i]) % mod;
// printf("%d : %d\n",i, dp[n - 1][i]);
}
printf("%d\n", ans + 1);
}
int main(){
while(scanf("%d%d", &n, &m) == 2){
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
scanf("%d", &a[i][j]);
solve();
}
return 0;
}