标签:des style blog http color java os io
2 6 3 1 1 1 1 0 1 1 1 1 1 1 1 1 0 1 1 1 1 2 4 1 1 1 1 1 1 1 1
Case 1: There are 3 ways to eat the trees. Case 2: There are 2 ways to eat the trees.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define maxn 205
#define MAXN 100005
#define mod 100000000
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-8
typedef long long ll;
using namespace std;
ll n,m,ans,cnt,tot,flag;
ll mp[12][12],dp[12][12][1<<12];
void solve()
{
ll i,j,k,t;
ll up,down,left,right;
tot=(1<<m+1)-1;
memset(dp,0,sizeof(dp));
dp[0][m][0]=1;
for(i=1;i<=n;i++)
{
for(k=0;k<(1<<m);k++) // 行与行之间单独转移
{
dp[i][0][k<<1]=dp[i-1][m][k];
}
for(j=1;j<=m;j++)
{
for(k=0;k<=tot;k++) // i j-1的状态
{
if(dp[i][j-1][k]==0) continue ;
down=left=1<<j-1;
right=up=1<<j;
if(mp[i][j])
{
if((k&left)&&(k&up)) // 两个插头
{
dp[i][j][k^left^up]+=dp[i][j-1][k];
}
else if(!(k&left)&&!(k&up)) // 0个插头
{
dp[i][j][k^left^up]+=dp[i][j-1][k];
}
else // 一个插头
{
dp[i][j][k]+=dp[i][j-1][k];
dp[i][j][k^left^up]+=dp[i][j-1][k];
}
}
else
{
if(!(k&left)&&!(k&up)) dp[i][j][k]+=dp[i][j-1][k];
}
}
}
}
ans=dp[n][m][0];
}
int main()
{
ll i,j,t,test=0;
scanf("%I64d",&t);
while(t--)
{
scanf("%I64d%I64d",&n,&m);
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
scanf("%I64d",&mp[i][j]);
}
}
solve();
printf("Case %I64d: There are %I64d ways to eat the trees.\n",++test,ans);
}
return 0;
}
hdu 1693 Eat the Trees (插头dp入门),布布扣,bubuko.com
hdu 1693 Eat the Trees (插头dp入门)
标签:des style blog http color java os io
原文地址:http://blog.csdn.net/tobewhatyouwanttobe/article/details/38521827