【每日一题】德玛西亚万岁
德玛西亚万岁
https://ac.nowcoder.com/acm/problem/15034
题意:
思路:
#include <cstdio> #include <cstring> #include <vector> #include <iostream> using namespace std; const int N = 15; const int mod = 100000000; int s[N];//每行的状态 int dp[N][4100];//dp(i,j)排到第i行,且第i行的状态为j的合法安排方案 int n,m; vector<int> v; //筛选所有合法状态 void init(){ v.clear(); for(int i = 0;i < (1<<m);i++){ if(i & i<<1) continue; v.push_back(i); } } int main(){ while(~scanf("%d%d",&n,&m)){ for(int i = 1;i <= n;i++){ int state = 0; for(int j = 0,x;j < m;j++){ scanf("%d",&x); state <<= 1; if(x) state |= 1; } s[i] = state; } init(); memset(dp,0,sizeof(dp)); dp[0][0] = 1; //s1上一行的状态,s2这一行的状态 int s1,s2; for(int i = 1;i <= n+1;i++){ for(int j = 0;j < v.size();j++){ s1 = v[j]; if((s1&s[i]) != s1) continue; for(int k = 0;k < v.size();k++){ s2 = v[k]; if(s1&s2) continue; dp[i][s1] = (dp[i][s1] + dp[i-1][s2]) % mod; } } } printf("%d\n",dp[n+1][0]); } return 0; }