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

加入VIP,免费下载
 

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

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

下载须知

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

版权提示 | 免责声明

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

四川大学算法设计.docx

1、四川大学算法设计算法设计课程报告课序号: 学 号: 104304XXXX 姓 名: XXX 任课教师: 陈瑜 评阅成绩: 评阅意见: 提交报告时间:2012年 6 月 4日一、 动态规划1、 问题描述 下图是个数字三角,请编写一个程序计算从顶部至底部某处一条路径,使得该路径所经过的数字总和最大。73 88 1 02 7 4 41 每一步可沿左斜线向下或右斜线向下走;2 1=三角形行数=1003 三角形中的数字为整数 0,1,99。4 如果有多种情况结果都最大,任意输出一种即可。输入:第一行一个整数N,代表三角形的行数。接下来N行,描述了一个数字三角。输出: 第一行一个整数,代表路径所经过底数字

2、总和。 第二行N个数,代表所经过的数字。2、 算法分析很裸的DP,一个点的最优选择只能从下面和斜下面产生,所可以从最下的一层反面推到,并且中间用二维数组进行0或者1标记表示选择的是那个,以便查询路径。动态啊规划转移方程:dpi-1j=max(dpij,dpij+1);3、 核心代码#include#include#includeusing namespace std;int num100100;int mid100100;int mm100100;int n;void slove() int i,j; memset(mid,0,sizeof(mid); for(i=n-1;i=0;i-) fo

3、r(j=0;ji;j+) if(numijnumij+1) midi-1j=j+1; numi-1j+=numij+1; else midi-1j=j; numi-1j+=numij; coutnum00endl; j=0; for(i=0;in-1;i+) coutmmij ; j=midij; coutnumijendl;int main() int i; int j; while(scanf(%d,&n)=1) memset(num,0,sizeof(num); for(i=0;in;i+) for(j=0;j=i;j+) scanf(%d,numi+j);mmij=numij; slo

4、ve(); 4、 测试数据5、 算法复杂度分析时间复杂度O(n)=n+(n-1)+1=n*(n-1)/2; 即时间复杂度为O(n2)空间复杂度为 T(n)=3*(n2);6、 和其他算法比较很裸的一个动态题目,效率一般,还可以在空间上加以优化,即储存路径的空间可以优化一下,省出一定的空间。二、 二分搜索1. 问题描述1.1问题概述Lumberjack Mirko needs to chop down M metres of wood. It is an easy job for him since he has a nifty new woodcutting machine that can

5、take down forests like wildfire. However, Mirko is only allowed to cut a single row of trees.Mirko?s machine works as follows: Mirko sets a height parameter H (in metres), and the machine raises a giant sawblade to that height and cuts off all tree parts higher than H (of course, trees not higher th

6、an H meters remain intact). Mirko then takes the parts that were cut off. For example, if the tree row contains trees with heights of 20, 15, 10, and 17 metres, and Mirko raises his sawblade to 15 metres, the remaining tree heights after cutting will be 15, 15, 10, and 15 metres, respectively, while

7、 Mirko will take 5 metres off the first tree and 2 metres off the fourth tree (7 metres of wood in total).Mirko is ecologically minded, so he doesn?t want to cut off more wood than necessary. That?s why he wants to set his sawblade as high as possible. Help Mirko find the maximum integer height of t

8、he sawblade that still allows him to cut off at least M metres of wood. 1.2输入The first line of input contains two space-separated positive integers, N (the number of trees, 1 N 1 000 000) and M (Mirko?s required wood amount, 1 M 2 000 000 000).The second line of input contains N space-separated posi

9、tive integers less than 1 000 000 000, the heights of each tree (in metres). The sum of all heights will exceed M, thus Mirko will always be able to obtain the required amount of wood. 1.3输出 The first and only line of output must contain the required height setting. 1.4 事例Sample Input4 7 20 15 10 17

10、5 204 42 40 26 46Sample Output15362. 算法分析对于砍树,一定从在一个唯一的最优解,所以首先针对于树的高度做一个从小到大的排序,针对于最后的结果M肯定有(i=0,j=n;min(tessij)=MgetSumTree(M)getSumTree(j)(iMj),所以通过mid=(i+j)/2二分不停的缩小区间直到找到具体的值。3. 核心代码#include#include#include#include#include#include#include#include#include#define in(x) scanf(%d,&x);#define out(x)

11、 printf(%dn,x);using namespace std;const int N=1000001;int treesN;int n,m;inline bool cmp(const int &a,const int &b)return ab;inline long long getSum(int mid);int main() int l,r; int mid; long long sum; while(scanf(%d%d,&n,&m)!=EOF) l=r=0; for(int i=0;in;i+) in(treesi); r=max(r,treesi); sort(trees,t

12、rees+n,cmp); while(l1; sum=getSum(mid); if(sum(long long)m) r=mid-1; else if(sum=(long long)m)break; else l=mid+1; out(r+l)/2); inline long long getSum(int mid) long long sum=0; for(int i=0;imid) sum+=treesi-mid; else break; return sum; 4. 测试数据5. 算法复杂度分析时间复杂度O(n*logn)=n+(n-1);空间复杂度为 T(n)=(n);6. 和其他算

13、法比较标准的二分,时间效率一般,还可以通过其他算法进行时间与优化,但思路简单,容易使用。三、 贪心算法1. 问题描述1.1问题概述One day,Tpkey has nothing to do when he glances at some kids playing games on grid area.Saddenly he gets an idea , inventing a new maze game.First he draws a big grid area consisting of n*n small grid. The grid area has a compeletly ce

14、nter.The player will stand at the center,then he or she was given a hand of directional order,indicading that the player should jump to the neighbouring grid successively in this ordered direction.But some of this order is replaced by ?,so that the player can jump at any direction.The winer will be

15、the one who is the closest to the grid area boundary in the process of jumping.Then a kid is playing the game,but he stop,then he give a thought to guarantee he will be the winer.He feel so confused,but he know you are a good coder.He needs your help1.2输入The first of input is an integer t (0 t 50) w

16、hich stands for the number of test cases.for each case will include two lnes, a number (int) and a string (length1000) ,the numbers means is that Tpkey will draw a square area which has n*n small square.and the string will include five letters E W S N ? which stands for EAST WEST SOUTH NORTH and Und

17、etermined and the string length at most 1000.1.3输出for each case ,the output should contain a single real number,on a single line:the minimum distance from the square border when he jumped by the instructions1.4样例15?215EE?WWW429WS?W?S?W?SES62. 算法分析很裸的贪心问题,遇到?号的时候,对应的四个方向都可以选择,所以在前一次的基础之上都进行加1;最终结果所有的

18、“问号”肯定为四种方向中的一种方向(对于最优宁愿同向也不怨反向或者不影响),所以可得,边算边保存中间最大,则就为题目离中心最远的距离,即为最优解。3. 核心代码#include#include#include#include#include#define in(x) scanf(%d,&x);#define out(x) printf(%dn,x);using namespace std;const int N=1005;char strN;int main() int nn; int t; scanf(%d,&t); for(int tt=0;ttt;tt+) scanf(%dn,&nn);

19、 scanf(%s,str); int sum=0; int e(0); int s(0); int w(0); int n(0); nn/=2; for(int i=0;inn) sum=nn;break; printf(Case #%d :%dn,tt+1,nn-sum); 4. 测试数据5. 算法复杂度分析时间复杂度o(n)的算法无法优化。空间复杂度为T(n);可以进行优化达到T(1)的算法。使用getchar来进行输入。6. 和其他算法比较很裸的一个贪心,算是最高效的算法,在空间上可以优化一些,做到无长数组的变量。四、 回溯法1. 问题描述在一个N*N的棋盘上放置N个皇后,且使得每两个

20、之间不能互相攻击,也就是使得每两个不在同一行,同一列和同一斜角线上。2. 算法分析对于N1,问题的解很简单,而且我们很容易看出对于N2和N3来说,这个问题是无解的。所让我们考虑4皇后问题并用回溯法对它求解。因为每个皇后都必须分别占据行,我们需要做的不过是为棋盘上的每个皇后分配一列。 我们从空棋盘开始,然后把皇后1放到它所在行的第一个可能位置上,也就是第一行第列。对于皇后2,在经过第一列和第二列的失败尝试之后,我们把它放在第一个可能的位置,就是格子2,3),位于第二行第二列的格子。这被证明是一个死胡同,因为皇后:将没有位置可放。所以,该算法进行回溯,把皇后2放在下一个可能位置(2,4)上。然后皇

21、后3就可以放在(3,2),这被证明是另一个死胡同。该算法然后就回溯到底,把皇后1移到(1,2)。接着皇后2到(2,4),皇后3到(3,1),而皇后4到(4,3),这就是该问题的一个解。 通过以此类推的关系得出相应输入N的结果。3. 核心代码#include #include #include#includeusing namespace std;#define N 4int colN+1;/输出结果void Output() for(int i=1;in) Output(); else for(int j=1;j=n;+j) int k=1; coli=j; while(ki) if(colk

22、-coli)*(fabs(colk-coli)-fabs(k-i)!=0) k+; if(k=i) Queen(i+1,n); else break; int main() printf(the answer is:n); for(int i=1;i=1 & t=10) 为测试数据的总数,接下来是 t 组测试数据, 每组测试数据均由 6 行,每行 3 个数字组成,前 3 行为初始状态,后 3 行为目标状态,空格由 0 表示,每组测试数据前面有一个空行。 输出格式:给出可以使初始状态转换到目标状态的最小移动次数,如果转化不到打印出-1。 输入样例: 22 8 31 6 47 0 52 3 01

23、8 47 6 52 8 31 6 47 0 52 8 31 4 67 0 5输出样例: 3-12、 算法分析很裸的一个A*搜索,中间加启发函数进行限定使得效率有大大的提高.首先声明一个一个结构体node来储存程序处理中间的状态八宫格的状态以及处理到这一步的值等。其中num表示的将八宫格状态转换成一个整数,a怎保存的是起每个格子的值,zero表示空格的位置,ti表示 已经走的步数,per估计值的结果,表示到目标点逆序数之和,h表示已走步数与估计值的和,代表这启发函数,并用来作为排序限定的作用,避免无效的搜索。本题主要用的基础算法是BFS作为搜索,将每一次的扩展判定有效性,将有效的结果加入到优先队

24、列中去,优先队列通过其启发函数进行排序,将较优的状态放到队头,以便及早扫描,并用两个map进行标记,表示一个已经扫描过,且无效的,另一个表示正在扫描中,作为一些扫描值的限定,当扫描值等于要求结果值时,则寻找成功。3、 核心代码/*A*搜索.*/ #include#include#include#include#include#include#includeusing namespace std;int ten10=1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000;int zt4=-1,-1,3,1,-1,0,4,2

25、,-1,1,5,-1,0,-1,6,4,1,3,7,5,2,4,8,-1,3,-1,-1,7,4,6,-1,8,5,7,-1,-1; struct node;struct node int num; int a9; int zero; int ti; int pre; int h; void setPer(const node &end) pre=0; for(int i=0;i9;i+) if(ai=0)continue; for(int j=0;j9;j+) if(end.aj=ai) pre+=abs(i%3-j%3)+abs(i/3-j/3); h=pre+ti; void setNum() num=0; for(int j=0;j9;j+) num+=ten8-j*aj; if(aj=

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

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