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