计算曼哈顿距离

时间:2011-11-22 09:24:13

标签: java algorithm

我正在java 2D数组int [] []状态中实现NxN puzzels。我需要以下列方式使用曼哈顿的heureustic:

             the sum of the vertical and horizontal distances from 
                the current node to the goal node/tile

                                +(plus)

    the number of moves to reach the goal node from the initial position

此刻我不知道该怎么走。我是2D阵列益智游戏编程的初学者,所以很难理解某些概念。

我在询问是否有人可以帮助我逐步向我解释在Java中编写此代码时必须遵循的程序。

谢谢

1 个答案:

答案 0 :(得分:41)

这更像是一个数学问题,但无论如何,曼哈顿距离是水平距离和垂直距离的绝对值之和

int distance = Math.abs(x1-x0) + Math.abs(y1-y0);

更多信息:http://en.wikipedia.org/wiki/Taxicab_geometry