hdu1400 状态压缩dp

 1 #include<stdio.h>
 2 #include<string.h>
 3 long long result[15][15],dp[15][2050];
 4 int n,m,t;
 5 long long dfs(int l,int state)
 6 {
 7     int i,j,judge,flag,last;
 8     long long sum;
 9     if (dp[l][state]!=-1) return dp[l][state];
10     if (l==1)
11     {
12         for (j=0;j<m;j++) if ((state&(1<<(j)))==0) {dp[1][state]=0; return 0; }
13         dp[1][state]=1; return 1;
14     }
15     last=state^((1<<m)-1); sum=0;
16     for (i=0;i<(1<<m);i++)
17     {
18         flag=1;
19         for (j=0;j<m;j++)
20          if (((last&(1<<j))!=0)&&((i&(1<<j))==0)) flag=0;
21         if (flag==0) continue;
22         judge=last^i;
23         for (j=0;j<m;j++)
24         {
25             if ((judge&(1<<j))!=0)
26             {
27                 if (j==m-1||((judge&(1<<(j+1)))==0)) {flag=0; break;}
28                 j++;
29             }
30         }
31         if (flag==1) sum+=dfs(l-1,i);
32     }
33     dp[l][state]=sum;
34     return dp[l][state];
35 }
36 int main()
37 {
38     memset(result,0,sizeof(result));
39     while (~scanf("%d%d",&n,&m)&&(n+m))
40     if ((n*m)%2) printf("0\n"); else
41     {
42         if (n>m) { t=n; n=m; m=t; }
43         if (result[n][m]) printf("%I64d\n",result[n][m]); else
44         {
45             memset(dp,-1,sizeof(dp));
46             result[n][m]=dfs(n+1,(1<<m)-1);
47             printf("%I64d\n",result[n][m]);
48         }
49     }
50 }

http://acm.hdu.edu.cn/showproblem.php?pid=1400

posted on 2014-07-21 23:38  xiao_xin  阅读(152)  评论(0编辑  收藏  举报

导航