python在数组中上下左右判定时的方法
image为3*3数组
使用以下方法可以考虑边界问题的情况下,进行上下左右判定,队列的方法避免重复判定
que = collections.deque([(sr, sc)])
while que:
x, y = que.popleft()
for mx, my in [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]:
if 0 <= mx < n and 0 <= my < m and image[mx][my] == currColor:
que.append((mx, my))
image[mx][my] = newColor