Leetcode1034

把当前连通块的边界染成别的颜色

 

非常精妙的写法:在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.
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值