POJ 2411 记录状态的dp

和poj3254一样

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include <set>
#include <cmath>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define ll long long
ll dp[(1<<12)+100][13];
int h,w;

ll dfs(int i,int j,int left,int now)
{
	if(i==h+1)
		return dp[now][w-j]=dfs(1,j+1,now,0);
	if(j==w+1)
		return left==(1<<(h+1))-2?1:0;
	if(dp[left][w-j+1]!=-1)
		return dp[left][w-j+1];
	ll ret=0;
	if(!(left&(1<<i))) // 左边为空
		ret+=dfs(i+1,j,left,now|(1<<i));
	else 
	{
		if(!(now&(1<<(i-1))) && i!=1) // 上边为空,并且i不是1
			ret+=dfs(i+1,j,left,now|(1<<i)|(1<<(i-1)));
		ret+=dfs(i+1,j,left,now); 
	}
	return ret;
}

int main ()
{
	while(scanf("%d%d",&h,&w)!=EOF)
	{
		if(h==0 && w==0) break;
		memset(dp,-1,sizeof(dp));
		ll ans=dfs(1,1,(1<<(h+1))-2,0);
		printf("%lld\n",ans);
	}
	return 0;
}


你可能感兴趣的:(POJ 2411 记录状态的dp)