我有一個二維數組,我轉換爲一維數組。在1d表示中,我怎樣才能找到一個單元的所有8個鄰居,並計算環繞?查找2D陣列的鄰居時表示爲一維數組
這樣做的上下文是我有存儲在存儲器中作爲存儲器中的一維塊的2D遊戲板。我需要能夠找到遊戲板中所有8個相鄰單元的內存位置。我遇到的問題是佔板纏繞在邊緣上(特別是如果該細胞是在2D陣列的角部)。
例如,如果該細胞是在右上角,上面的鄰居是在右下角,等
我知道當我計算這個電路板尺寸。
編輯:這可能是有關一提的是,我在MIPS彙編這樣做......
我有一個二維數組,我轉換爲一維數組。在1d表示中,我怎樣才能找到一個單元的所有8個鄰居,並計算環繞?查找2D陣列的鄰居時表示爲一維數組
這樣做的上下文是我有存儲在存儲器中作爲存儲器中的一維塊的2D遊戲板。我需要能夠找到遊戲板中所有8個相鄰單元的內存位置。我遇到的問題是佔板纏繞在邊緣上(特別是如果該細胞是在2D陣列的角部)。
例如,如果該細胞是在右上角,上面的鄰居是在右下角,等
我知道當我計算這個電路板尺寸。
編輯:這可能是有關一提的是,我在MIPS彙編這樣做......
你只需要能夠映射到包含在數組中的位置的任意位置的功能。
您必須將問題分解分爲兩步:
包裝可以用模運算很容易做到,像
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
}
然後你就會有這確實是一個包含在數組中的位置,您將n EED映射它做1D,這是更簡單:
int flatten(pos p)
{
return p.x*WIDTH + p.y;
}
這樣你就可以將它們組合起來:
int fpos = flatten(wrap({30,20}));
和你做。
這是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|
我給你那種僞代碼,事實上,你正在做它在MIPS應該不會影響到算法都沒有。你可以很容易地計算模(用'div/divu'),你可以很容易地進行基本的數學運算。如果這確實是家庭作業,我想你應該嘗試做出一些努力,我已經向你解釋了你需要的一切。 – Jack 2012-02-20 02:53:10