状态DP
#include <stdio.h> #include <string.h> #include <stdlib.h> #define MOD 100000000 #define MAX 12 #define MAX_STATE (1 << 12) int dp[MAX][MAX_STATE]; int map[MAX]; //存储土地的状态 int st[MAX_STATE]; //存储所有可能的状态 //非0,则该状态不符合 int judge(int s) { return (s&(s << 1)); } //非0,则该状态不符合 int judgeState(int i, int s) { return (map[i] & st[s]); } int main() { int M, N; int i, j, s, k; int ans; while (~scanf("%d %d", &M, &N)) { memset(dp, 0, sizeof(dp)); memset(map, 0, sizeof(map)); memset(st, 0, sizeof(st)); //初始化map,将土地状态压缩 int temp; for (i = 0; i < M; i++) { for (j = 0; j < N; j++) { scanf("%d", &temp); if (temp == 0) { map[i] += (1 << j); } } } //初始化所有可能状态 k = 0; for (s = 0; s < (1 << N); s++) { if (!judge(s)) { st[k++] = s; } } //初始化dp[0][] for (s = 0; s < k; s++) { if (!judgeState(0, s)) { dp[0][s] = 1; } } for (i = 1; i < M; i++) { for (j = 0; j < k; j++) { if (judgeState(i, j)) continue; for (s = 0; s < k; s++) { if (judgeState(i - 1, s)) continue; if (!(st[j] & st[s])) { dp[i][j] += dp[i - 1][s]; } } } } ans = 0; for (s = 0; s < k; s++) { ans += dp[M - 1][s]; ans = ans % MOD; } printf("%d\n", ans); } }