矩阵中两个节点之间的最短距离是多少?

发布于 2024-10-27 14:57:11 字数 322 浏览 5 评论 0原文

我有一个 5x5 矩阵(25 个节点)。有没有一个公式可以找到矩阵中两个节点 i 和 j 之间的最短距离?

注:1 个节点与其邻居之间的距离为 1 个单位。

=================

根据我的观察,这两个节点 i 和 j 之间有许多距离相同的路径 所以我不确定是否有一个公式可以计算最短的?如果有人可以提供帮助,我将不胜感激。谢谢。

例如:

* * * i *
* * * * *
* * * * *
* * * * *
* j * * *

i 和 j 之间的最短距离是 6 个单位。

I have a matrix 5x5 (25 nodes). Is there a formula that I can find the shortest distance between 2 node i and j in the matrix ?

Note: distance between 1 node and its neighbor is 1 unit.

=================

In my observation, there are many paths with the same distance between those 2 nodes i and j
so i'm not sure if there is a formula to calculate the shortest one? I appreciate if anybody can help. Thanks.

Ex:

* * * i *
* * * * *
* * * * *
* * * * *
* j * * *

Shortest distance between i and j is 6 units.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

三五鸿雁 2024-11-03 14:57:11

我相信您需要的是 L1 距离,也称为 曼哈顿距离。因此,如果您的两个节点具有矩阵索引 (i1,j1)(i2,j2),那么它们之间的最短距离为 |i1-i2|+ |j1-j2|

当然,这是假设您不能沿对角线移动。

I believe what you need is the L1 distance, also called the Manhattan distance. So if your two nodes have matrix indices (i1,j1) and (i2,j2), then the shortest distance between them is |i1-i2|+|j1-j2|.

This is of course, assuming you can't move diagonally.

冷月断魂刀 2024-11-03 14:57:11

我认为正常的勾股定理就可以了。获取您所在位置与您想去的位置之间的 X、Y 差异;这会给你一个负值或正值。由此,您应该能够根据需要向上/向下左/右移动,直到位于同一行/列。无法弄清楚如何获取上标;但这会起作用。

a^2 + b^2 = c^2

I would think that the normal Pythagorean theorem would work just fine. Get the X,Y difference between where you are and where you want to go; this will give you a negative or positive value. From this you should be able to move up/down left/right as needed until you are in the same row/column. Couldn't figure out how to get superscript; but this will work.

a^2 + b^2 = c^2
听风吹 2024-11-03 14:57:11

Take a look at Metric Space

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文