查找具有相同值的相邻小区及其相邻小区等

时间:2018-12-15 11:45:21

标签: python-3.x algorithm matrix

我有一张这样的桌子

6 5 6 6 3 6
2 4 6 8 1 5
3 6 6 6 4 6
7 4 2 6 1 8

,用户选择了0.2(6) 我必须找到该单元格具有相同值的所有邻居,以及它们具有相同值的邻居,依此类推。 对于此表,我需要这样的列表

['0,2','0,3','1,2','2,1','2,2','2,3','3,3']

注意:表的大小没有限制。

2 个答案:

答案 0 :(得分:0)

为什么不创建与矩阵中当前坐标偏移1的所有组合,而抛弃具有任何负坐标的组合呢?

答案 1 :(得分:0)

一年后,我想起了我的问题,然后又回答了我自己的问题,因为我现在好多了。这是我的解决方法。

def findNeighbors(gameMap,selectedRow,selectedColumn):
    finalNeighbors = [] #the list that we will store final state of neighbor list
    newNeighbors = [] #the list that we will store the new neighbors in every loop
    newNeighborsClone = [] #the list that we will traverse inside after clearing the new neighbor list
    newNeighbors.append([selectedRow,selectedColumn]) #we already have a known neighbor which is chosed one
    selectedNumber = gameMap[selectedRow][selectedColumn] 
    print("Selected number = " + str(selectedNumber))
    while newNeighbors: #while newNeighbors list has an element inside
        finalNeighbors = finalNeighbors + newNeighbors #add every new neighbor to main neighbor list
        newNeighborsClone = newNeighbors[::] #clone the new neighbors list
        newNeighbors = [] #clearing the new neighbors list
        for row,column in newNeighborsClone:
            if row == 0: #if we're at peak we only need to look for down
                if gameMap[row+1][column] == selectedNumber and [row+1,column] not in finalNeighbors: #and to prevent infinite loops, we have to check if its already in final neighbors list
                    newNeighbors.append([row+1,column])
            elif row == (len(gameMap)-1): #if we're at the bottom we only need to look for up
                if gameMap[row-1][column] == selectedNumber and [row-1,column] not in finalNeighbors:
                    newNeighbors.append([row-1,column])
            else: #if we're somewhere at middle we have to look up and down
                if gameMap[row+1][column] == selectedNumber and [row+1,column] not in finalNeighbors:
                    newNeighbors.append([row+1,column])
                if gameMap[row-1][column] == selectedNumber and [row-1,column] not in finalNeighbors:
                    newNeighbors.append([row-1,column])
            if column == 0: #if we're at the left side we only need to look for right
                if gameMap[row][column+1] == selectedNumber and [row,column+1] not in finalNeighbors: 
                    newNeighbors.append([row,column+1])
            elif column == (len(gameMap[0])-1): #if we're at the right side we only need to look for left
                if gameMap[row][column-1] == selectedNumber and [row,column-1] not in finalNeighbors:
                    newNeighbors.append([row,column-1])
            else: #if we're somewhere at middle
                if gameMap[row][column+1] == selectedNumber and [row,column+1] not in finalNeighbors:
                    newNeighbors.append([row,column+1])
                if gameMap[row][column-1] == selectedNumber and [row,column-1] not in finalNeighbors:
                    newNeighbors.append([row,column-1])
    finalNeighbors = deleteDuplicates(finalNeighbors) #Even after all of if elses, there can be still duplicated elements. Better removing them
    return finalNeighbors

我无法将我的整个代码复制到此处,我不知道为什么。但是这是分配给执行此工作的功能。我可以只使用tryexcept块代替所有其他if块,但是在该分配中禁止使用tryexcept块。所以我必须这样做。您可以只在try块中检查所有四个方面,而不是那么多的ifs