poj 2411

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
#define max( a,b ) ( (a)>(b)?(a):(b) )
using namespace std;

long long N,M,dp[12][4097];
void DFS( int sta, int c,long long stu,long long ans )
{
    if( sta >= M )return;
    DFS( sta+1,c,stu,ans );
    if( !(stu&(1<<sta)) && !(stu&(1<<(sta-1))) )
    {
        long long t = stu|(1<<sta)|(1<<(sta-1));
        if( dp[c][t] == -1 )dp[c][t]  = ans;
        else                dp[c][t] += ans;
        DFS( sta+1,c,t,ans);
    }
}
int main( )
{
    while( scanf("%I64d%I64d",&N,&M) && N + M )
    {
        memset( dp,-1,sizeof(dp) );
        dp[0][(1<<M)-1] = 1;
        for( int i = 1; i <= N; i++ )
        for( int j = 0; j < (1<<M); j++ )
         if( dp[i-1][j] != -1 )
         {
            dp[i][(~j)&((1<<M)-1)] =  dp[i-1][j];
            DFS( 1,i,(~j)&((1<<M)-1), dp[i-1][j] );
         }
        long long res = max(0,dp[N][(1<<M)-1]);
        printf("%I64d\n",res);
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/wulangzhou/p/3075617.html