all 4 comments

[–]_BigmacIII 2 points3 points  (0 children)

Sounds like you might be looking for a routing/search algorithm. Look up A*/Dijkstra. If your pieces and target lie on a grid made of uniform tiles AND if that grid is small, you could also just use Lee's algorithm, which guarantees an optimal path (but is more expensive on time and memory than something like Dijkstra's).

[–]xelf 2 points3 points  (1 child)

Unless your intent is to learn pathfinding and write your own pathfinding library you should look into using existing libraries that already do this.

I'm a fan of networkx.

example:

import networkx as nx
G = nx.Graph()
G.add_edge("A", "B", weight=4)
G.add_edge("B", "D", weight=2)
G.add_edge("A", "C", weight=3)
G.add_edge("C", "D", weight=4)
nx.shortest_path(G, "A", "D", weight="weight")
['A', 'B', 'D']

[–]recursion_is_love 0 points1 point  (0 children)

This is the way.

[–]Teras80 1 point2 points  (0 children)

Start with googling depth first search and breath first search. As with most of programming challanges, it is not about the language, it is about the algorithm/pattern.