I\'ve learnt a dynamic programming algorithm to find the \"cheapest\" path from A to B. Each sub path has an associated cost.
Each corner is calculated using
Check out Hillclimbing and A-star algorithms on wikipedia.