曼哈顿距离澄清 [英] Manhattan Distance Clarification

查看:99
本文介绍了曼哈顿距离澄清的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在计算以下code snipets的曼哈顿距离的差异。我有二维数组 INT [] []状态,并要计算从当前节点到目的节点的manahattan距离: 例如:

I would like to know the difference in calculating the manhattan distance of the following code snipets. I have 2D array int[][] state and want to calculate the manahattan distance from a current node to the goal node: example:

当前节点

0 1 3
4 2 5
7 8 6

0 ==空瓦

0 == empty tile

我现在必须计算曼哈顿距离这个节点到目的节点:

I must now calculate the manhattan distance from this node to the goal node:

1 2 3
4 5 6
7 8 0

这些都是一些我已经找到了例子:

These are some of the examples i have found:

1)这一个使用的x和y坐标来计算距离

1) This one uses the x and y coordinates to calculate the distance

public int manhattan(Node currentNode, Node goalNode) {
    return Math.abs(currentNode.x - goalNode.x) + Math.abs(currentNode.y - goalNode.y);
}



2)这一个使用的坐标,但做了一些计算,在这我不明白其中的含义。

2) This one uses the coordinate but does some calculation in the which i don't understand the meaning.

private static int manhattan(int[] pos, int tile) {
    int[] dest = new int[] { (tile - 1) % size, (tile - 1) / size };
    return Math.abs(dest[0] - pos[0]) + Math.abs(dest[1] - pos[1]);
} 



3)这一个人使用在细胞中的编号,以执行计算



3) This one the person uses the numbers in the cells to do the calculations

public int Manhattan(Node current Node goal){
    int dist = 0;
    for(int x = 0; x < current.row; x++)
        for(int y = 0; y < current.col; y++)
            dist += Math.abs(current.state[x][y] - goal.state[x][y]);
}

哪一个是正确的我?

which one is correct for me?

感谢

推荐答案

第一个是假设的边界不能被缠。二是假设,如果你去到右边的权利,你到了左边。我不知道第三个是做相关的曼哈顿距离。而其中,更适合您取决于你正在试图解决什么问题。

The first one is assuming that the borders cannot be wrapped around. The second is assuming that if you go to the right of the right edge, you get to the left edge. I have no idea what the third one is doing related to the Manhattan distance. The one that is correct for you depends on what problem you are trying to solve.

这篇关于曼哈顿距离澄清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆