避免锯齿形的有效路径查找算法

阿尔珀

我正在开发将物体与电线连接起来的软件。此接线的规则是这些电线不能穿过其他物体,并且不接受对角线移动。喜欢这个萤幕撷取画面

我知道的所有最短路径算法(A *,dijkstra等)都找到这种类型的路径:

我不希望第二个屏幕截图中出现不必要的锯齿。我如何实现这个目标?

注意:想要尝试算法的任何人都可以使用应用程序。

Another Note: This is the exact situation that i do not want. It finds the zigzag path instead of the one "go to the right until the you reach x position of the target, go to the up until you reach the y position of the target" which has the same cost with the zigzagged one. 在此处输入图片说明

Dialecticus

Your current algorithm finds a shortest path, Pmin, but the improved algorithm should find a shortest path that takes minimum number of turns (Pmin, Tmin). General solution requires that you work with pair of numbers instead of a single number. If newly found Pnew is smaller than current Pmin OR if it is equal but Tnew is smaller than Tmin then take (Pnew, Tnew) as new minimal path.

If the board is small enough you could still use a single number as you currently use, but this number must be a compound number C = P * N + T, where N is sufficiently large and sufficiently small constant number. It must be larger than largest possible T for that board, which is almost the total number of tiles in the board. It must be also small enough so that there is no integer overflow when algorithm happens to handle the largest path in the board, which is also the total number of tiles in the board. So N must satisfy these two terms (B is total number of tiles in the board):

N > B
B * N < INT_MAX

If B is larger than SQRT(INT_MAX) then this system is unsolvable, and you should go with pair of values. N should be SQRT(INT_MAX), which for 232 is 216.

现在的主要问题是如何计算所有匝数,但这取决于您使用的算法。添加该部分应该不会太困难。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章