otmar's ideas on how the algorithm should work

Do away with the specific sequence of lookup.

Consider the problem-space to be a graph-search problem and implement a generic search algorithm there.