-1

这项工作是为了分配。我正在使用二维数组,到目前为止,我已经找到了一条我想要通过数组的路径。数组中还有其他路径,我也希望能够通过它们,但我不能重用任何路径。我的数组看起来像:

0,2,4,1,6,0,0
2,0,0,0,5,0,0
4,0,0,0,5,5,0
1,0,0,0,1,1,0
6,5,0,1,0,5,5
0,0,5,1,5,0,0
0,0,0,0,5,0,0

我查找附近邻居的代码是:

def check_neighbours(Alist, node):
        nodes = []
        for i in range(0,2):
                for j in range(0,2):
                        x = node[0]+i
                        y = node[1]+j
                        if x>=0 and y>=0 and (i!=0 or j!=0) and Alist[x][y]>0:
                                nodes.append([x, y])

我将每个访问的坐标 x,y 附加到一个列表中,该列表构建了所采用的完整路径。以下是输出示例:

inside pathing function
path taken is ['[0, 1]', '[0, 2]', '[0, 3]', '[0, 4]', '[1, 4]', '[2, 4]', '[2, 5]', '[3, 5]', '[4, 5]', '[4, 6]']

由于我搜索邻居的方式(将 x 和 y 分开),我想不出一种方法来测试建立的当前坐标是否已经位于列表中。我认为代码将适合以下两行:

if x>=0 and y>=0 and (i!=0 or j!=0) and Alist[x][y]>0:
                                    nodes.append([x, y])
4

1 回答 1

0

关于什么

nodes = []
nodes.append([1,1])
nodes.append([2,2])
nodes.append([3,3])

[1,1] in nodes
# True

[1,3] in nodes
# False

我不是很清楚你的问题,所以这可能是关闭的。

话虽如此,因为为了被添加到nodes列表中,它必须是前一个节点的右侧一列,下方一列,或两者兼而有之。而且由于您只考虑右侧和下方 ( range(0,2)) 的节点,因此您永远不会在列表中找到重复项。

于 2013-04-05T21:26:13.887 回答