无需重新访问即可遍历 3D 坐标的算法
假设我们有一组从 (0,0,0) 到 (100,100,100) 的 3D(整数)坐标 我们希望访问每个可能的坐标(100^3 个可能访问的坐标),而不需要多次访问每个坐标。
相邻步骤中每个坐标之间的差异之和不能超过2(我不知道这是否可能。如果不可能,则最小化) 例如,从 (0,2,1) 到 (2,0,0) 的步长总差为 5,因为 |x1-x2|+|y1-y2|+|z1-z2| = 5
我们如何生成这样的坐标序列?
例如,开始: (0,0,0) (0,0,1) (0,1,0) (1,0,0) (1,0,1) (0,0,2) (0,1,1) (0,2,0) (1,1,0) (2,0,0) (3,0,0) (2,0,1) (1,0,2) (0,0,3) 等等...
任何人都知道一种算法,可以生成这样的序列到任意坐标(x,y,z),其中x = y = z,或者可以证明这样的算法不可能存在?谢谢
额外的学分:展示如何使用 x!=y!=z 生成这样的序列:D
Say we have a set of 3D (integer) coordinates from (0,0,0) to (100,100,100)
We want to visit each possible coordinate (100^3 possible coordinates to visit) without visiting each coordinate more than once.
The sum of the differences between each coordinate in adjacent steps cannot be more than 2 (I don't know if this is possible. If not, then minimized)
for example, the step from (0,2,1) to (2,0,0) has a total difference of 5 because |x1-x2|+|y1-y2|+|z1-z2| = 5
How do we generate such a sequence of coordinates?
for example, to start:
(0,0,0)
(0,0,1)
(0,1,0)
(1,0,0)
(1,0,1)
(0,0,2)
(0,1,1)
(0,2,0)
(1,1,0)
(2,0,0)
(3,0,0)
(2,0,1)
(1,0,2)
(0,0,3)
etc...
Anyone know an algorithm that will generate such a sequence to an arbitrary coordinate (x,y,z) where x=y=z or can prove that it is impossible for such and algorithm to exist? Thanks
Extra credit: Show how to generate such a sequence with x!=y!=z :D
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
其中一个技巧(还有其他方法)是一次一条线[线段],一次一个平面[正方形]。解决问题的最后一部分,即使访问的体积大小在每个维度上都不相同(例如:100 x 6 x 33 块),这种方法也是有效的。
换句话说:
本质上,每一步都是在一个维度上完成的,恰好是一个。这有点像您在 101 x 101 x 101 的建筑物中从一个房间“步行”到另一个房间,其中每个房间都可以通向给定轴上直接相邻的任何房间(即不会对角连接)。
用编程语言实现这种逻辑是微不足道的!唯一具有一定挑战性的部分是处理“来回”,即有时给定维度的一些变化是积极的,有时是消极的。
编辑:(Sid 关于对角线做同样的问题):
是的!这是很有可能的,因为问题表明我们可以将 [曼哈顿] 距离设置为 2,这是对角线所需的距离。
该路径将类似于上面列出的路径,即来回做线(仅这里是可变长度的线),然后移动到三维中的下一个“面板”,并重复,仅“向上” ”等等。
确实可以混合搭配这些方法,无论是在完整的“面板”级别,例如对角线执行一个,水平方向执行下一个,以及在给定面板内,例如从对角线开始,但是当在顶线上时,继续进行水平模式,只需在“左侧”循环时提前停止一点,因为该侧的一部分已使用对角线处理。
实际上,这允许使用相当多(但显然有限)的方法来“绘制”整个空间。关键是要避免留下(太多)未绘制的相邻区域,因为回到它们可能会让我们陷入死胡同,或者可能需要超过 2 的“跳跃”。
One of the tricks (there are other approaches) is to do it one line [segment] at a time, one plane [square] at a time. Addressing the last part of the question, this approach works, even if the size of the volume visited is not the same in each dimension (ex: a 100 x 6 x 33 block).
In other words:
Essentially, each move is done on a single dimension, by exactly one. It is a bit as if you were "walking" from room to room in a 101 x 101 x 101 building where each room can lead to any room directly next to it on a given axis (i.e. not going joining diagonally).
Implementing this kind of of logic in a programming language is trivial! The only mildly challenging part is to deal with the "back-and-forth", i.e. the fact that sometimes, some of the changes in a given dimension are positive, and sometimes negative).
Edit: (Sid's question about doing the same diagonally):
Yes! that would be quite possible, since the problem states that we can have a [Manhattan] distance of two, which is what is required to go diagonally.
The path would be similar to the one listed above, i.e. doing lines, back-and-forth (only here lines of variable length), then moving to the next "panel", in the third dimension, and repeating, only going "upward" etc.
It is indeed possible to mix-and-match these approaches, both at the level of complete "panel", for example doing one diagonally and the next one horizontally, as well as within a given panel, for example starting diagonally, but when on the top line, proceeding with the horizontal pattern, simply stopping a bit earlier when looping on the "left" side, since part of that side has been handled with the diagonals.
Effectively this allows a rather big (but obviously finite) number of ways to "paint" the whole space. The key thing is to avoid leaving (too many) non painted adjacent area behind, for getting back to them may either bring us to a dead-end or may require a "jump" of more than 2.
也许您可以概括 格雷码,这似乎解决了问题的特殊情况。
Maybe you can generalize Gray Codes, which seem to solve a special case of the problem.
乍一看似乎很简单,但一旦开始,就很棘手!特别是步骤可以是 1 或 2。
这不是一个答案,而是一个特定序列的前 10 多个步骤的演示,希望可以帮助其他人进行可视化。 Sid,如果以下内容有误,请告诉我:
Seems trivial at first but once started, it is tricky! Especially the steps can be 1 or 2.
This is not an answer but more of a demostration of the first 10+ steps for a particular sequence which hopefully can help others to visualise. Sid, please let me know if the following is wrong: