javaalgorithmsliding-tile-puzzle

depth of a* search algorithm in puzzle problem


I am trying to implement a 4*4 puzzle solver using A* search algorithm. I understand that the totalDistance f(n) = g(n) + h(n), whera g(n)is the path cost from root to the current node. However, I am confused about how to calculate the depth for each level when setting the totalcost to each node. Hope someone could explain.


Solution

  • The A* algorithm doesn't really have a concept of 'depth'. Instead, the idea is to keep a record of the cost of getting from the start to all visited nodes. When a new node is visited, the cost to get to the new node is cost to current node + cost to go from current to new node.

    The total cost heuristic is used for only 1 purpose: choosing the next node to consider. As you say it is generally the cost to the node + estimated cost to destination.

    With respect to changes to your code, I suggest you either store the 'score' as the cost to the node (ignoring the estimate) and calculate the heuristic in your sorting comparator, or store both the cost and the estimate separately (if calculating the estimation is expensive for any reason).