classSolution: defcolorBorder(self, grid: List[List[int]], row: int, col: int, color: int) -> List[List[int]]: m, n = len(grid), len(grid[0]) visited = [[False] * n for _ inrange(m)] borders = [] originalColor = grid[row][col] visited[row][col] = True self.dfs(grid, row, col, visited, borders, originalColor) for x, y in borders: grid[x][y] = color return grid
defdfs(self, grid, x, y, visited, borders, originalColor): isBorder = False m, n = len(grid), len(grid[0]) direc = ((-1, 0), (1, 0), (0, -1), (0, 1)) for dx, dy in direc: nx, ny = x + dx, y + dy ifnot (0 <= nx < m and0 <= ny < n and grid[nx][ny] == originalColor): isBorder = True elifnot visited[nx][ny]: visited[nx][ny] = True self.dfs(grid, nx, ny, visited, borders, originalColor) if isBorder: borders.append((x, y))
publicclassSolution { publicint[][] ColorBorder(int[][] grid, int row, int col, int color) { int m = grid.Length, n = grid[0].Length; bool[,] visited = newbool[m, n]; IList<int[]> borders = new List<int[]>(); int originalColor = grid[row][col]; visited[row, col] = true; DFS(grid, row, col, visited, borders, originalColor); for (int i = 0; i < borders.Count; i++) { int x = borders[i][0], y = borders[i][1]; grid[x][y] = color; } return grid; }
privatevoidDFS(int[][] grid, int x, int y, bool[,] visited, IList<int[]> borders, int originalColor) { int m = grid.Length, n = grid[0].Length; bool isBorder = false; int[][] direc = {newint[]{0, 1}, newint[]{0, -1}, newint[]{1, 0}, newint[]{-1, 0} }; for (int i = 0; i < 4; i++) { int nx = direc[i][0] + x, ny = direc[i][1] + y; if (!(nx >= 0 && nx < m && ny >= 0 && ny < n && grid[nx][ny] == originalColor)) { isBorder = true; } elseif (!visited[nx, ny]){ visited[nx, ny] = true; DFS(grid, nx, ny, visited, borders, originalColor); } } if (isBorder) { borders.Add(newint[]{x, y}); } } }
var dirs = []struct{ x, y int }{ {-1, 0}, {1, 0}, {0, -1}, {0, 1} }
funccolorBorder(grid [][]int, row, col, color int) [][]int { m, n := len(grid), len(grid[0]) type point struct{ x, y int } borders := []point{} originalColor := grid[row][col] vis := make([][]bool, m) for i := range vis { vis[i] = make([]bool, n) }
var dfs func(int, int) dfs = func(x, y int) { vis[x][y] = true isBorder := false for _, dir := range dirs { nx, ny := x+dir.x, y+dir.y if !(0 <= nx && nx < m && 0 <= ny && ny < n && grid[nx][ny] == originalColor) { isBorder = true } elseif !vis[nx][ny] { vis[nx][ny] = true dfs(nx, ny) } } if isBorder { borders = append(borders, point{x, y}) } } dfs(row, col)
for _, p := range borders { grid[p.x][p.y] = color } return grid }
classSolution: defcolorBorder(self, grid: List[List[int]], row: int, col: int, color: int) -> List[List[int]]: originalColor = grid[row][col] m, n = len(grid), len(grid[0]) visited = [[False] * n for _ inrange(m)] borders = [] direc = ((-1, 0), (1, 0), (0, -1), (0, 1)) q = deque([(row, col)]) visited[row][col] = True while q: x, y = q.popleft() isBorder = False for dx, dy in direc: nx, ny = x + dx, y + dy ifnot (0 <= nx < m and0 <= ny < n and grid[nx][ny] == originalColor): isBorder = True elifnot visited[nx][ny]: visited[nx][ny] = True q.append((nx, ny)) if isBorder: borders.append((x, y)) for x, y in borders: grid[x][y] = color return grid
var dirs = []struct{ x, y int }{ {-1, 0}, {1, 0}, {0, -1}, {0, 1} }
funccolorBorder(grid [][]int, row, col, color int) [][]int { m, n := len(grid), len(grid[0]) type point struct{ x, y int } borders := []point{} originalColor := grid[row][col] vis := make([][]bool, m) for i := range vis { vis[i] = make([]bool, n) }
q := []point{ {row, col} } vis[row][col] = true forlen(q) > 0 { p := q[0] q = q[1:] x, y := p.x, p.y isBorder := false for _, dir := range dirs { nx, ny := x+dir.x, y+dir.y if !(0 <= nx && nx < m && 0 <= ny && ny < n && grid[nx][ny] == originalColor) { isBorder = true } elseif !vis[nx][ny] { vis[nx][ny] = true q = append(q, point{nx, ny}) } } if isBorder { borders = append(borders, point{x, y}) } }
for _, p := range borders { grid[p.x][p.y] = color } return grid }