简体   繁体   English

有人可以在曼哈顿为我解释java中的8个谜题吗?

[英]Can somebody explain in Manhattan dstance for the 8 puzzle in java for me?

i am writing an A* algorithm which can solve the 8-puzzle in Java, so far i have implemented DFS, BFS, A* using the number of tiles out of place and i just need to implement it using the heuristic for the Manhattan distance. 我正在编写一个A *算法,它可以解决Java中的8-puzzle,到目前为止,我已经使用了多少个瓷块实现了DFS,BFS,A *,我只需要使用曼哈顿距离的启发式实现它。

As you are probably aware the Manhattan distance is the sum of each tiles displacement in relation to its current position and its index in the goal state. 您可能已经意识到曼哈顿距离是每个瓦片位移相对于其当前位置和目标状态下的索引的总和。

I have googled around and found these stack over flow topics: 我已经google了一下,发现这些堆栈的流程主题:

Calculating Manhattan Distance Manhattan distance in A* 在A *中 计算曼哈顿距离 曼哈顿距离

Which returned the following code: 其中返回了以下代码:

  int manhattanDistanceSum = 0;
for (int x = 0; x < N; x++) // x-dimension, traversing rows (i)
    for (int y = 0; y < N; y++) { // y-dimension, traversing cols (j)
        int value = tiles[x][y]; // tiles array contains board elements
        if (value != 0) { // we don't compute MD for element 0
            int targetX = (value - 1) / N; // expected x-coordinate (row)
            int targetY = (value - 1) % N; // expected y-coordinate (col)
            int dx = x - targetX; // x-distance to expected coordinate
            int dy = y - targetY; // y-distance to expected coordinate
            manhattanDistanceSum += Math.abs(dx) + Math.abs(dy); 
        } 
    }

This is what i don't understand, given this board and this goal state: 这是我不明白的,鉴于这个董事会和这个目标状态:

initial board: 初始董事会:

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

goal state: 目标状态:

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

if i key in the values for board[0][0], which has the value 1, which happens to be 1 move away from its correct position i get these results: 如果我键入board [0] [0]的值,其值为1,恰好是1离开其正确的位置,我得到这些结果:

 x = 0;
 y = 0;
 value = 1;
 targetX = (value -1)/3; // 0/3 = 0
 targetY = (value -1)%3 //0%3 = 0

 int dx = x - targetX; // 0 - 0
 int dy = y - targetY; // 0 - 0
 manhattanDistanceSum += Math.abs(dx) + Math.abs(dy); 

Which produces ultimately 0 + 0, which is obviously incorrect as it should return the value of 1. 最终会产生0 + 0,这显然是不正确的,因为它应该返回值1。

Is there another way to it for example: 有没有其他方法,例如:

  for(int i = 0; i < 3 i ++){
     for(int j =0; j < 3; j ++){
        int value = currentBoard[i][j];
        int goalValue = getLocationOfValueInGoalState(value);

       /* caluclate the value's X distance away from the returned goal state location   for said value, then do the same for the Y */





      }
   }

Hope someone understands my question, im a bit confused at the moment in all honesty. 希望有人理解我的问题,我现在有点困惑,诚实。

There is a fine point of difference in what the goal state looks like for you, and what the goal state looks like for the reference implementation you are viewing. 目标状态对您来说是一个很好的区别,以及您正在查看的参考实现的目标状态。

For the reference implementation, it works if the goal state looks like: 对于参考实现,如果目标状态如下所示:

1 2 3
4 5 6
7 8 0

In your case, you want Manhattan distance for: 在您的情况下,您需要曼哈顿距离:

0 1 2
3 4 5 
6 7 8

A quick fix is to simply redefine your goal state as the former. 快速解决方法是简单地将您的目标状态重新定义为前者。

However, if the latter is what you really want, then change the targetX/Y to not have a subtraction of 1 from value. 但是,如果后者是您真正想要的,那么将targetX / Y更改为不从值减去1。

The manhattan distance is the distance defined as the increase in the distance when moving pieces diagonally. 曼哈顿距离是定义为沿对角线移动棋子时距离增加的距离。 If the movable tile is in the upper right hand corner, to move the piece to the bottom left hand corner, you can't move it directly along the diagonal. 如果可移动的瓷砖位于右上角,要将工件移动到左下角,则无法沿对角线直接移动。 You have to make sequential left then down move. 你必须顺序左移然后向下移动。 The increase is the manhattan distance. 增加的是曼哈顿距离。 The fun part of this is the shuffling algorithm. 有趣的部分是改组算法。 The manhattan distance is also know in mathematics as the taxi-cab distance, http://en.wikipedia.org/wiki/Taxicab_geometry . 曼哈顿距离在数学上也被称为出租车距离, http://en.wikipedia.org/wiki/Taxicab_geometry

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

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