ImageVerifierCode 换一换
格式:DOCX , 页数:24 ,大小:21.50KB ,
资源ID:8021249      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/8021249.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(计算机算法分析与设计第四版习题算法分析部分详解实验六.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

计算机算法分析与设计第四版习题算法分析部分详解实验六.docx

1、计算机算法分析与设计第四版习题算法分析部分详解实验六实验六 分支限界法/6-1、6-6项目VC+6.0测试通过/6-15项目VC2005测试通过 /6-1 最小长度电路板排列问题/头文件stdafx.h/ stdafx.h : include file for standard system include files,/ or project specific include files that are used frequently, but/ are changed infrequently/#pragma once#define WIN32_LEAN_AND_MEAN / Exclu

2、de rarely-used stuff from Windows headers#include #include / TODO: reference additional headers your program requires here/ sy61.cpp : Defines the entry point for the console application./ /Description: /分支限界法 6_1 最小长度电路板排列问题 /#include my.h#include stdafx.h#include #include using namespace std; int

3、n,m;/#include OutOfBounds.h/定义节点类class BoardNode friend int FIFOBoards(int *,int ,int,int *&);/非类成员,可以访问私有成员的函数,最优序列查找 public: operator int() constreturn cd;/返回常数 cd int len(); public: int *x,s,cd,*low,*high;/x表示当前节点的电路板排列,s表示当前节点排列好的电路板的数/表示当前节点的最大长度,low,high分别表当前节点表示每一个连接块的第一个,和最后一个电路板/的位置;/编写类的le

4、n()函数,求出当前节点的连接块长度的最大值int BoardNode:len() int tmp=0; for(int k=1;k=m;k+) if(lowk0 & tmphighk-lowk) tmp=highk-lowk; return tmp;int FIFIOBoards(int *B,int n,int m,int *&bestx)/n为电路板的数量,m为连接块的数量/ int bestd; queue q;/声明BoardNode类的节点队列q BoardNode E; E.x=new intn+1;/为数组指针x分配n+1个动态空间,存储当前的排列 E.s=0;/最初时,排列好

5、的电路板的数目为0 E.cd=0; E.low=new intm+1;/存储每个连接块的第一个电路板的位置 E.high=new intm+1;/存储每个连接块的最后一个电路板的位置 for(int i=1;i=m;i+) E.highi=0;/初始化开始时的每个连接块的最后一个电路板的位置为0,表示连接块i还没有电路板放入E.x的排列中 E.lowi=n+1;/初始化开始时的每个连接块的第一个电路板的位置为n+1,表示连接块i还没有电路板放入E.x的排列中 for(i=1;i=n;i+) E.xi=i;/初始化E.x的排列为1,2,3.n int bestd=n+1;/最优距离 bestx=

6、0;/记录当前最优排列 do if(E.s=n-1)/当已排列了n-1个时 /判断是否改变每个连接块的最后一个电路板的位置 for(int j=1;jE.highj) E.highj=n; int ld=E.len();/存储当前节点的各连接块长度中的最大长度 /如果当前的最大长度小于了n+1 if(ldbestd) delete bestx; bestx=E.x; bestd=ld;/最优距离 else delete E.x;/删除分配给E.x的数组空间 delete E.low;/删除分配给E.low的数组空间 delete E.high;/删除分配给E.high的数组空间 else in

7、t curr=E.s+1;/rr记录现在应该排列第几个电路板 for(int i=E.s+1;i=n;i+)/处理扩展节点下一层所有子节点 BoardNode N; N.low=new intm+1;/与if中的意思相同 N.high=new intm+1; for(int j=1;j=m;j+) N.lowj=E.lowj;/将E.low中的值赋给N.low N.highj=E.highj; if(BE.xij) if(currN.highj) N.highj=curr; N.cd=N.len(); /如果,当前节点的最大长度小于了最优长度则: if(N.cdbestd) N.x=new i

8、ntn+1; N.s=E.s+1; for(int j=1;jnm; int *B=new int*n+1; for (int t=0; t=n; t+) Bt = new intm+1; for(int i=1;i=n;i+) for(int j=1;jBij; /scanf(%d,&Bij); int *bestx=new intn+1; int bestd=0; bestd=FIFIOBoards(B,n,m,bestx); printf(%dn,bestd); for(i=1;i=n;i+) coutbestxi ; coutendl;/6-6 经典n皇后问题/Description:

9、经典n皇后问题 广度优先 建议n=14 解空间为子集树/参考答案说排列树是不正确的,本例打印n*n棋盘的所有解,即放置方法#include #include #include #include #include using namespace std; /本例子直接输入棋盘大小,不用文件/ifstream in(input.txt); /请在项目文件夹下新建一个input.txt/ofstream out(output.txt); class Node public: Node(int n) t = 0; this-n = n; loc = new intn + 1; for (int i =

10、 0; it = other.t; this-n = other.n; this-loc = new int n + 1; for (int i = 0; i loci = other.loci; int t;/已放置t个皇后 int *loc;/loc1:t int n;/共放置n个皇后 bool checkNext(int next); void printQ(); ; bool Node:checkNext(int next) int i; for (i = 1; i = t; +i) if (loci = next)/检测同列 return false; if (loci - next

11、 = t + 1 - i)/检测反斜线 行差=列差 return false; if (loci - next = i - t - 1)/检测正斜线 return false; return true; void Node:printQ() for (int i = 1; i = n; +i) coutloci ; coutn = n; ansNum = 0; void ArrangQueen(); ; void Queen:ArrangQueen() queue Q; Node ini(n); /初始化 Q.push(ini); while(!Q.empty() Node father =

12、Q.front(); /取队列下一个节点 Q.pop(); if (father.t = n) father.printQ(); +ansNum; for (int i = 1; i = n; +i) /一次性将当前扩展节点所有子节点考虑完,符合条件的插入队列 if (father.checkNext(i) Node Child(father); +Child.t; Child.locChild.t = i; Q.push(Child); int main() /#define in cin /#define out cout coutn; /从文件中读入一个整数 /for(int Case

13、= 1; Case n; Queen Q(n); Q.ArrangQueen(); /outCase #Case: Q.ansNumendl; cout共Q.ansNum种放置皇后方法,如上所示。endl; / return 0; /6-15 排列空间树问题 VC2005测试通过/ stdafx.h头文件/ stdafx.h : include file for standard system include files,/ or project specific include files that are used frequently, but/ are changed infreque

14、ntly/#pragma once#define WIN32_LEAN_AND_MEAN / Exclude rarely-used stuff from Windows headers#include #include / TODO: reference additional headers your program requires here/头文件MinHeap2.h;最小堆实现#include template class Graph; template class MinHeap template friend class Graph; public: MinHeap(int max

15、heapsize = 10); MinHeap()delete heap; int Size() constreturn currentsize; T Max()if(currentsize) return heap1; MinHeap& Insert(const T& x); MinHeap& DeleteMin(T &x); void Initialize(T x, int size, int ArraySize); void Deactivate(); void output(T a,int n); private: int currentsize, maxsize; T *heap;

16、; template void MinHeap:output(T a,int n) for(int i = 1; i = n; i+) cout ai ; cout endl; template MinHeap:MinHeap(int maxheapsize) maxsize = maxheapsize; heap = new Tmaxsize + 1; currentsize = 0; template MinHeap& MinHeap:Insert(const T& x) if(currentsize = maxsize) return *this; int i = +currentsiz

17、e; while(i != 1 & x heapi/2) heapi = heapi/2; i /= 2; heapi = x; return *this; template MinHeap& MinHeap:DeleteMin(T& x) if(currentsize = 0) coutEmpty heap!endl; return *this; x = heap1; T y = heapcurrentsize-; int i = 1, ci = 2; while(ci = currentsize) if(ci heapci + 1) ci+; if(y = heapci) break; h

18、eapi = heapci; i = ci; ci *= 2; heapi = y; return *this; template void MinHeap:Initialize(T x, int size, int ArraySize) delete heap; heap = x; currentsize = size; maxsize = ArraySize; for(int i = currentsize / 2; i = 1; i-) T y = heapi; int c = 2 * i; while(c = currentsize) if(c heapc + 1) c+; if(y

19、= heapc) break; heapc / 2 = heapc; c *= 2; heapc / 2 = y; template void MinHeap:Deactivate() heap = 0; /批作业调度问题 优先队列分支限界法求解 /算法编码与教材一致#include stdafx.h #include MinHeap2.h #include using namespace std; class Flowshop; class MinHeapNode friend Flowshop; public: operator int() const return bb; private

20、: void Init(int); void NewNode(MinHeapNode,int,int,int,int); int s, /已安排作业数 f1, /机器1上最后完成时间 f2, /机器2上最后完成时间 sf2, /当前机器2上完成时间和 bb, /当前完成时间和下界 *x; /当前作业调度 ; class Flowshop friend int main(void); public: int BBFlow(void); private: int Bound(MinHeapNode E,int &f1,int &f2,bool *y); void Sort(void); int n

21、, /作业数 * M, /各作业所需的处理时间数组 *b, /各作业所需的处理时间排序数组 *a, /数组M和b的对应关系数组 *bestx, /最优解 bestc; /最小完成时间和 bool *y; /工作数组 ; template inline void Swap(Type &a, Type &b); int main() int n=3,bf; int M132=2,1,3,1,2,3; int *M = new int*n; int *b = new int*n; int *a = new int*n; bool *y = new bool*n; int *bestx = new i

22、ntn; for(int i=0;i=n;i+) Mi = new int2; bi = new int2; ai = new int2; yi = new bool2; cout各作业所需要的时间处理数组M(i,j)值如下:endl; for(int i=0;in;i+) for(int j=0;j2;j+) Mij=M1ij; for(int i=0;in;i+) cout(; for(int j=0;j2;j+) coutMij ; cout); coutendl; Flowshop flow; flow.n = n; flow.M = M; flow.b = b; flow.a = a; flow.y = y; flow.bestx = bestx; flow.bestc = 1000;/给初值 flow.BBFlow(); cout最优值是:flow.bestcendl;

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1