【HDOJ】【1693】Eat The Trees

时间:2023-03-09 09:17:46
【HDOJ】【1693】Eat The Trees

插头DP

  插头dp模板题……

  这题比CDQ论文上的例题还要简单……因为不用区分左右插头(这题可以多回路,并不是一条哈密尔顿路)

  硬枚举当前位置的状态就好了>_<

  题解:http://blog.****.net/xymscau/article/details/6756351

 //HDU 1693
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;++i)
#define F(i,j,n) for(int i=j;i<=n;++i)
#define D(i,j,n) for(int i=j;i>=n;--i)
using namespace std;
int getint(){
int v=,sign=; char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') sign=-; ch=getchar();}
while(isdigit(ch)) {v=v*+ch-''; ch=getchar();}
return v*sign;
}
/*******************template********************/
const int N=;
typedef long long LL;
int mp[N][N];
LL dp[N][N][<<N];
int n,m; void DP(){
memset(dp,,sizeof (dp));
dp[][m][]=;
F(i,,n){//第i行
// for(int j=0;j<(1<<m);j++)
rep(j,(<<m))
dp[i][][(j<<)]=dp[i-][m][j];
//换行(将上一行最后一格状态放到这一行第一格,方便转移……) F(k,,m)//第k个格子
rep(sta,<<(m+)){//枚举当前状态sta
int y=<<k,x=<<(k-);
if(mp[i][k]){//如果当前格子无障碍
if( (sta&x)!= && (sta&y)!= )//如果状态为(1,1)
dp[i][k][sta]=dp[i][k-][sta-x-y];//从(0,0)转移过来
else if( (sta&x)== && (sta&y)== )//如果状态为(0,0)
dp[i][k][sta]=dp[i][k-][sta+x+y];//从(1,1)转移过来
else //状态为(0,1)或(1,0)则从(1,0)&&(0,1)转过来
dp[i][k][sta]=dp[i][k-][sta^x^y]+dp[i][k-][sta];
}//如果当前格子有障碍
else
if( (sta&x)== && (sta&y)==)//若当前状态为(0,0)
dp[i][k][sta]=dp[i][k-][sta];//则ans从前一格
else
dp[i][k][sta]=;
}
}
printf("There are %lld ways to eat the trees.\n",dp[n][m][]);
}
int main(){
int T=getint(),num=;
while(T--){
num++;
n=getint();m=getint();
F(i,,n)
F(j,,m) mp[i][j]=getint();
printf("Case %d: ",num);
DP();
}
return ;
}

(带注释)