【发布时间】:2016-12-10 13:33:36
【问题描述】:
给定一组代表飞行路径的坐标,练习是找到最大距离(给定 n 个要通过的点)。为了说明问题,我们在 2D 网格上表示了一条飞行路径,如下所示:
.
下面是算法应该对参数 n(整数)执行的操作。
问题是找到一种算法,可以扫描所有点并尝试通过组合所有距离并返回最终路径的长度。
我们已经有了一个可以得到两点距离的方法:
/**
* @return the distance between the two coordinates
*/
public double distance(Coordinate destination) {}
/**
* @return the farthest coordinate from start
*/
public Coordinate coordMax() {}
/**
* @return max distance using n points
* I would maybe try to go for a recursive solution
* and already have the 2 corner cases down.
*/
public double statMaxDistance(int n) {
if (n == 0)
return coordTable[0].distance(coordTable[coordTable.length - 1]);
if (n == 1)
return coordTable[0].distance(coordMax());
// TODO recursive step
return statMaxDistance();
}
问题是:
有没有一种方法可以完成这项任务,而无需逐个迭代整个路径的每个点,尝试所有可能的组合,计算所有可能的距离以最终得到最远的距离?
遵循这样一种方法似乎相当合理,其中只有 1 或 2 个点会沿着整个路径移动,但在计算给定 3 个以上参考点的最大距离时,这种算法会非常贪婪。
【问题讨论】:
标签: java algorithm optimization path coordinates