具有 Zig Zag 顺序的 2D 数组到 1D 数组算法
我有一个 8x8 数组,想将其转换为具有 64 个字段的一维数组。 但在这个特殊的之字形顺序中:
我想要聪明的方法,但我没有任何线索。我有两种可能的理论,但它们并不聪明。有什么想法吗?
i got an 8x8 Array and want to convert it into an 1d array with 64 fields.
But in this special zig zag order:
I want the smart way but i don't have a clue. I got two possible theories but they are not smart. Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有一个包含 64 个条目的数组,用于按照锯齿形模式访问索引的顺序保存索引。您可能会使用它们作为某些缓冲区的偏移量。这是 JPEG 的东西吧?
Have an array with 64 entries that holds the indexes in the order that the zig-zag pattern would visit them. You'd probably use them as offsets from some buffer. This is for JPEG stuff right?
你可以试试这个:开始时,你向右走一步,然后在唯一允许对角线的方向上进行对角线移动(一开始是西南方向),一直循环直到列[0 ]。向下移动 1 步(使用一个标志来检查您之前是否向右走了 1 步,并且只有在此标志之前为 true 时,您才能向下移动;您可以将此标志更改为 false,并将向下标志更改为 true)并再次沿唯一允许的方向(东北)走对角线,直到到达 row[0]。直到 AC[70] 为止都很好,因为没有向下移动。此时,将 right 和 down 的两个标志重置为 false 并重新启动该过程。
You can try this: when you start, you take 1 step to the right, and then take a diagonal move in the only direction that permits diagonals (at first this is south-west), and loop all the way till the column[0]. The you move 1 step down (use a flag to check if you took 1 step to the right previously, and you can only move down if this flag was true before; you can change this flag to false and change the down flag to true) and again take a diagonal in the only permitted direction (north-east) till you hit row[0]. This will be fine till AC[70], as there is no down move. At this point, reset both flags for right and down to false and restart the process.
为了帮助任何谷歌搜索但找不到答案的人,这里有一个我想出的简单的 Perl 算法来解决这个问题。
您可以这样使用它:
其中
$OneArray
是您要将$TwoArray
转换为的一维数组。For the help of anyone google searching and can't find an answer, here's a simple perl algorithm I came up with to solve the problem.
And you use it like this:
Where
$OneArray
is the 1D array you're converting$TwoArray
to.