简体   繁体   中英

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#?

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)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM