·天新网首页·加入收藏·设为首页
首页|笔记本|手机|数码相机|摄像机|MP3/MP4|主板|内存|显示器|办公|打印机|下载|开发|汽车|学院|业界
硬件|台式机|数码|数字家庭|投影仪|GPS/CPU|显卡|硬盘|服务器|网络|一体机|驱动|源码|游戏|考试|报价
Java学习:TSP递归程序的优化
http://dev.21tx.com 2004年02月14日 zhxfzhxf1/CSDN

上一页 1 2 3 4 5 下一页

  在Java中定义一个类

  ClassCities

  {
  
  privateint[][]cities;//各城市表示为(X,Y)X,Y为0到99之间的值
  
  privateint[]shortestPath;//保存最短路程对应的经历顺序
  
  privateintnum;//保存N(城市个数)
  
  privatelongshortestLength=100000000;//N个城市遍历时可能最大路程
  
  privatelonggetLength(int[]tPath){...}//计算以tPath为经历顺序的路程
  
  publicCities(intn)//构造n个城市的坐标,假设为0到99之间的随机数
  
  {
  
  ...
  
  }
  
  publicint[]getShortestPath()//获得最短路径
  
  {
  
  int[]tempPath=newint[num];
  
  shortestPath=newint[num];
  
  int[]citiesToured=newint[num];//保存第I个城市是否已经经历
  
  intcitiesNum=0;//已经经历城市的个数
  
  for(inti=0;i<num;i++)
  
  citiesToured[i]=0;
  
  goThrough(tempPath,citiesNum,citiesToured);//遍历各城市
  
  for(inti=0;i<num;i++)
  
  tempPath[i]=shortestPath[i];//得到遍历顺序
  
  returntempPath;//返回结果
  
  }
  
  privatevoidgoThrough(int[]tPath,intcNum,int[]cToured)//遍历N个城市
  
  {
  
  if(cNum==0)//无经历城市时,选择第1个城市
  
  {
  
  cNum++;
  
  tPath[0]=0;

  cToured[0]=1;
  
  goThrough(tPath,cNum,cToured);
  
  }
  
  elseif(cNum==num)//各个城市已经经历,结束
  
  {
  
  longtempLength=getLength(tPath);//计算此经历顺序所走的路程
  
  if(tempLength<shortestLength)//比较路程
  
  {
  
  shortestLength=tempLength;//更新最短路程及其经历顺序
  
  for(inti=0;i<num;i++)
  
  shortestPath[i]=tPath[i];
  
  }
  
  }
  
  else
    
  {
  

上一篇: java学习:VisualAge使用技巧
下一篇: java学习:VisualAge使用技巧二

上一页 1 2 3 4 5 下一页

Google
 
热点文章
关于我们 | 联系我们 | 广告服务 | 工作机会 | 版权声明 | 欢迎投稿 | 网站地图
Copyright © 2000-2008 , www.21tx.com , All Rights Reserved .
© 晨新科技 版权所有 Created by TXSite.net