简体   繁体   English

2D阵列上的路径查找

[英]Path finding on 2D array

I have a 2D int array which I processed and got from an image. 我有一个二维int数组,该数组是我从图像中处理并获得的。 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. 我想找到成本最低的2个索引之间的路径(我将这些索引作为输入)。 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#? 我该如何在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. A *算法(已经标记为:))是一个很好的选择。

See, for example, How to implement an A* algorithm? 参见,例如, 如何实现A *算法?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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