【发布时间】:2017-03-26 10:51:04
【问题描述】:
我想知道是否有一种方法可以在我的解决方案中获取蛮力算法 (TSP) 选择的路径(不仅是距离),就像我只得到距离一样。
请注意,我从 City Stockholm 开始,到 Stockholm 结束。
- 最短路径是city1 --> City2 --> City3 ----> .......然后---> City1 ?
我的主要课程的一部分:
public void step(boolean[] wentTo, int currentCity, float distance)
{
int wentToCount = 0;
for (int i = 1; i <= cityCount; ++i)
{
if (wentTo[i - 1])
{
++wentToCount;
continue;
}
boolean[] copy = new boolean[cityCount];
System.arraycopy(wentTo, 0, copy, 0, cityCount);
copy[i - 1] = true;
float dist = distance + distances[distanceIndex(currentCity, i)];
step(copy, i, dist);
}
if (wentToCount == cityCount)
{
if (shortest > distance)
{
shortest = distance;
}
}
}
【问题讨论】:
-
这不只是在计算中注册变量的问题吗?如果您的算法稍后决定另一条路线,则重置。
-
@OleV.V.请在代码中显示它吗?
标签: java algorithm brute-force traveling-salesman