Manhattan Distance Clarification

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:

current node

0 1 3
4 2 5
7 8 6

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) 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) 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) 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?

thanks


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.

链接地址: http://www.djcxy.com/p/65558.html

上一篇: N的启发式A *算法

下一篇: 曼哈顿距离澄清