From f51fdcfe2b6b2d3171082aaf8a2a481259cd6c3a Mon Sep 17 00:00:00 2001 From: tphyhFighting <2363176358@qq.com> Date: Tue, 7 Dec 2021 17:13:10 +0800 Subject: [PATCH 1/4] add: leetcode 1034 solution --- .../1034.Coloring A Border.go | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 leetcode/1034.Coloring-A-Border/1034.Coloring A Border.go diff --git a/leetcode/1034.Coloring-A-Border/1034.Coloring A Border.go b/leetcode/1034.Coloring-A-Border/1034.Coloring A Border.go new file mode 100644 index 00000000..239ff306 --- /dev/null +++ b/leetcode/1034.Coloring-A-Border/1034.Coloring A Border.go @@ -0,0 +1,53 @@ +package leetcode + +type point struct { + x int + y int +} + +var ( + borders []point + m int + n int + vis [][]bool + dirs []point + q []point + originalColor int +) + +func colorBorder(grid [][]int, row, col, color int) [][]int { + m, n = len(grid), len(grid[0]) + vis = make([][]bool, m) + for i := range vis { + vis[i] = make([]bool, n) + } + dirs = []point{{1, 0}, {-1, 0}, {0, 1}, {0, -1}} + originalColor = grid[row][col] + borders = []point{} + q = []point{{row, col}} + bfs(q, grid) + for _, p := range borders { + grid[p.x][p.y] = color + } + return grid +} + +func bfs(q []point, grid [][]int) { + for len(q) != 0 { + ele := q[0] + vis[ele.x][ele.y] = true + q = q[1:] + isBorder := false + for _, dir := range dirs { + nx, ny := ele.x+dir.x, ele.y+dir.y + if !(0 <= nx && nx < m && 0 <= ny && ny < n && grid[nx][ny] == originalColor) { + isBorder = true + } else if !vis[nx][ny] { + q = append(q, point{nx, ny}) + } + } + if isBorder { + borders = append(borders, point{ele.x, ele.y}) + } + } +} From 789c49065caec588a3c24c141b43ef7ee192b415 Mon Sep 17 00:00:00 2001 From: tphyhFighting <2363176358@qq.com> Date: Tue, 7 Dec 2021 17:13:19 +0800 Subject: [PATCH 2/4] add: leetcode 1034 test --- .../1034.Coloring A Border_test.go | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 leetcode/1034.Coloring-A-Border/1034.Coloring A Border_test.go diff --git a/leetcode/1034.Coloring-A-Border/1034.Coloring A Border_test.go b/leetcode/1034.Coloring-A-Border/1034.Coloring A Border_test.go new file mode 100644 index 00000000..812a9c24 --- /dev/null +++ b/leetcode/1034.Coloring-A-Border/1034.Coloring A Border_test.go @@ -0,0 +1,53 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question1034 struct { + para1034 + ans1034 +} + +// para 是参数 +type para1034 struct { + grid [][]int + row int + col int + color int +} + +// ans 是答案 +type ans1034 struct { + ans [][]int +} + +func Test_Problem1034(t *testing.T) { + + qs := []question1034{ + + { + para1034{[][]int{{1, 1}, {1, 2}}, 0, 0, 3}, + ans1034{[][]int{{3, 3}, {3, 2}}}, + }, + + { + para1034{[][]int{{1, 2, 2}, {2, 3, 2}}, 0, 1, 3}, + ans1034{[][]int{{1, 3, 3}, {2, 3, 3}}}, + }, + + { + para1034{[][]int{{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}, 1, 1, 2}, + ans1034{[][]int{{2, 2, 2}, {2, 1, 2}, {2, 2, 2}}}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 1034------------------------\n") + + for _, q := range qs { + _, p := q.ans1034, q.para1034 + fmt.Printf("【input】:%v 【output】:%v\n", p, colorBorder(p.grid, p.row, p.col, p.color)) + } + fmt.Printf("\n\n\n") +} From bda9f48034ffa524d7b215ad824b6ded0cb09b1a Mon Sep 17 00:00:00 2001 From: tphyhFighting <2363176358@qq.com> Date: Tue, 7 Dec 2021 17:13:34 +0800 Subject: [PATCH 3/4] add: leetcode 1034 readme --- leetcode/1034.Coloring-A-Border/README.md | 111 ++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 leetcode/1034.Coloring-A-Border/README.md diff --git a/leetcode/1034.Coloring-A-Border/README.md b/leetcode/1034.Coloring-A-Border/README.md new file mode 100644 index 00000000..8821b455 --- /dev/null +++ b/leetcode/1034.Coloring-A-Border/README.md @@ -0,0 +1,111 @@ +# [1034. Coloring A Border](https://leetcode-cn.com/problems/coloring-a-border/) + +## 题目 + +You are given an m x n integer matrix grid, and three integers row, col, and color. Each value in the grid represents the color of the grid square at that location. + +Two squares belong to the same connected component if they have the same color and are next to each other in any of the 4 directions. + +The border of a connected component is all the squares in the connected component that are either 4-directionally adjacent to a square not in the component, or on the boundary of the grid (the first or last row or column). + +You should color the border of the connected component that contains the square grid[row][col] with color. + +Return the final grid. + +**Example 1**: + + Input: grid = [[1,1],[1,2]], row = 0, col = 0, color = 3 + Output: [[3,3],[3,2]] + +**Example 2**: + + Input: grid = [[1,2,2],[2,3,2]], row = 0, col = 1, color = 3 + Output: [[1,3,3],[2,3,3]] + +**Example 3**: + + Input: grid = [[1,1,1],[1,1,1],[1,1,1]], row = 1, col = 1, color = 2 + Output: [[2,2,2],[2,1,2],[2,2,2]] + +**Constraints:** + +- m == grid.length +- n == grid[i].length +- 1 <= m, n <= 50 +- 1 <= grid[i][j], color <= 1000 +- 0 <= row < m +- 0 <= col < n + +## 题目大意 + +给你一个大小为 m x n 的整数矩阵 grid ,表示一个网格。另给你三个整数 row、col 和 color 。网格中的每个值表示该位置处的网格块的颜色。 + +当两个网格块的颜色相同,而且在四个方向中任意一个方向上相邻时,它们属于同一连通分量 + +边界:在连通分量的块中(前提)并且满足以下条件之一: +(1)要么上下左右存在一个块不在连通分量里面 +(2)要么这个块的位置在整个grid的边框上 + +请你使用指定颜色 color 为所有包含网格块 grid[row][col] 的连通分量的边界进行着色,并返回最终的网格 grid 。 + +## 解题思路 + +- 用bfs进行遍历选出边界,使用color给边界着色 + +## 代码 + +```go +package leetcode + +type point struct { + x int + y int +} + +var ( + borders []point + m int + n int + vis [][]bool + dirs []point + q []point + originalColor int +) + +func colorBorder(grid [][]int, row, col, color int) [][]int { + m, n = len(grid), len(grid[0]) + vis = make([][]bool, m) + for i := range vis { + vis[i] = make([]bool, n) + } + dirs = []point{{1, 0}, {-1, 0}, {0, 1}, {0, -1}} + originalColor = grid[row][col] + borders = []point{} + q = []point{{row, col}} + bfs(q, grid) + for _, p := range borders { + grid[p.x][p.y] = color + } + return grid +} + +func bfs(q []point, grid [][]int) { + for len(q) != 0 { + ele := q[0] + vis[ele.x][ele.y] = true + q = q[1:] + isBorder := false + for _, dir := range dirs { + nx, ny := ele.x+dir.x, ele.y+dir.y + if !(0 <= nx && nx < m && 0 <= ny && ny < n && grid[nx][ny] == originalColor) { + isBorder = true + } else if !vis[nx][ny] { + q = append(q, point{nx, ny}) + } + } + if isBorder { + borders = append(borders, point{ele.x, ele.y}) + } + } +} +``` \ No newline at end of file From 94c703c2d5f2a20c5e5ed76a68c29a7ac59d818c Mon Sep 17 00:00:00 2001 From: tphyhFighting <2363176358@qq.com> Date: Thu, 9 Dec 2021 12:53:23 +0800 Subject: [PATCH 4/4] update: leetcode 1034 solution && readme --- .../1034.Coloring A Border.go | 55 +++++++++---------- leetcode/1034.Coloring-A-Border/README.md | 55 +++++++++---------- 2 files changed, 52 insertions(+), 58 deletions(-) diff --git a/leetcode/1034.Coloring-A-Border/1034.Coloring A Border.go b/leetcode/1034.Coloring-A-Border/1034.Coloring A Border.go index 239ff306..70df28ed 100644 --- a/leetcode/1034.Coloring-A-Border/1034.Coloring A Border.go +++ b/leetcode/1034.Coloring-A-Border/1034.Coloring A Border.go @@ -5,49 +5,46 @@ type point struct { y int } -var ( - borders []point +type gridInfo struct { m int n int - vis [][]bool - dirs []point - q []point + grid [][]int originalColor int -) +} func colorBorder(grid [][]int, row, col, color int) [][]int { - m, n = len(grid), len(grid[0]) - vis = make([][]bool, m) + m, n := len(grid), len(grid[0]) + dirs := []point{{1, 0}, {-1, 0}, {0, 1}, {0, -1}} + vis := make([][]bool, m) for i := range vis { vis[i] = make([]bool, n) } - dirs = []point{{1, 0}, {-1, 0}, {0, 1}, {0, -1}} - originalColor = grid[row][col] - borders = []point{} - q = []point{{row, col}} - bfs(q, grid) + var borders []point + gInfo := gridInfo{ + m: m, + n: n, + grid: grid, + originalColor: grid[row][col], + } + dfs(row, col, gInfo, dirs, vis, &borders) for _, p := range borders { grid[p.x][p.y] = color } return grid } -func bfs(q []point, grid [][]int) { - for len(q) != 0 { - ele := q[0] - vis[ele.x][ele.y] = true - q = q[1:] - isBorder := false - for _, dir := range dirs { - nx, ny := ele.x+dir.x, ele.y+dir.y - if !(0 <= nx && nx < m && 0 <= ny && ny < n && grid[nx][ny] == originalColor) { - isBorder = true - } else if !vis[nx][ny] { - q = append(q, point{nx, ny}) - } - } - if isBorder { - borders = append(borders, point{ele.x, ele.y}) +func dfs(x, y int, gInfo gridInfo, dirs []point, vis [][]bool, borders *[]point) { + vis[x][y] = true + isBorder := false + for _, dir := range dirs { + nx, ny := x+dir.x, y+dir.y + if !(0 <= nx && nx < gInfo.m && 0 <= ny && ny < gInfo.n && gInfo.grid[nx][ny] == gInfo.originalColor) { + isBorder = true + } else if !vis[nx][ny] { + dfs(nx, ny, gInfo, dirs, vis, borders) } } + if isBorder { + *borders = append(*borders, point{x, y}) + } } diff --git a/leetcode/1034.Coloring-A-Border/README.md b/leetcode/1034.Coloring-A-Border/README.md index 8821b455..dfb01180 100644 --- a/leetcode/1034.Coloring-A-Border/README.md +++ b/leetcode/1034.Coloring-A-Border/README.md @@ -62,50 +62,47 @@ type point struct { y int } -var ( - borders []point +type gridInfo struct { m int n int - vis [][]bool - dirs []point - q []point + grid [][]int originalColor int -) +} func colorBorder(grid [][]int, row, col, color int) [][]int { - m, n = len(grid), len(grid[0]) - vis = make([][]bool, m) + m, n := len(grid), len(grid[0]) + dirs := []point{{1, 0}, {-1, 0}, {0, 1}, {0, -1}} + vis := make([][]bool, m) for i := range vis { vis[i] = make([]bool, n) } - dirs = []point{{1, 0}, {-1, 0}, {0, 1}, {0, -1}} - originalColor = grid[row][col] - borders = []point{} - q = []point{{row, col}} - bfs(q, grid) + var borders []point + gInfo := gridInfo{ + m: m, + n: n, + grid: grid, + originalColor: grid[row][col], + } + dfs(row, col, gInfo, dirs, vis, &borders) for _, p := range borders { grid[p.x][p.y] = color } return grid } -func bfs(q []point, grid [][]int) { - for len(q) != 0 { - ele := q[0] - vis[ele.x][ele.y] = true - q = q[1:] - isBorder := false - for _, dir := range dirs { - nx, ny := ele.x+dir.x, ele.y+dir.y - if !(0 <= nx && nx < m && 0 <= ny && ny < n && grid[nx][ny] == originalColor) { - isBorder = true - } else if !vis[nx][ny] { - q = append(q, point{nx, ny}) - } - } - if isBorder { - borders = append(borders, point{ele.x, ele.y}) +func dfs(x, y int, gInfo gridInfo, dirs []point, vis [][]bool, borders *[]point) { + vis[x][y] = true + isBorder := false + for _, dir := range dirs { + nx, ny := x+dir.x, y+dir.y + if !(0 <= nx && nx < gInfo.m && 0 <= ny && ny < gInfo.n && gInfo.grid[nx][ny] == gInfo.originalColor) { + isBorder = true + } else if !vis[nx][ny] { + dfs(nx, ny, gInfo, dirs, vis, borders) } } + if isBorder { + *borders = append(*borders, point{x, y}) + } } ``` \ No newline at end of file