Theoretically, looking at what you wrote is correct. However, there is one very important property of the graphs that you run A * that must be valid so that you know that the algorithm gives the optimal solution: the heuristic function that you use should be optimistic, i.e. Never overestimate the real distance to the target. If I understand correctly, you have a couple of node nodes C and D, and the problem is that the heuristic value of A is not optimistic, in fact, it overestimates (the path from A to the target node C is only 1, which is less than h (A ) = 3). That is why you are not really getting the optimal solution.
source share