0x56 状压DP
2024-08-31 09:04:00
gan这两题怎么差不多
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL; bool u[];
LL f[][];
int main()
{ int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==&&m==)break;
int li=(<<m)-; for(int i=;i<=li;i++)
{
int cc=;u[i]=true;
for(int j=m-;j>=;j--)
if(i&(<<j))
{
if(cc%==)u[i]=false;
cc=;
}
else cc++;
if(cc%==)u[i]=false;
} //-------------------------- memset(f,,sizeof(f));f[][]=;
for(int i=;i<=n;i++)
for(int zt=;zt<=li;zt++)
for(int lzt=;lzt<=li;lzt++)
if((lzt&zt)==&&u[lzt|zt]==true)
f[i][zt]+=f[i-][lzt];
printf("%lld\n",f[n][]);
}
return ;
}
poj2311
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL; char ss[][]; int b[];
int f[][][];
int len,z[],d[];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)scanf("%s",ss[i]+);
for(int i=;i<=n;i++)
{
b[i]=;
for(int j=;j<=m;j++)
if(ss[i][j]=='H')b[i]^=(<<(j-));
} int li=(<<m)-;len=;
for(int i=;i<=li;i++)
{
int cc=-,dd=;bool bk=true;
for(int j=;j<=m-;j++)
if(i&(<<j))
{
if(j-cc<=){bk=false;break;}
cc=j;dd++;
}
if(bk==true){z[++len]=i;d[len]=dd;}
} //-------------------------- int now=,ans=;f[now][][]=;
for(int i=;i<=n;i++)
{
now^=;
for(int j=;j<=len;j++) if(i==||(!(z[j]&b[i-])))
{
for(int k=;k<=len;k++) if( (!(z[k]&b[i])) && (!(z[j]&z[k])) )
{
f[now][j][k]=;
for(int p=;p<=len;p++) if( (i<=||(!(z[p]&b[i-]))) && (!(z[j]&z[p])) && (!(z[k]&z[p])) )
{
f[now][j][k]=max(f[now][j][k],f[now^][p][j]+d[k]);
}
ans=max(ans,f[now][j][k]);
}
}
}
printf("%d\n",ans);
return ;
}
poj1185
同样0/1区分特殊位置,同样预处理当前行的合法状态,同样用位运算判断合法
还是插头DP有意思哈哈,还很快哩
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const LL mod=; struct Plug_DP
{
LL f[];
int top;LL sta[],hash[];
void pha(LL s,LL sum)
{
LL x=s%mod;
while(hash[x]!=&&sta[hash[x]]!=s)x=(x+)%mod;
if(hash[x]==)sta[++top]=s,hash[x]=top;
f[hash[x]]+=sum;
}
void clean()
{
top=;
memset(hash,,sizeof(hash));
memset(f,,sizeof(f));
}
}dp[];
LL get_bracket(LL s,LL p)
{
return ((s>>(p-))&);
}
LL set_bracket(LL s,LL p,LL v)
{
s^=(get_bracket(s,p)<<(p-));
s^=(v<<(p-));
return s;
} int n,m;LL ans;
void Plug_DP()
{
int pre=,now=;
dp[now].clean();dp[now].pha(,);
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
swap(pre,now);dp[now].clean();
for(int k=;k<=dp[pre].top;k++)
{
LL s=dp[pre].sta[k],sum=dp[pre].f[k];
LL p=get_bracket(s,j),q=get_bracket(s,j+); if(i==n&&j==m)
{
if((p==&&q==)||(p==&&q==))ans+=sum;
continue;
} if(p==&&q==)
{
if(i!=n)
{
s=set_bracket(s,j,);
s=set_bracket(s,j+,);
dp[now].pha(s,sum);
}
if(j!=m)
{
s=set_bracket(s,j,);
s=set_bracket(s,j+,);
dp[now].pha(s,sum);
}
}
if((p==&&q==)||(p==&&q==))
{
s=set_bracket(s,j,);
s=set_bracket(s,j+,);
dp[now].pha(s,sum);
}
}
}
for(int k=;k<=dp[now].top;k++)dp[now].sta[k]<<=;
}
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==&&m==)break;
ans=;Plug_DP();
printf("%lld\n",ans);
}
return ;
}
poj2311(Plug_DP)
最新文章
- javascript(定时函数)
- 批量运行R包
- TextInfo
- 重构笔记---MEF框架(上)
- tyvj1213 嵌套矩形
- J2EE 第二阶段项目之编写代码(四)
- 简单实现兼容各大浏览器的js复制内容到剪切板
- 2012第二届GIS制图大赛——公开课技术问题&;答疑(珍贵资源哦!)(http://blog.csdn.net/arcgis_all/article/details/8216984)
- delphi xe5 android tts(Text To Speech)
- bnuoj 27987 Record of the Attack at the Orbit (模拟)
- [转]gcc -I -L -l区别
- C++对象模型--总结
- 一个奇怪的注意事项TNS-12545 TNS-12560 TNS-00515
- 588. [NOIP1999] 拦截导弹
- iOS-xcode代码统计
- 【Android Studio安装部署系列】十六、Android studio在layout目录下新建子目录
- day 7-21 pymysql模块
- ABP框架系列之五:(Unit Of Work-工作单元)
- lightswitch binding custom control
- ABPZero中的Name和SurName处理,以及EmailAddress解决方案(完美)。