滑雪算法(贪心算法)的java和c的实现
2013-12-24 10:52
417 查看
java实现:
publicclassDemo{ /** *计算区域最长长度 * *Michael喜欢滑雪这并不奇怪,因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜, *而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道在一个区域中最长底滑坡。 *区域由一个二维数组给出。数组的每个数字代表点的高度 *下面是一个例子 *12345 *161718196 *152425207 *142322218 *131211109 *一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。 *在上面的例子中,一条可滑行的滑坡为24-17-16-1。当然25-24-23-...-3-2-1更长。事实上,这是最长的一条。 * *思路:1、先把各点的高度按照从小到大排序 * 2、再依次从最小的点遍历,看它周围【上下左右】有没有比它高的点 * 3、一旦有,就更新比它高的点的路径长度 *注意:最后输出的是路径长度而不是最长路径的起始高度 *由于开始时路径长度初始化为0,所以最后的结果要+1 *@paramall存储所有区域高度数据 *@return区域最长长度 */ publicintgetMaxDistance(int[][]all){ inti=0,j=0,n=0; intilen=all.length; intjlen=all[0].length; intlen=jlen*jlen; intmaxDistance=0; int[][]dirlen=newint[ilen][jlen]; int[][]dir={{0,1},{1,0},{0,-1},{-1,0}};//向右、下、左、上 Point[]points=newPoint[ilen*jlen]; for(i=0;i<ilen;i++){ for(j=0;j<jlen;j++){ points =newPoint(); points .x=i; points .y=j; points .height=all[i][j]; // System.out.println(points .height+","); n++; } } //按照高度从小到大排序 quickSort(points,0,points.length-1); for(intk=0;k<len;k++){ intx=points[k].x; inty=points[k].y; intnx,ny; for(intd=0;d<dir.length;d++) { nx=x+dir[d][0]; ny=y+dir[d][1]; if(judge(all,nx,ny)&&all[nx][ny]>all[x][y]){ if(dirlen[nx][ny]>dirlen[x][y]+1){ dirlen[nx][ny]=dirlen[nx][ny]; } else{ dirlen[nx][ny]=dirlen[x][y]+1; } } } } for(i=0;i<ilen;i++){ for(j=0;j<jlen;j++){ if(dirlen[i][j]>maxDistance){ maxDistance=dirlen[i][j]; } } } // System.out.println(maxDistance+1); returnmaxDistance+1; } //判断点是否超出范围 Booleanjudge(int[][]all,intx,inty){ if(x>=0&&x<all.length&&y>=0&&y<all[0].length){ returntrue; } else{ returnfalse; } } //用快速排序法把所有点按照高度从小到大排序 voidquickSort(Point[]points,intlow,inthigh){ intloc=0; if(low<high){ loc=partition(points,low,high); quickSort(points,low,loc-1); quickSort(points,loc+1,high); } } intpartition(Point[]points,intlow,inthigh){ Pointpivot=points[low]; while(low<high){ while(low<high&&points[high].height>pivot.height){ high--; } points[low]=points[high]; while(low<high&&points[low].height<=pivot.height){ low++; } points[high]=points[low]; } points[low]=pivot; returnlow; } }
publicclassPoint{intx;inty;intheight;}
importjunit.framework.Assert;importjunit.framework.TestCase;publicclassDemoTestextendsTestCase{publicvoidtestCase01(){Demodemo=newDemo();int[][]all={{1,2,3,4,5},{16,17,18,19,6},{15,24,25,20,7},{14,23,22,21,8},{13,12,11,10,9}};Assert.assertEquals(25,demo.getMaxDistance(all));}}
c实现:
#include<stdio.h>#include<string.h>#include<algorithm>usingnamespacestd;constintmaxn=110;intmap[maxn][maxn];intdp[maxn][maxn];intr,c;intdir[4][2]={0,1,1,0,0,-1,-1,0};structPoint{intx,y;inthigh;}p[maxn*maxn];boolcmp(Pointa,Pointb){returna.high<b.high;}intjudge(intx,inty){if(x>=1&&x<=r&&y>=1&&y<=c)return1;elsereturn0;}voidsolve(intx,inty){intnx,ny;intm=0;for(inti=0;i<4;i++)//临近的点比当前点高{nx=x+dir[i][0];ny=y+dir[i][1];if(map[nx][ny]>map[x][y]&&judge(nx,ny))dp[nx][ny]=max(dp[nx][ny],dp[x][y]+1);}}intmain(){while(scanf("%d%d",&r,&c)!=EOF){memset(map,0,sizeof(map));memset(dp,0,sizeof(dp));intn=0;//点的个数for(inti=1;i<=r;i++){for(intj=1;j<=c;j++){scanf("%d",&map[i][j]);p.x=i;p.y=j;p.high=map[i][j];n++;}}sort(p,p+n,cmp);//按照高度从小到大排序for(inti=0;i<n;i++){intx=p[i].x;inty=p[i].y;solve(x,y);}/*for(inti=1;i<=r;i++){for(intj=1;j<=c;j++)printf("%d",dp[i][j]);printf("\n");}*/intlen=0;for(inti=1;i<=r;i++){for(intj=1;j<=c;j++){if(dp[i][j]>len){len=dp[i][j];}}}printf("%d\n",len+1);}return0;}
相关文章推荐
- playframework拦截器和热加载 源码浅析
- Java中的内省
- java日期/时间浅谈
- 什么是Spring
- Eclipse 自动提示补全设置
- Java学习路线图
- [转发]java线程安全问题之静态变量、实例变量、局部变量
- java Annotation 遇到的错误
- 避免Java应用中NullPointerException的技巧和最佳实践
- myeclipse9或myeclipse10安装svn的方法
- Java中super的几种用法并与this的区别
- java开源报表工具——42种,只有你想不到,没有你找不到!
- myeclipse堆栈溢出
- springMVC uploadify的应用
- java(枚举)使用详解 + 总结
- myeclipse 上安装 Maven3
- java常见错误及解决方法
- java中的四舍五入研究
- java_集合体系之List体系总结、应用场景——07
- spring properties resolve 问题