算法设计
文章平均质量分 67
hbjiaxiaoxue
这个作者很懒,什么都没留下…
展开
-
棋盘问题
#includeusing namespace std;//特殊方格的值赋为0,三个一样的数字表示一个骨牌//tr:棋盘左上角方格的行号//tc:棋盘左上角方格的列号//dr:特殊方格的行号//dc:特殊方格的列号int tile = 1;int ** Bord;//int Bord[8][8]={0};void ChessBord(int tr,i原创 2011-12-05 21:45:51 · 346 阅读 · 0 评论 -
动态规划解决流水作业调度(Johnson法则)
#include#includeusing namespace std;#include#include#define random(x) (rand()%x)typedef struct { int key; //以Ai还是Bi作为关键字排序 bool job;//标志是Ai>Bi还是Ai<Bi型,从而分别归在N1或N2集合中 int index;//将它作为第index个作原创 2011-12-15 20:55:34 · 3236 阅读 · 0 评论 -
递归分治解决快速排序和限行时间选择
#includeusing namespace std;#include#include#define Random(x) (rand()%x)templatevoid Swap(Type &m,Type &n){//交换两个数 Type temp = m; m = n; n = temp;}templateint Partition(Type a[],int p,in原创 2011-12-14 21:19:20 · 740 阅读 · 0 评论 -
动态规划解决最大字段和
#includeusing namespace std;int MaxSum(int n,int *a,int &besti,int &bestj){ int sum = 0,starthere,b = 0; besti = bestj = 1; //b存放临时的和besti和bestj分别保存最佳开始和结束位置,starthere临时存放起始位置 for(int i = 1;i <原创 2011-12-14 20:20:52 · 591 阅读 · 0 评论 -
动态规划解决0-1背包问题
#includeusing namespace std;#includetemplate Type min(Type a,Type b){ return a > b ? b:a;}template Type max(Type a,Type b){ return a < b ? b:a;}template void Knapsack(Type *v,int *w,int原创 2011-12-13 22:06:43 · 429 阅读 · 0 评论 -
哈夫曼编码
#includeusing namespace std;#include#include #define MAX_CODE 10class HNode{public: char c;//节点的字符 int weight;//节点的权重,即为出现的频率 int lchild;//节点的左孩子的编号 int rchild;//节点的右孩子的编号 char code[MAX_C原创 2011-12-13 21:24:17 · 482 阅读 · 0 评论 -
动态规划解决最长公共子序列
#include#include#includeusing namespace std;int c[10][10];int b[10][10];void LCSLength(int m,int n,char *x,char *y,int c[10][10],int b[10][10]){ int i,j; for(i = 0;i <= m;i++) c[i][0] = 0原创 2011-12-12 21:10:36 · 384 阅读 · 0 评论 -
动态规划解决矩阵连乘
#includeusing namespace std;#include/*p是一维数组,记录矩阵的规模(第一个矩阵的行和其余矩阵的列);n是矩阵的个;m是二维数组,记录计算A[i:j],1<=i<=j<=n所需要的最少数乘次数m[i][j];相对应于m[i][j]的断开位置k记为s[i][j]*/void MatrixChain(int *p,int n,int **m,int原创 2011-12-12 19:16:39 · 344 阅读 · 0 评论 -
快速排序
#includeusing namespace std;templatevoid Swap(Type &x,Type &y){ Type temp = x; x = y; y = temp;}templateint Partition(Type a[],int p,int r){ int i = p,j = r + 1; Type x = a[p]; while(tru原创 2011-12-08 22:10:01 · 300 阅读 · 0 评论 -
一维最接近点对(递归分治)
//一维情况下的最接近点对#includeusing namespace std;#include#include#include#include#define maxint 1000templatevoid Swap(Type &a,Type &b){ Type temp = a; a = b; b = temp;}/*templateType min(Typ原创 2011-12-17 21:45:25 · 1029 阅读 · 0 评论 -
非递归合并排序
#includeusing namespace std;#includeint b[5];templatevoid Merge(Type c[],Type d[],int l,int m,int r){ int i = l,j = m+1,k = l; while( (i <= m) && (j <= r) ) { if(c[i] <= c[j]) d[k++] =原创 2011-12-08 20:15:23 · 337 阅读 · 0 评论 -
递归合并排序
#includeusing namespace std;#includeint b[5];templatevoid Merge(Type c[],Type d[],int l,int m,int r){ int i = l,j = m+1,k = l; while( (i <= m) && (j <= r) ) { if(c[i] <= c[j]) d[k++] =原创 2011-12-08 20:02:12 · 378 阅读 · 0 评论 -
递归分治解决全排列问题
#includeusing namespace std;templateinline void Swap(Type &a,Type &b){ Type temp = a; a = b; b = temp;}templatevoid Perm(Type list[],int k,int m){ if(k == m) { for(int i = 0;i<=m;i++)原创 2011-12-08 16:16:10 · 431 阅读 · 0 评论 -
流水作业调度
#include#includeusing namespace std;#include#include#define random(x) (rand()%x)typedef struct { int key; //以Ai还是Bi作为关键字排序 bool job;//标志是Ai>Bi还是Ai int index;//将它作为第index个作原创 2011-12-05 21:47:27 · 650 阅读 · 0 评论 -
回溯法解决装载问题
#include#includeusing namespace std;typedef int* pointer; templateclass Loding{ friend Type MaxLoding(Type [],Type,int,int[]); private: void Backtrack(int i);原创 2011-12-05 21:43:57 · 1191 阅读 · 1 评论 -
0-1背包 动态规划
#include#includeusing namespace std;#includetypedef struct{ int index;//物品的编号 int w;//物品的重量 int v;//物品的价值}Goods;int min(int a,int b){ return a}int max(int原创 2011-12-05 21:48:45 · 356 阅读 · 0 评论 -
单源最短路径
#include#includeusing namespace std;#define maxint 1000typedef int* pointer;void Dijkstra(int n,int v,int *dist,int *prev,int **c){ bool *s = new bool[n+1];//顶点i是否属于集合s for(int原创 2011-12-05 21:46:38 · 405 阅读 · 0 评论 -
动态规划解决装载问题
#include#includetypedef int* pointer;int min(int a,int b){ return a > b? b:a; }int max(int a,int b){ return a > b? a:b; }void LodingC1(int C1,int* w,int n,int原创 2011-12-05 21:44:58 · 1803 阅读 · 2 评论 -
单源最短路径(贪心算法)
#includeusing namespace std;#include#define maxint 999typedef int* pointer;templatevoid Dijkstra(int n,int v,Type *dist,int *prev,Type **c){//v是源点,dist记录源点到每一个顶点的最短距离,prev记录前一个顶点//c是边距离矩阵 b原创 2011-12-16 20:23:29 · 760 阅读 · 0 评论