狀態DPspa
#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); } }