LeetCode 1034 边界着色

给你一个大小为 m x n 的整数矩阵 grid ,表示一个网格。另给你三个整数 rowcolcolor 。网格中的每个值表示该位置处的网格块的颜色。

当两个网格块的颜色相同,而且在四个方向中任意一个方向上相邻时,它们属于同一 连通分量

连通分量的边界 是指连通分量中的所有与不在分量中的网格块相邻(四个方向上)的所有网格块,或者在网格的边界上(第一行/列或最后一行/列)的所有网格块。

请你使用指定颜色 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

 

Related Topics
  • 深度优先搜索
  • 广度优先搜索
  • 数组
  • 矩阵
  • \n
  • 👍 91
  • 👎 0
  • 深度优先
    /**
     * DFS
     *
     * @param grid
     * @param row
     * @param col
     * @param color
     * @return
     */
    public static int[][] colorBorder(int[][] grid, int row, int col, int color) {
        if (grid == null || grid.length == 0 || grid[0].length == 0) {
            return new int[][]{};
        }
    
        // 判断是否为边界
        int rows = grid.length;
        int cols = grid[0].length;
    
        // 表示该位置是否为边界
        boolean[][] colorFlag = new boolean[rows][cols];
        // 表示该位置是否已遍历
        boolean[][] flag = new boolean[rows][cols];
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                colorFlag[i][j] = false;
                flag[i][j] = false;
            }
        }
    
        int target = grid[row][col];
        dfs(grid, target, row, col, colorFlag, flag);
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (colorFlag[i][j] == true) {
                    grid[i][j] = color;
                }
            }
        }
        return grid;
    }
    
    private static void dfs(int[][] grid, int target,
                            int i, int j,
                            boolean[][] colorFlag,
                            boolean[][] flag) {
    
        if (i < 0 || i >= grid.length
                || j < 0 || j >= grid[0].length
                || grid[i][j] != target || flag[i][j]) {
            return;
        }
        flag[i][j] = true;
        colorFlag[i][j] = true;
        if (i > 0) {
            dfs(grid, target, i - 1, j, colorFlag, flag);
        }
        if (i < grid.length - 1) {
            dfs(grid, target, i + 1, j, colorFlag, flag);
        }
        if (j > 0) {
            dfs(grid, target, i, j - 1, colorFlag, flag);
        }
        if (j < grid[0].length - 1) {
            dfs(grid, target, i, j + 1, colorFlag, flag);
        }
    
        flag[i][j] = false;
        isNotBoard(grid, target, i, j, colorFlag);
        return;
    }
    
    /**
     * 是否是边界:主要问题在于边界的判断
     *
     * @param row
     * @param col
     * @param colorFlag
     * @return
     */
    private static void isNotBoard(int[][] grid, int target,
                                   int row, int col, boolean[][] colorFlag) {
    
        /**
         * 网格第一行/列或最后一行/列都是边界
         */
        if (row == 0 || row == colorFlag.length - 1
                || col == 0 || col == colorFlag[0].length - 1) {
            colorFlag[row][col] = true;
            return;
        }
        /**
         * 4个方向相邻的都是边界或者都不是边界,说明该位置不是边界
         * 其他情况说明该位置是边界
         */
        boolean flag = (grid[row - 1][col] == target) && (grid[row][col - 1] == target)
                && (grid[row + 1][col] == target) && (grid[row][col + 1] == target);
        colorFlag[row][col] = !flag;
    }
    
    测试用例
    public static void main(String[] args) {
        int[][] grid = new int[][]{{1, 1}, {1, 2}};
        int row = 0, col = 0;
        int color = 3;
        int[][] colorBoard = ColorBorder.colorBorder(grid, row, col, color);
        System.out.print("ColorBorder demo01 result : [");
        for (int i = 0; i < colorBoard.length; i++) {
            System.out.print("[");
            for (int j = 0; j < colorBoard[0].length; j++) {
                System.out.print("," + colorBoard[i][j]);
            }
            System.out.print("]");
        }
        System.out.println("]");
    
        grid = new int[][]{{1, 2, 2}, {2, 3, 2}};
        row = 0;
        col = 1;
        color = 3;
        colorBoard = ColorBorder.colorBorder(grid, row, col, color);
        System.out.print("ColorBorder demo02 result : [");
        for (int i = 0; i < colorBoard.length; i++) {
            System.out.print("[");
            for (int j = 0; j < colorBoard[0].length; j++) {
                System.out.print("," + colorBoard[i][j]);
            }
            System.out.print("]");
        }
        System.out.println("]");
    
        grid = new int[][]{{1, 1, 1}, {1, 1, 1}, {1, 1, 1}};
        row = 1;
        col = 1;
        color = 2;
        colorBoard = ColorBorder.colorBorder(grid, row, col, color);
        System.out.print("ColorBorder demo03 result : [");
        for (int i = 0; i < colorBoard.length; i++) {
            System.out.print("[");
            for (int j = 0; j < colorBoard[0].length; j++) {
                System.out.print("," + colorBoard[i][j]);
            }
            System.out.print("]");
        }
        System.out.println("]");
    
        grid = new int[][]{{1, 2, 1, 2, 1, 2}, {2, 2, 2, 2, 1, 2}, {1, 2, 2, 2, 1, 2}};
        row = 1;
        col = 3;
        color = 1;
        colorBoard = ColorBorder.colorBorder(grid, row, col, color);
        System.out.print("ColorBorder demo04 result : [");
        for (int i = 0; i < colorBoard.length; i++) {
            System.out.print("[");
            for (int j = 0; j < colorBoard[0].length; j++) {
                System.out.print("," + colorBoard[i][j]);
            }
            System.out.print("]");
        }
        System.out.println("]");
    
        grid = new int[][]{{1, 2, 2}, {2, 1, 2}, {1, 2, 3}, {3, 2, 1}};
        row = 1;
        col = 1;
        color = 3;
        colorBoard = ColorBorder.colorBorder(grid, row, col, color);
        System.out.print("ColorBorder demo05 result : [");
        for (int i = 0; i < colorBoard.length; i++) {
            System.out.print("[");
            for (int j = 0; j < colorBoard[0].length; j++) {
                System.out.print("," + colorBoard[i][j]);
            }
            System.out.print("]");
        }
        System.out.println("]");
    
        grid = new int[][]{{2, 1, 3, 2, 1, 1, 2},
                {1, 2, 3, 1, 2, 1, 2},
                {1, 2, 1, 2, 2, 2, 2},
                {2, 1, 2, 2, 2, 2, 2},
                {2, 3, 3, 3, 2, 1, 2}};
        row = 4;
        col = 4;
        color = 3;
        //{{2,1,3,2,1,1,3},{1,2,3,1,3,1,3},{1,2,1,3,2,3,3},{2,1,3,3,2,3,3},{2,3,3,3,3,1,3}}
        colorBoard = ColorBorder.colorBorder(grid, row, col, color);
        System.out.print("ColorBorder demo06 result : [");
        for (int i = 0; i < colorBoard.length; i++) {
            System.out.print("[");
            for (int j = 0; j < colorBoard[0].length; j++) {
                System.out.print("," + colorBoard[i][j]);
            }
            System.out.print("]");
        }
        System.out.println("]");
    }
    
    运行结果
    ColorBorder demo01 result : [[,3,3][,3,2]]
    ColorBorder demo02 result : [[,1,3,3][,2,3,3]]
    ColorBorder demo03 result : [[,2,2,2][,2,1,2][,2,2,2]]
    ColorBorder demo04 result : [[,1,1,1,1,1,2][,1,2,1,1,1,2][,1,1,1,1,1,2]]
    ColorBorder demo05 result : [[,1,2,2][,2,3,2][,1,2,3][,3,2,1]]
    ColorBorder demo06 result : [[,2,1,3,2,1,1,3][,1,2,3,1,3,1,3][,1,2,1,3,2,3,3][,2,1,3,3,2,3,3][,2,3,3,3,3,1,3]]
    
    posted @ 2021-12-07 14:22  枫叶艾辰  阅读(40)  评论(0编辑  收藏  举报