Question

I'm trying to write 8-puzzle solver but I couldn't yet: I use Manhattan priority function and I wonder, how to make choice between descendant board arrangements if they have the equal priority values.

For example:

this is initial board arrangement:

           8 1 3
           4   2
           7 6 5

and its descendants board arrangements:

I              

  8 1 3  
  4 6 2                
  7   5  

Manhattan distance + move Number = 10

 II

  8   3   
  4 1 2   
  7 6 5 

Manhattan distance + move Number = 12

III

  8 1 3
  4 2  
  7 6 5  

Manhattan distance + move Number = 10

which board arrangement should program choose? I or III ???

Was it helpful?

Solution

You should sum Manhattan distance for every single number (Manhattan distance to get 1 into place plus Manhattan distance to get 2 into place ... plus Manhattan distance to get 8 into place). If you use some heuristic algorithm (such as A*) you expand first board with lowest sum. If two boards have the same sum value it doesn't matter which one you choose to expand first. States you've visited earlier should be saved as closed somewhere (so you don't get to recurrent cycle) and not considered at all.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top