简体   繁体   English

计算二维数组中的曼哈顿距离

[英]Calculating Manhattan Distance within a 2d array

I am writing a program to solve an nxn 8 puzzle problem. 我正在编写一个程序来解决nxn 8难题。 I'm having difficulty with my Manhattan calculation function being off by two from the puzzle I'm testing my program with. 我在测试程序时遇到的难题使我的Manhattan计算功能减少了2倍,遇到了困难。 This will eventually be expanded to use the A* pathfinding algorithm, but I'm not there yet. 最终将扩展为使用A *寻路算法,但我还没有。

Here is my function(which is based on the initial state of the board and not taking into account the amount of moves taken so far): 这是我的功能(该功能基于板的初始状态,不考虑到目前为止的移动量):

// sum of Manhattan distances between blocks and goal
public int Manhattan()  // i don't think this is right yet - check with vince/thomas
{
    int distance = 0;

    // generate goal board
    int[][] goal = GoalBoard(N);

    // iterate through the blocks and check to see how far they are from where they should be in the goal array
    for(int row=0; row<N; row++){
        for(int column=0; column<N; column++){
            if(blocks[row][column] == 0)
                continue;
            else
                distance += Math.abs(blocks[row][column]) + Math.abs(goal[row][column]);
        }
    }

    distance = (int) Math.sqrt(distance);

    return distance; // temp
}

This is the example I'm working off of: 这是我正在处理的示例:

 8  1  3        1  2  3     1  2  3  4  5  6  7  8    1  2  3  4  5  6  7  8
 4     2        4  5  6     ----------------------    ----------------------
 7  6  5        7  8        1  1  0  0  1  1  0  1    1  2  0  0  2  2  0  3

 initial          goal         Hamming = 5 + 0          Manhattan = 10 + 0

My Hamming calculation is correct and returns 5, but my Manhattan returns 8 instead of 10. What am I doing wrong? 我的汉明计算正确,返回5,但曼哈顿返回8,而不是10。我在做什么错?

This is the output of my program: 这是我程序的输出:

Size: 3
Initial Puzzle: 
 8  1   3   
 4  0   2   
 7  6   5   

Is goal board: false

Goal Array: 
 1  2   3   
 4  5   6   
 7  8   0   
Hamming: 5
Manhatten distance: 8
Inversions: 12
Solvable: true

The error lies in the update of the distance. 错误在于距离的更新。

In writing distance += Math.abs(blocks[row][column]) + Math.abs(goal[row][column]); 在书写distance += Math.abs(blocks[row][column]) + Math.abs(goal[row][column]); you add all contents of the cells of initial and goal. 您添加初始和目标单元格的所有内容。 Only one excluded in initial and goal is the cell with same coordinates as 0 in initial. 只有在初始和目标中排除的一个单元格的初始坐标与0相同。

In your example this gives 2 times sum from 0 to 8 minus 5. 2 * 36 -8 = 64 . 在您的示例中,这得到2乘以从0到8减去5的总和2 * 36 -8 = 64 Then you take the square which is 8. 然后取平方为8。

Manhattan - as described by Wiktionary is calculated by distance in rows plus distance in columns. 曼哈顿-如Wiktionary所述,是通过行距加上列距来计算的。

Your algorithm must lock like (beware, pseudocode ahead!) 您的算法必须像一样锁定(注意,伪代码在前!)

for (cell : cells) {
    goalCell = findGoalcell(cell.row, cell.col);
    distance += abs(cell.row - goalCell.row);
    distance += abs(cell.col - goalCell.col);
} 

And don't take the square root. 而且不要扎根。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM