【问题标题】:Finding neighbors of 2d array when represented as 1d array当表示为一维数组时查找二维数组的邻居
【发布时间】:2012-02-20 02:36:14
【问题描述】:

我有一个二维数组,我将其转换为一维数组。在 1d 表示中,我怎样才能找到一个单元格的所有 8 个邻居,以考虑环绕?

这里的上下文是我有一个 2d 游戏板,我将它作为 1d 内存块存储在内存中。我需要能够找到游戏板中所有 8 个相邻单元格的内存位置。我遇到的问题是考虑边缘上的板环绕(特别是如果单元格位于二维数组的角落)。

例如,如果单元格在右上角,则顶部邻居在右下角等。

我在计算时知道电路板尺寸。

编辑:提到我在 MIPS 汇编中这样做可能是相关的......

【问题讨论】:

  • 我给了你一些伪代码,你在 MIPS 中做这件事根本不应该影响算法。您可以轻松计算模数(使用div/divu),并且可以轻松执行基本的数学运算。如果这确实是家庭作业,我想你应该努力做一些努力,我已经向你解释了你需要的一切。

标签: arrays


【解决方案1】:

您只需要一个可以将任意位置映射到数组中包含的位置的函数。

你必须分两步分解问题:

  • 包装
  • 将二维坐标映射到一维

可以使用模运算符轻松完成包装,例如

struct pos { int x,y };

pos wrap(pos p)
{
  pos p2 = p;

  if (p.x >= WIDTH)
    p.x %= WIDTH;
  else if (p.x < 0)
    p.x += WIDTH;

  if (p.y >= HEIGHT)
    ... same thing
}

然后你会有一个肯定包含在数组中的位置,你需要将它映射到 1d,这更容易:

int flatten(pos p)
{
   return p.x*WIDTH + p.y;
}

所以你可以将它们组合起来:

int fpos = flatten(wrap({30,20}));

你就完成了。

【讨论】:

  • 是的,这对于高级语言来说是完美的,但我在 MIPS 中对此进行编码,所以我的数据结构设计为 1d 以模仿内存的线性特性。我需要完全避免使用二维表示。
  • 如果您无法在低级别表示二维数组,那么您将无法在计算机上拥有二维数组..
【解决方案2】:

这是python代码,但是逻辑,使用一个简单的一维平面列表,应该足够清楚:

def neighbors(i, w, h, mode=8):
    """Return a list of neighbors.

    Works as like a 2d graph of 'w' width and 'h' height with boundaries.

    Args:
        i(int): 1d index
        w(int): width of the graph.
        h(int): height of the graph.
        mode(int): 8 for eight directions (includes diagonals); else for
            4 directions (top, down, left, right).

    Returns:
        list
    """
    size = w * h
    neighbors = []
    if i - w >= 0:
        neighbors.append(i - w)  # north
    if i % w != 0:
        neighbors.append(i - 1)  # west

    if (i + 1) % w != 0:
        neighbors.append(i + 1)  # east

    if i + w < size:
        neighbors.append(i + w)  # south

    if mode == 8:
        if ((i - w - 1) >= 0) and (i % w != 0):
            neighbors.append(i - w - 1)  # northwest

        if ((i - w + 1) >= 0) and ((i + 1) % w != 0):
            neighbors.append(i - w + 1)  # northeast

        if ((i + w - 1) < size) and (i % w != 0):
            neighbors.append(i + w - 1)  # southwest

        if ((i + w + 1) < size) and ((i + 1) % w != 0):
            neighbors.append(i + w + 1)  # southeast
    return neighbors

测试/打印它:

if __name__ == '__main__':
    W = 3  # width
    H = 3  # height

    def show(start, neighbors):
        """Simple display of an 2d table.

        Args:
            start(int): initial position (shown as 'S')
            neighbors(list): list of positions (draw as their values)
        """
        for y in range(H):
            print("|", end="")
            for x in range(W):
                i = y * W + x
                if i == start:
                    c = "  S|"
                elif i in neighbors:
                    c = "%3d|" % i
                else:
                    c = "  .|"

                print(c, end="")
            print()

    for i in range(W * H):
        print()
        n = neighbors(i, W, H)
        print("neighbors(%d) of '%d':" % (len(n), i), n)
        show(i, n)

结果:

neighbors(3) of '0': [1, 3, 4]
|  S|  1|  .|
|  3|  4|  .|
|  .|  .|  .|

neighbors(5) of '1': [0, 2, 4, 3, 5]
|  0|  S|  2|
|  3|  4|  5|
|  .|  .|  .|

neighbors(3) of '2': [1, 5, 4]
|  .|  1|  S|
|  .|  4|  5|
|  .|  .|  .|

neighbors(5) of '3': [0, 4, 6, 1, 7]
|  0|  1|  .|
|  S|  4|  .|
|  6|  7|  .|

neighbors(8) of '4': [1, 3, 5, 7, 0, 2, 6, 8]
|  0|  1|  2|
|  3|  S|  5|
|  6|  7|  8|

neighbors(5) of '5': [2, 4, 8, 1, 7]
|  .|  1|  2|
|  .|  4|  S|
|  .|  7|  8|

neighbors(3) of '6': [3, 7, 4]
|  .|  .|  .|
|  3|  4|  .|
|  S|  7|  .|

neighbors(5) of '7': [4, 6, 8, 3, 5]
|  .|  .|  .|
|  3|  4|  5|
|  6|  S|  8|

neighbors(3) of '8': [5, 7, 4]
|  .|  .|  .|
|  .|  4|  5|
|  .|  7|  S|

【讨论】:

    猜你喜欢
    • 2017-10-04
    • 2010-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多