user1125953
user1125953

Reputation: 605

Path finding on 2D array

I have a 2D int array which I processed and got from an image. Each index can be thought as weight of that pixel. I want to find a path between 2 indexes (I'll give these indexes as input) that has the least cost. It would be great if the direction of movements can be modified (like only down&left, up&left. or all. etc. otherwise it may be down, left and right)

How can i do that in C#?

Upvotes: 0

Views: 5350

Answers (2)

payo
payo

Reputation: 4561

Regardless of language, I would calculate the cost for a direct path first. This will became the first base line. Then I would recursively search for a shorter path. You can make a few boundary checks to reduce the recursion.

  1. Any path that is >= the base line (or current best) is terminated
  2. Any path that would hit an index twice is terminated
  3. Any successful path sets the new base line (or best)

Upvotes: 1

AKX
AKX

Reputation: 169388

The A* algorithm (as was already tagged :)) is a good choice for this.

See, for example, How to implement an A* algorithm?

Upvotes: 0

Related Questions