class Solution:
def __init__(self):
self.V = list() def bfs(self,grid,color,rows,coloums,r,c,ocolor):
cur = [r,c]
if cur[0]-1 >=0 and self.V[cur[0]-1][cur[1]]==0 and grid[cur[0]-1][cur[1]]==ocolor:
grid[cur[0]-1][cur[1]] = color
self.V[cur[0]-1][cur[1]] = 1
self.bfs(grid,color,rows,coloums,cur[0]-1,cur[1],ocolor)
if cur[0]+1 <rows and self.V[cur[0]+1][cur[1]]==0 and grid[cur[0]+1][cur[1]] == ocolor:
grid[cur[0]+1][cur[1]] = color
self.V[cur[0]+1][cur[1]] = 1
self.bfs(grid,color,rows,coloums,cur[0]+1,cur[1],ocolor)
if cur[1]-1 >=0 and self.V[cur[0]][cur[1]-1]==0 and grid[cur[0]][cur[1]-1] == ocolor:
grid[cur[0]][cur[1]-1] = color
self.V[cur[0]][cur[1]-1] = 1
self.bfs(grid,color,rows,coloums,cur[0],cur[1]-1,ocolor)
if cur[1]+1 <coloums and self.V[cur[0]][cur[1]+1]==0 and grid[cur[0]][cur[1]+1]==ocolor:
grid[cur[0]][cur[1]+1] = color
self.V[cur[0]][cur[1]+1] = 1
self.bfs(grid,color,rows,coloums,cur[0],cur[1]+1,ocolor) def colorBorder(self, grid: 'List[List[int]]', r0: int, c0: int, color: int) -> 'List[List[int]]':
if grid[r0][c0] != color:
ocolor = grid[r0][c0]
rows = len(grid)
coloums = len(grid[0])
self.V = [[0 for c in range(coloums)] for r in range(rows)]
self.V[r0][c0] = 1
grid[r0][c0] = color
self.bfs(grid,color,rows,coloums,r0,c0,ocolor)
print(self.V)
for i in range(rows):
for j in range(coloums):
if i>0 and i<rows-1 and j>0 and j<coloums-1:
if self.V[i][j] == 1 and self.V[i-1][j]==1 and self.V[i+1][j]==1 and self.V[i][j-1]==1 and self.V[i][j+1]==1:
grid[i][j] = ocolor
return grid
这题是什么鬼玩意儿?是我打开方式不对么。。。