把当前连通块的边界染成别的颜色
非常精妙的写法:在dfs后,判断是否是边界,不是边界的话改回去。
图解:https://leetcode.com/problems/coloring-a-border/discuss/282847/C%2B%2B-with-picture-DFS
public int[][] colorBorder(int[][] grid, int r0, int c0, int color) {
dfs(grid, r0, c0, grid[r0][c0]);
for (int i = 0; i < grid.length; i++)
for (int j = 0; j < grid[0].length; j++) if (grid[i][j] < 0) grid[i][j] = color;
return grid;
}
private void dfs(int[][] grid, int x, int y, int c) {
if (x < 0 || y < 0 || x >= grid.length || y >= grid[0].length || grid[x][y] != c) return;
grid[x][y] = -c;
dfs(grid, x + 1, y, c);
dfs(grid, x - 1, y, c);
dfs(grid, x, y + 1, c);
dfs(grid, x, y - 1, c);
if (x > 0 && y > 0 && x < grid.length - 1 && y < grid[0].length - 1 &&
Math.abs(grid[x - 1][y]) == c && Math.abs(grid[x + 1][y]) == c && Math.abs(grid[x][y + 1]) == c && Math.abs(grid[x][y - 1]) == c) {
grid[x][y] = c;
}
}
bfs写法:
需要注意,访问过的且是同一连通块的要用set存储起来,判断是否是边界的时候是判断不没访问过的。
因为访问过的如果是边界的话是别的颜色,可能会导致判断其他cell的时候边界出现错误。
private static final int[] d = { 0, 1, 0, -1, 0 }; // neighbors' relative displacements.
public int[][] colorBorder(int[][] grid, int r0, int c0, int color) {
int clr = grid[r0][c0], m = grid.length, n = grid[0].length;
Set<Integer> seen = new HashSet<>(); // put the cell number into Set to avoid visiting again.
Queue<int[]> q = new LinkedList<>();
q.offer(new int[]{ r0, c0 }); // add initial cell.
seen.add(r0 * n + c0); // add initial cell number.
while (!q.isEmpty()) { // BFS starts.
int r = q.peek()[0], c = q.poll()[1];
if (r == 0 || r == m - 1 || c == 0 || c == n - 1) { grid[r][c] = color; } // on grid boundary.
for (int i = 0; i < 4; ++i) { // travers its 4 neighbors.
int x = r + d[i], y = c + d[i + 1]; // neighbor coordinates.
if (x < 0 || x >= m || y < 0 || y >= n || seen.contains(x * n + y)) { continue; } // out of grid or visited already.
if (grid[x][y] == clr) { // its neighbor is of same color.
seen.add(x * n + y); // avoid visiting again.
q.offer(new int[]{ x, y }); // put it into Queue.
}else { // its neighbor is of different color, hence it is on component boundary.
grid[r][c] = color;
}
}
}
return grid;
}
dfs写法:
private int[] d = { 0, 1, 0, -1, 0 }; // neighbors' relative displacements.
public int[][] colorBorder(int[][] grid, int r0, int c0, int color) {
dfs(grid, r0, c0, grid[r0][c0]);
for (int[] g : grid) {
for (int i = 0; i < g.length; ++i) {
if (g[i] < 0) { g[i] = color; }
}
}
return grid;
}
private void dfs(int[][] grid, int r, int c, int clr) {
grid[r][c] = -clr; // mark as visited.
int cnt = 0; // use to count grid[r][c]'s component neighbors (same color as itself).
for (int i = 0; i < 4; ++i) { // traverse 4 neighbors.
int x = r + d[i], y = c + d[i + 1]; // nieghbor's coordinates.
if (x < 0 || x >= grid.length || y < 0 || y >= grid[0].length || Math.abs(grid[x][y]) != clr) { continue; } // out of grid or not same component.
++cnt; // only if all 4 neighbors of grid[r][c] have same color as itself, it is on inner part.
if (grid[x][y] == clr) { dfs(grid, x, y, clr); }
}
if (cnt == 4) { grid[r][c] = clr; } // inner part, change back.
}