Search code examples
heuristics

straight-line distance heuristic for n-puzzle


Can someone please explain to me what a straight-line distance heuristic would look like while solving the n-puzzle? How would you apply the straight line distance, say for a 8x8 puzzle? Here is an example puzzle:

7 3 4
5 _ 6
8 2 1


Solution

  • Let's recall basic geometry, it is well known that the shortest path between two points is a straight line.

    So considering a 8-puzzle, the straight line distance between two tiles is the number of tiles to get from tile A to tile B whether is diagonal, horizontal or vertical line.

    Considering the example in your question, let's call d(a,b) the straight line distance between tile a and b:

    • d(1,_) = 1
    • d(1,2) = 1
    • d(1,3) = 2 = d(1,6) + d(6,3) = d(1,_ ) + d(_,3)
    • d(1,4) = 2

    and so on.

    We can now generalize that definition to the n-puzzle. Keep in mind that 3 steps are allowed diagonal, horizontal, vertical. In this case the heuristic is usually optimal.

    Note: Remember the straight line definition between cities.