【问题标题】:Find all the index adjacent to the given index in a NxN board在 NxN 板上找到与给定索引相邻的所有索引
【发布时间】:2017-02-20 21:22:48
【问题描述】:

假设我有一个 10x10 单元板。每个单元格的索引从 1 到 100(基于 1 的索引)。该板是一个单元格列表:[1, 2, 3, ..., 100]

任务。给定一个单元格的索引,找出覆盖它的所有单元格。

例如:

1 | 2 | 3 | 4
_____________
5 | 6 | 7 | 8
_____________
9 |10 |11 |12
_____________
13|14 |15 |16

给定索引:11

返回:[6, 7, 8, 12, 16, 15, 14, 10],订单不是问题。

我的解决方案:

def allAdjacentCell(given_index):
    result = []
    dimension = 4 # length of a line
    if (1 <= given_index - dimension <= 16):
        result.append(given_index - 1) # the cell above the given cell
    if (1 <= given_index + dimension <= 16):
        # below given index
    if (1 <= given_index - 1 <= 16):
        # ...
    if (1 <= given_index + 1 <= 16):
        # ...
    # check for diagonal cells
    return result

如果给定的单元格位于棋盘中心的某个位置,我的方法似乎返回了正确的结果。但是如果给定的单元格在角落或边缘,那就错了。例如,如果给定单元格 = 5,则该方法将包括索引 4 处的单元格,尽管它不与 5 相邻。

解决这个问题的正确方法是什么?

【问题讨论】:

  • 你的董事会是如何代表的?一个列表?列表列表?
  • 是的,它是一个单一的列表。我把它包含在问题中,非常感谢。
  • 这里被称为摩尔社区。​​span>
  • 这会容易得多,如果你的 indeces 开始于0
  • 我会改变基础然后...

标签: python algorithm


【解决方案1】:

我们注意到,只有当索引值位于最右侧或最左侧列或顶部或底部行时,我们才会获得“恶意”值。

顶部/底部行流氓值只是负值或超出范围的值。

对于最左侧列中的索引,恶意值将具有 %dim=0。

对于最右侧列中的索引,恶意值将具有 %dim=1。

因此我们只需要将它们从中心索引的标准值中过滤掉即可。

def all_adjacent(index,dim):
    arr = [index+1,index-1,index+dim,index-dim,index+dim+1,index+dim-1,index-dim+1,index-dim-1]
    if index%dim==0: ## right most row
         arr = filter(lambda x:x%dim!=1,arr)
    if index%dim==1: ## left most  row
        arr = filter(lambda x:x%dim!=0,arr)

    arr = filter(lambda x:x>=1 and x<=dim*dim,arr)  ## top and bottom rows

    return arr

【讨论】:

【解决方案2】:

Willem Van Onsem 给出的答案的一个更普通的版本,其中我明确地在您的索引之间进行转换,并且更容易处理坐标。我还包括了一个测试,以检查一切是否按预期工作:

dimension = 4

def label_to_coords(label):
    x = (label - 1) % dimension
    y = (label - 1 - x) // dimension
    return x, y

def coords_to_label(x, y):
    return x + dimension * y + 1


def allAdjacentCell(given_index):
    result = []
    x, y = label_to_coords(given_index)
    for delta_x in range(-1, 2):
        for delta_y in range(-1, 2):
            if not (delta_x == delta_y == 0):
                new_x = x + delta_x
                new_y = y + delta_y
                if (0 <= new_x < dimension) and (0 <= new_y < dimension):
                    result.append(coords_to_label(new_x, new_y))
    return result

def test():
    size = dimension**2
    for i in range(1, size + 1):
        print(i, allAdjacentCell(i))

if __name__ == "__main__":
    test()

【讨论】:

    【解决方案3】:

    问题在于,如果它是 edge-case(例如5),那么5-1 仍然是有效索引(但不是相邻索引)。您可以通过首先确定xy 来解决这个问题:

    x = (given_index-1) % dimension
    y = (given_index-1) // dimension
    

    没有必要让它那么难:你知道结果应该在0(含)和15(含)之间。您可以使用嵌套的for 循环:

    result = []
    for nx in (x-1,x,x+1):
        if 0 <= nx < dimension:
            for ny in (y-1,y,y+1):
                if 0 <= ny < dimension and (nx != x or ny != y):
                    result.append(ny*dimension+nx+1)
    

    你甚至可以把它放在一个漂亮的单衬里

    x = (given_index-1) % dimension
    y = (given_index-1) // dimension
    result = [ny*dimension+nx+1 for nx in (x-1,x,x+1) if 0 <= nx < dimension for ny in (y-1,y,y+1) if 0 <= ny < dimension and (nx != x or ny != y)]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-17
      • 1970-01-01
      • 1970-01-01
      • 2012-10-25
      • 2017-02-16
      • 2014-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多