http://poj.org/problem?id=3254

 

状压DP:

 

 1 #include<iostream>
 2 #include<cstring>
 3 using namespace std;
 4 
 5 int dp[13][1<<13];
 6 int mp[13],s[1<<13];
 7 int n,m;
 8 
 9 int check1(int x)
10 {
11     return x&(x<<1);
12 }
13 
14 int check2(int x,int p)
15 {
16     return mp[x]&p;
17 }
18 
19 int main()
20 {
21     cin>>n>>m;
22     memset(dp,0,sizeof(dp));
23     memset(mp,0,sizeof(mp));
24     memset(s,0,sizeof(s));
25     for (int i=1;i<=n;i++)
26     {
27         for (int j=1;j<=m;j++)
28         {
29             int x;
30             cin>>x;
31             if (x==0) mp[i]+=1<<(j-1);
32         }
33     }
34     int cut=0;
35     for (int i=0;i<(1<<m);i++)
36     {
37         if (!check1(i)) s[cut++]=i;
38     }
39     for (int i=1;i<=n;i++)
40     {
41         for (int j=0;j<cut;j++)
42         {
43             if (check2(i,s[j])) continue;
44             if (i==1)
45             {
46                 dp[1][j]=1;
47                 continue;
48             }
49             for (int k=0;k<cut;k++)
50             {
51                 if (s[j]&s[k]) continue;
52                 dp[i][j]+=dp[i-1][k];
53             }
54         }
55     }
56     int ans=0;
57     for (int i=0;i<cut;i++)
58     {
59         ans=(ans+dp[n][i])%100000000;
60     }
61     cout<<ans<<endl;
62 }

 

posted on 2016-03-15 16:09  pb2016  阅读(145)  评论(0编辑  收藏  举报