如何在图中搜索相同的点?
24111452
11355551
33451543
11444533
11311413
54353141
14134141
25133152
如上面所示,搜索出个数大于3的相同的相邻点, 例如第一行第三,四,五个'1'
除了广度和深度搜索,还有其它吗?
这是我用的算法
def search(matrix, remove, x, y):
v = matrix[x][y]
#left, do not search right
if y - 1 >= 0 and (x,y-1) not in remove:
if matrix[x][y-1] == v :
remove.append((x,y-1))
temp = click(matrix, remove, x,y-1)
if len(temp) > 0: remove.extend(temp)
#right, do not search left
if y + 1 < N and (x,y+1) not in remove:
if matrix[x][y+1] == v:
remove.append((x,y+1))
temp = click(matrix, remove, x,y+1)
if len(temp) > 0: remove.extend(temp)
#up, do not search down
if x - 1 >= 0 and (x-1,y) not in remove:
if matrix[x-1][y] == v:
remove.append((x-1,y))
temp = click(matrix, remove, x-1,y)
if len(temp) > 0: remove.extend(temp)
#down, do not search up
if x + 1 < N and (x+1,y) not in remove:
if matrix[x+1][y] == v:
remove.append((x+1,y))
temp = click(matrix, remove, x+1,y)
if len(temp) > 0: remove.extend(temp)
return remove
matrix就是上面的矩阵,remove保存相同的点.