前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ZOJ1100 状压DP +深搜

ZOJ1100 状压DP +深搜

作者头像
全栈程序员站长
发布2022-07-12 17:38:19
1520
发布2022-07-12 17:38:19
举报

大家好,又见面了,我是全栈君,祝每个程序员都可以多学几门语言。

记得做过类似于这类题目是能够用组合数学方法来解决的,可惜淡忘了,也找不到了,看了网上的也有人提到过能够用组合公式解决,但是没人做,都是用了状压DP的方法,这个状压非常难讲清楚吧,推荐两篇

第一遍大体看看这个:http://blog.csdn.net/crux_d/article/details/2206736

想要详细实现的时候看看他的解析:http://blog.csdn.net/yan_____/article/details/8719748

代码语言:javascript
复制
#include<iostream>
#include<cstdio>
#include<list>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<cmath>
#include<memory.h>
#include<set>
#include<cctype>

#define ll long long

#define LL __int64

#define eps 1e-8

#define inf 0xfffffff

//const LL INF = 1LL<<61;

using namespace std;

//vector<pair<int,int> > G;
//typedef pair<int,int > P;
//vector<pair<int,int> > ::iterator iter;
//
//map<ll,int >mp;
//map<ll,int >::iterator p;


ll dp[12][10000];//第i行状态为j的放置方法数

int binary[15000][2];//[i][0]表示当前这一行的状态,[i][1]表示与[i][0]相匹配的下一行的状态,由于竖着放会影响下一行


int w,h,cnt;

void init() {
	cnt = 0;
	memset(dp,0,sizeof(dp));
}

void dfs(int n,int now,int nex) {
	if(n > w)return;//还有个n+2的,所以有可能直接大于w,而不会产生等于w
	if(n == w) {
		binary[cnt][0] = now;
		binary[cnt++][1] = nex;
		return;
	}
	dfs(n+2,(now<<2) + 3,(nex<<2) + 3);
	dfs(n+1,(now<<1) + 1,nex<<1);
	dfs(n+1,now<<1,(nex<<1) + 1);
}

int main() {
	while(scanf("%d %d",&w,&h),w + h) {
		init();
		dfs(0,0,0);
		dp[0][(1<<w) - 1] = 1;//边界要填满1
		for(int i=0;i<h;i++)
			for(int j=0;j<cnt;j++)
				dp[i+1][binary[j][1]] += dp[i][binary[j][0]];//下一行的与当前一行的相匹配,所以可递推
		printf("%lld\n",dp[h][(1<<w) - 1]);
	}
	return 0;
}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/118558.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021年12月,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档