算法
//到了考试周了,整理了老师说的重点算法,作为自己的复习资料,木得脑子,我记不住啊啊啊啊啊啊
三位四进制数
#include “stdafx.h”
#include
int data[5];
void f(int n)
{
int k;
if(n==3)
{
for(k=0;k
printf(”\n");
}
else
{
data[n]=0; f(n+1);
data[n]=1; f(n+1);
data[n]=2; f(n+1);
data[n]=3; f(n+1);
}
}
int main()
{
f(0);
return 0;
}
0-1背包问题
class Object
{ …
public:
int operator <= (Object a) const
{
return d>=a.d;
}
private:
int ID;
float d;//单位重量价值
};
template
class bbnode
{ …
bbnode * parent; //指向父节点的指针
bool LChild; //左儿子节点标识
};
template
class HeapNode
{ …
Typep uprofit, //节点的价值上界,优先队列依据uprofit排序
profit; //节点所相应的价值
Typew weight; //节点所相应的重量
int level; //活节点在子集树中所处的层序号
bbnode *ptr; //指向活节点在子集中相应节点的指针
};
template
class Knap
{ …
bbnode *E; //指向扩展节点的指针
Typew c; //背包容量
int n; //物品总数
Typew *w; //物品重量数组
Typep *p; //物品价值数组
Typew cw; //当前重量
Typep cp; //当前价值
int *bestx; //最优解
};
template
Typep Knap
{
Typew cleft = c-cw; //剩余容量高
Typep b = cp; //价值上界
//以物品单位重量价值递减序装填剩余容量
while(i<=n && w[i]<=cleft)
{
cleft -= w[i];
b += p[i];
i++;
}
//装填剩余容量装满背包
if(i<=n)
{
b += p[i]/w[i]*cleft;
}
return b;
}
//将一个活节点插入到子集树和优先队列中
template
void Knap
{
bbnode *b = new bbnode;
b->parent = E;
b->LChild = ch;
HeapNode N;
N.uprofit = up;
N.profit = cp;
N.weight = cw;
N.level = lev;
N.ptr = b;
H->Insert(N);
}
//优先队列式分支限界法,返回最大价值,bestx返回最优解
template
Typep Knap
{
H = new MaxHeap
//为bestx分配存储空间
bestx = new int[n+1];
//初始化
int i = 1;
E = 0;
cw = cp = 0;
Typep bestp = 0;//当前最优值
Typep up = Bound(1); //价值上界
//搜索子集空间树
while(i!=n+1)
{
//检查当前扩展节点的左儿子节点
Typew wt = cw + w[i];
if(wt <= c)//左儿子节点为可行节点
{
if(cp+p[i]>bestp)
{
bestp = cp + p[i];
}
AddLiveNode(up,cp+p[i],cw+w[i],true,i+1);
}
up = Bound(i+1);
//检查当前扩展节点的右儿子节点
if(up>=bestp)//右子树可能含有最优解
{
AddLiveNode(up,cp,cw,false,i+1);
}
//取下一扩展节点
HeapNode N;
H->DeleteMax(N);
E = N.ptr;
cw = N.weight;
cp = N.profit;
up = N.uprofit;
i = N.level;
}
//构造当前最优解
for(int j=n; j>0; j--)
{
bestx[j] = E->LChild;
E = E->parent;
}
return cp;
}
//返回最大价值,bestx返回最优解
template
Typep Knapsack(Typep p[],Typew w[],Typew c,int n, int bestx[])
{
//初始化
Typew W = 0; //装包物品重量
Typep P = 0; //装包物品价值
//定义依单位重量价值排序的物品数组
Object *Q = new Object[n];
for(int i=1; i<=n; i++)
{
//单位重量价值数组
Q[i-1].ID = i;
Q[i-1].d = 1.0*p[i]/w[i];
P += p[i];
W += w[i];
}
if(W<=c)
{
return P;//所有物品装包
}
//依单位价值重量排序
BubbleSort(Q,n);
//创建类Knap的数据成员
Knap K;
K.p = new Typep[n+1];
K.w = new Typew[n+1];
for(int i=1; i<=n; i++)
{
K.p[i] = p[Q[i-1].ID];
K.w[i] = w[Q[i-1].ID];
}
K.cp = 0;
K.cw = 0;
K.c = c;
K.n = n;
//调用MaxKnapsack求问题的最优解
Typep bestp = K.MaxKnapsack();
for(int j=1; j<=n; j++)
{
bestx[Q[j-1].ID] = K.bestx[j];
}
delete Q;
delete []K.w;
delete []K.p;
delete []K.bestx;
return bestp;
}
全排列问题
void perm(int data[], int k, int n)
{ int i;
if(k==n-1)
{ for(i=0;i
printf(“ ");
}
else
{ for(i=k;i
perm(data,k+1,n);
swap(data[i],data[k]);
}
}
}
void swap(int &x, int &y)
{
int temp;
temp=x; x=y; y=temp;
}
void main()
{
int i;
int data[20];
for(i=0;i<20;i++)
data[i]=i+1;
perm(data,0,4);
}
最长公共子序列
int LookupChain(int i, int j)
{
if (i == j) return 0;
int u = LookupChain(i,i) + LookupChain(i+1,j) + p[i-1]*p[i]*p[j];
s[i][j] = i;
for (int k = i+1; k < j; k++) {
int t = LookupChain(i,k) + LookupChain(k+1,j) + p[i-1]*p[k]*p[j];
if (t < u) { u = t; s[i][j] = k;}
}
return u;
}
n皇后问题
#include
#include
int x[20],n,count;
void Backtrack(int t)
{ int i;
if(t>n)
{ for(i=1;i<=n;i++) printf("%d “,x[i]);
printf(”\n");
count++;
}
else
for(i=1;i<=n;i++)
{ x[t]=i;
if(Place(t)) Backtrack(t+1);
}
}
int Place(int k)
{ for(int j=1;j
||(x[j]==x[k]))
return 0;
return 1;
}
int main()
{ printf(“请输入n:”);
scanf("%d",&n);
Backtrack(1);
printf(“共%d组答案\n”,count);
return 0;
}