Door Frames

Door Frames题意:

4a 2b 门框,木板做门框,木板可以分段,门框不能拼接,问最少需要多少木板

深搜 DFS 代码借鉴别人的,自己写的解释

输入:木板长度x 左右门框高度a 上门框b

输出:最少使用的木板

#include<iostream> 
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
 
using namespace std;

//x木板长度   
int x,a,b,ans=10;//是用最少的木板,4a2b用6  初始化为10 
int len[10][10],vis[10][10][10];//vis[aa][bb][use]左右 上下 根数 

void dfs(int aa,int bb,int use){
	if(vis[aa][bb][use]||use>6)return ;//边界use 使用的木板不能超过6   访问过的不再访问 
	vis[aa][bb][use]=1;//标记 
	//	dfs(aa-i,bb-j,use+1)  aa+bb==0全都上了门框 
	if(aa+bb==0){
		ans=min(ans,use);
		return;
	}
	//i j表示 所有情况遍历一遍 
	for(int i=0;i<=aa;i++){
		for(int j=0;j<=bb;j++){
			if(x<len[i][j])continue;//控制这种情况的长度要小于木板 
			dfs(aa-i,bb-j,use+1);
		}
	}
}

using namespace std;
int main()
{
	scanf("%d%d%d",&x,&a,&b);
	//存入各种情况的值 
	for(int i=0;i<=4;i++){
		for(int j=0;j<=2;j++){
			len[i][j]=a*i+b*j;
		}
	}
	dfs(4,2,0);
	printf("%d",ans);
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/dujuancao11/article/details/79994362
今日推荐