import numpy as np r = np.array([[1,1],[1,1],[1,1]]) w = 2 h = 3 mask = [[False] * w] * h dx = [1,0,1,0,1,-1,1,-1] dy = [0,1,0,-1,1,1,-1,-1] def dfs(x,y): mask[x][y] = True r[x][y] = 42 res = 0 for j in range(len(dx)): nx = x + dx[j] ny = y + dy[j] if 0 <= nx < h and 0 <= ny < w and mask[nx][ny] == False: # and opening[nx][ny] == 255 res += dfs(nx,ny) + 1 return res print r,mask dfs(0,0) print r,mask [[1 1] [1 1] [1 1]] [[False, False], [False, False], [False, False]] [[42 42] [ 1 1] [ 1 1]] [[True, True], [True, True], [True, True]]
Как видно заполняется только первая строка, в списках заполяется вся таблица.