First decide if you need to optimize, because any optimization will complicate your code and for a small number of purposes, your current solution is probably great for simple heuristics such as Manhattan distance.
Before you take the first step, calculate a heuristic for each target. Remember the closest target as the selected target and move towards it, but subtract the maximum possible progress in relation to any target from all other distances. You can consider this second meaning "metaheuristic"; it is an optimistic assessment of heuristics for other purposes.
In the next steps, calculate the heuristic for the current target and any goals with a "metaheuristic" that is less than or equal to the heuristic. Other goals cannot be better than heuristics, so you do not need to calculate them. The immediate goal will be the new current goal; move to it, subtracting the maximum possible progress from others. Repeat until you reach your goal.
source share