【BZOJ5248】【九省联考2018】—一双木棋(轮廓线dp)

传送门

轮廓线dp入门题

考虑我们用 0 / 1 0/1 0/1串来表示一个已经选了的点和没选的点之间的轮廓

如果从左下到右上
比如向上为0,向右为1,那轮廓线就是一个01串

我们发现选了一个点实际上只是改变了轮廓方向的顺序

比如原来是 . . . 000111... ...000111... ...000111...
画图发现能填一个点当且仅当轮廓线为 01 01 01
而且填完之后为 10 10 10
可以通过和3进行位运算来判断

这样就可以妙妙的维护了

#include<bits/stdc++.h>
using namespace std;
inline int read(){
	char ch=getchar();
	int res=0,f=1;
	while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
	while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=getchar();
	return res*f;
}
const int N=12;
int a[N][N],b[N][N];
int n,m;
const int inf=1e9;
int f[1<<21];
int dfs(int sta,int who){
	if(f[sta]<=inf)return f[sta];
	int res=who?-inf:inf;
	int x=n+1,y=1;
	for(int i=1;i<=n+m-1;i++){
		if((sta>>(i-1))&1)x--;else y++;
		if(((sta>>(i-1))&3)!=1)continue;
		int nxt=sta^(3<<(i-1));
		if(who){
			res=max(res,dfs(nxt,who^1)+a[x][y]);
		}
		else res=min(res,dfs(nxt,who^1)-b[x][y]);
	}
	return f[sta]=res;
}
int main(){
	n=read(),m=read();
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			a[i][j]=read();
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			b[i][j]=read();
		}
	}
	memset(f,127,sizeof(f));
	f[((1<<n)-1)<<m]=0;
	cout<<dfs((1<<n)-1,1);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值