第五章 习题(一)

5-1 子集和问题

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

const int NM=25;
int set[NM],x[NM],path[NM],n,Wleft,c;

bool Backtrack(int cw,int t)
{
	if(t>n){
		for(int j=1;j<=n;j++)
			path[j]=x[j];
		if(cw==c) return true;
		else return false;  //
	}
	
	Wleft-=set[t];
	if(cw+set[t]<=c){
		x[t]=1;
		if(Backtrack(cw+set[t],t+1)) return true;  //
	}
	if(cw+Wleft>=c){
		x[t]=0;
		if(Backtrack(cw,t+1)) return true;
	}
	Wleft+=set[t];
	return false;
}

int main()
{
	int i;
	while(~scanf("%d%d",&n,&c))
	{
		Wleft=0;
		for(i=1;i<=n;i++){
			scanf("%d",&set[i]);
			Wleft+=set[i];
		}
		
		memset(x,0,sizeof(x));
		if(Backtrack(0,1)){
			for(i=1;i<=n;i++){
				printf("%d ",path[i]);
			}
			printf("\n");
		}
		else printf("No solution.\n");
	}
	return 0;
}
/*
5 10
2 2 6 5 4
5 11
2 2 2 2 2
*/


5-3 最小重量机器设计问题

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

const int NM=25;
int c[NM][NM],w[NM][NM],x[NM],path[NM],bestw,n,m,d;
bool flag;

void Backtrack(int t,int cc,int ww)
{
	if(t>n){
		for(int j=0;j<n;j++)
			path[j]=x[j];
		bestw=ww;flag=true;
		return;
	}
	for(int i=0;i<m;i++)
	{
		if(cc+c[t][i]<=d){
			if(ww+w[t][i]<bestw){ 
				x[t]=i+1;
				Backtrack(t+1,cc+c[t][i],ww+w[t][i]);
			}
		}
	}
}

int main()
{
	int i,j;
	while(~scanf("%d%d%d",&n,&m,&d))
	{
		for(i=0;i<n;i++)
			for(j=0;j<m;j++)
				scanf("%d",&c[i][j]);
		for(i=0;i<n;i++)
			for(j=0;j<m;j++)
				scanf("%d",&w[i][j]);
		bestw=0xfffffff;flag=false;
		Backtrack(0,0,0);
		if(flag){
				printf("%d\n",bestw);
		for(i=0;i<n;i++)
			printf("%d ",path[i]);
		}
		else printf("No solution!");
		printf("\n");
	}
	return 0;
}
/*
3 4 4
1 2 3 4
3 2 1 4
2 2 2 1
1 2 3 2
3 2 1 2
2 2 2 1

3 4 4
1 2 3 4
3 2 3 4
2 2 2 1
1 2 3 2
3 2 1 2
2 1 2 2
*/



5-4 运动员最佳配对

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

const int NM=25;
int n,bestmax,cw,x[NM],p[NM][NM],q[NM][NM],path[NM];

void Backtrack(int t)
{
	int j;
	if(t>n){
		cw=0;
		for(j=1;j<=n;j++){
			cw+=p[j][x[j]]*q[j][x[j]];
		}
		if(cw>bestmax) {
			for(int j=1;j<=n;j++) 
				path[j]=x[j];
			bestmax=cw;
		}
		return;
	}
	for(j=t;j<=n;j++){
		swap(x[t],x[j]);
		Backtrack(t+1);
		swap(x[t],x[j]);
	}
}

int main()
{
	int i,j;
	while(~scanf("%d",&n))
	{
		for(i=1;i<=n;i++) 
			for(j=1;j<=n;j++)
				scanf("%d",&p[i][j]);
		for(i=1;i<=n;i++) 
			for(j=1;j<=n;j++)
				scanf("%d",&q[i][j]);
		for(i=1;i<=n;i++) x[i]=i;
		
		bestmax=0;
		Backtrack(1);
		printf("%d\n",bestmax);
		for(i=1;i<=n;i++) cout<<path[i]<<" ";
		cout<<endl;
	}
	return 0;
}
/*
3
10 2 3
2 3 4
3 4 5
2 2 2
3 5 3
4 5 1

*/



5-8 整数变换问题

注:3n+1问题,但不可证明是否一定无解,故只能加入一个步数判断:在一定步数内无方案即认为无解

1)BFS的渣实现,保存路径的方法太烂了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

const int NM=105;
int n,m;
bool flag;
struct Node{
	int step,vau,path[NM];
}node;

void BFS()
{
	queue<Node>q1;
	Node t,pt;
	t.step=0;t.vau=n;
	q1.push(t);
	while(!q1.empty())
	{
		t=q1.front();q1.pop();
		if(t.step>100) {
			flag=false;break;
		}
		if(t.vau==m) {
			node=t;break;
		}
		pt=t;
		pt.vau=t.vau*3;pt.path[t.step]=1;pt.step=t.step+1;
		q1.push(pt);
		pt=t;
		pt.vau=t.vau/2;pt.path[t.step]=2;pt.step=t.step+1;
		q1.push(pt);
	}
}

int main()
{
	int i;
	while(cin>>n>>m)
	{
		if(n==0){
			cout<<"No solution!"<<endl;continue;
		}
		flag=true;
		BFS();
		if(!flag){
			cout<<"No solution!"<<endl;continue;
		} 
		cout<<node.step<<endl;
		for(i=node.step-1;i>=0;i--){
			if(node.path[i]==1) cout<<'f';
			else if(node.path[i]==2) cout<<'g';
		}
		cout<<endl;
	}
	return 0;
}

2)DFS

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

const int NM=105;
int maxdep,k,n,m,t[NM];
bool found;

int fun(int n,int i)
{
	if(i==0) n*=3;
	else n/=2;
	return n;
}

void output()
{
	cout<<maxdep<<endl;
	for(int i=maxdep;i>0;i--){
		if(t[i]==0) cout<<'f';
		else if(t[i]==1) cout<<'g';
	}
	cout<<endl;
}

bool search(int dep,int n)
{
	if(dep>k) return false;
	for(int i=0;i<2;i++){
		int n1=fun(n,i);t[dep]=i;
		if(n1==m || search(dep+1,n1)){
			found=true;return true;  //out();
		}
	}
	return false;
}

void init(){
	memset(t,0,sizeof(t));
}

void compute()
{
	k=1;
	maxdep=100;  //
	while(!search(1,n)){
		k++;
		if(k>maxdep) break;
		init();
	}
	maxdep=k;
	if(found) output();
	else cout<<"No solution!"<<endl;
}

int main()
{
	while(cin>>n>>m)
	{
		if(n==0){
			cout<<"No solution!"<<endl;continue;
		}
		found=true;
		compute();
	}
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值