给你一个大小为 m x n
的整数矩阵 grid
,表示一个网格。另给你三个整数 row
、col
和 color
。网格中的每个值表示该位置处的网格块的颜色。
两个网格块属于同一 连通分量 需满足下述全部条件:
- 两个网格块颜色相同
- 在上、下、左、右任意一个方向上相邻
连通分量的边界 是指连通分量中满足下述条件之一的所有网格块:
- 在上、下、左、右任意一个方向上与不属于同一连通分量的网格块相邻
- 在网格的边界上(第一行/列或最后一行/列)
请你使用指定颜色 color
为所有包含网格块 grid[row][col]
的 连通分量的边界 进行着色,并返回最终的网格 grid
。
示例 1:
输入:grid = [[1,1],[1,2]], row = 0, col = 0, color = 3 输出:[[3,3],[3,2]]
示例 2:
输入:grid = [[1,2,2],[2,3,2]], row = 0, col = 1, color = 3 输出:[[1,3,3],[2,3,3]]
示例 3:
输入:grid = [[1,1,1],[1,1,1],[1,1,1]], row = 1, col = 1, color = 2 输出:[[2,2,2],[2,1,2],[2,2,2]]
提示:
m == grid.length
n == grid[i].length
1 <= m, n <= 50
1 <= grid[i][j], color <= 1000
0 <= row < m
0 <= col < n
深度优先搜索,利用 vis 记录访问过的位置。
class Solution:
def colorBorder(
self, grid: List[List[int]], row: int, col: int, color: int
) -> List[List[int]]:
m, n = len(grid), len(grid[0])
vis = [[False] * n for _ in range(m)]
def dfs(i, j, color):
vis[i][j] = True
old_color = grid[i][j]
for a, b in [[-1, 0], [1, 0], [0, -1], [0, 1]]:
x, y = a + i, b + j
if 0 <= x < m and 0 <= y < n:
if not vis[x][y]:
if grid[x][y] == old_color:
dfs(x, y, color)
else:
grid[i][j] = color
else:
grid[i][j] = color
dfs(row, col, color)
return grid
class Solution {
private int[] dirs = new int[] {-1, 0, 1, 0, -1};
public int[][] colorBorder(int[][] grid, int r0, int c0, int color) {
boolean[][] vis = new boolean[grid.length][grid[0].length];
dfs(grid, r0, c0, color, vis);
return grid;
}
private void dfs(int[][] grid, int i, int j, int color, boolean[][] vis) {
vis[i][j] = true;
int oldColor = grid[i][j];
for (int k = 0; k < 4; ++k) {
int x = i + dirs[k], y = j + dirs[k + 1];
if (x >= 0 && x < grid.length && y >= 0 && y < grid[0].length) {
if (!vis[x][y]) {
if (grid[x][y] == oldColor) {
dfs(grid, x, y, color, vis);
} else {
grid[i][j] = color;
}
}
} else {
grid[i][j] = color;
}
}
}
}
class Solution {
public:
int m, n;
vector<vector<int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
vector<vector<int>> colorBorder(vector<vector<int>>& grid, int row, int col, int color) {
m = grid.size();
n = grid[0].size();
vector<vector<bool>> vis(m, vector<bool>(n, false));
dfs(row, col, color, grid, vis);
return grid;
}
void dfs(int i, int j, int color, vector<vector<int>>& grid, vector<vector<bool>>& vis) {
vis[i][j] = true;
int oldColor = grid[i][j];
for (auto& dir : dirs) {
int x = i + dir[0], y = j + dir[1];
if (x >= 0 && x < m && y >= 0 && y < n) {
if (!vis[x][y]) {
if (grid[x][y] == oldColor)
dfs(x, y, color, grid, vis);
else
grid[i][j] = color;
}
} else
grid[i][j] = color;
}
}
};
func colorBorder(grid [][]int, row int, col int, color int) [][]int {
m, n := len(grid), len(grid[0])
vis := make([][]bool, m)
for i := 0; i < m; i++ {
vis[i] = make([]bool, n)
}
dirs := [4][2]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}}
var dfs func(i, j, color int)
dfs = func(i, j, color int) {
vis[i][j] = true
oldColor := grid[i][j]
for _, dir := range dirs {
x, y := i+dir[0], j+dir[1]
if x >= 0 && x < m && y >= 0 && y < n {
if !vis[x][y] {
if grid[x][y] == oldColor {
dfs(x, y, color)
} else {
grid[i][j] = color
}
}
} else {
grid[i][j] = color
}
}
}
dfs(row, col, color)
return grid
}