From 6a2aaa8d737479962c108c0553439c019e7b1c53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BD=A9=E8=BE=95=E9=BE=99=E5=84=BF?= Date: Thu, 9 Feb 2023 10:26:53 +0800 Subject: [PATCH] =?UTF-8?q?1034:=E8=BE=B9=E7=95=8C=E7=9D=80=E8=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leetcode/editor/cn/ColoringABorder.java | 112 ++++++++++++++++++ .../editor/cn/doc/content/ColoringABorder.md | 54 +++++++++ 2 files changed, 166 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/ColoringABorder.java create mode 100644 src/main/java/leetcode/editor/cn/doc/content/ColoringABorder.md diff --git a/src/main/java/leetcode/editor/cn/ColoringABorder.java b/src/main/java/leetcode/editor/cn/ColoringABorder.java new file mode 100644 index 0000000..060d4e3 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/ColoringABorder.java @@ -0,0 +1,112 @@ +//

给你一个大小为 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]]
+// +//

 

+// +//

提示:

+// +// +// +//

 

+// +//
Related Topics
  • 深度优先搜索
  • 广度优先搜索
  • 数组
  • 矩阵

  • 👍 159
  • 👎 0
  • +package leetcode.editor.cn; + +import java.util.LinkedList; +import java.util.Queue; + +// 1034:边界着色 +public class ColoringABorder { + public static void main(String[] args) { + Solution solution = new ColoringABorder().new Solution(); + // TO TEST + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int[][] colorBorder(int[][] grid, int row, int col, int color) { + int xLength = grid.length; + int yLength = grid[0].length; + boolean[][] use = new boolean[xLength][yLength]; + int[][] res = new int[xLength][yLength]; + Queue queue = new LinkedList<>(); + queue.add(new int[]{row, col}); + use[row][col] = true; + int[] xArr = new int[]{-1, 1, 0, 0}; + int[] yArr = new int[]{0, 0, -1, 1}; + int ori = grid[row][col]; + while (!queue.isEmpty()) { + int[] point = queue.poll(); + int num = 0; + for (int i = 0; i < 4; i++) { + int x = point[0] + xArr[i]; + int y = point[1] + yArr[i]; + if (x >= xLength || x < 0 || y >= yLength || y < 0 + || grid[x][y] != ori) { + continue; + } + num++; + if (!use[x][y]) { + use[x][y] = true; + queue.add(new int[]{x, y}); + } + } + if (num < 4) { + res[point[0]][point[1]] = color; + } + } + for (int i = 0; i < xLength; i++) { + for (int j = 0; j < yLength; j++) { + if (res[i][j] == 0) { + res[i][j] = grid[i][j]; + } + } + } + return res; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} diff --git a/src/main/java/leetcode/editor/cn/doc/content/ColoringABorder.md b/src/main/java/leetcode/editor/cn/doc/content/ColoringABorder.md new file mode 100644 index 0000000..8251a78 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/doc/content/ColoringABorder.md @@ -0,0 +1,54 @@ +

    给你一个大小为 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]]
    + +

     

    + +

    提示:

    + + + +

     

    + +
    Related Topics
  • 深度优先搜索
  • 广度优先搜索
  • 数组
  • 矩阵

  • 👍 159
  • 👎 0
  • \ No newline at end of file