Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions leetcode/1001-1100/1034.Coloring-A-Border/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
# [1034.Coloring A Border][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
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 are called **adjacent** if they are next to each other in any of the 4 directions.

Two squares belong to the same **connected component** if they have the same color and they are adjacent.

The **border of a connected component** is all the squares in the connected component that are either adjacent to (at least) 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: a = "11", b = "1"
Output: "100"
Input: grid = [[1,1],[1,2]], row = 0, col = 0, color = 3
Output: [[3,3],[3,2]]
```

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Coloring A Border
```go
```
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]]
```

## 结语

Expand Down
38 changes: 36 additions & 2 deletions leetcode/1001-1100/1034.Coloring-A-Border/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
package Solution

func Solution(x bool) bool {
return x
var dirs1034 = [][2]int{
{1, 0}, {-1, 0}, {0, 1}, {0, -1},
}

func Solution(grid [][]int, row int, col int, color int) [][]int {
m, n := len(grid), len(grid[0])
initColor := grid[row][col]

queue := [][2]int{{row, col}}
v := map[[2]int]struct{}{
[2]int{row, col}: struct{}{},
}

pos := make([][2]int, 0)
for len(queue) > 0 {
nq := make([][2]int, 0)
for _, i := range queue {
for _, d := range dirs1034 {
nx, ny := i[0]+d[0], i[1]+d[1]
if nx < 0 || nx >= m || ny < 0 || ny >= n || grid[nx][ny] != initColor {
pos = append(pos, i)
continue
}
key := [2]int{nx, ny}
if _, ok := v[key]; !ok {
v[key] = struct{}{}
nq = append(nq, key)
}
}
}
queue = nq
}
for _, p := range pos {
grid[p[0]][p[1]] = color
}
return grid
}
23 changes: 12 additions & 11 deletions leetcode/1001-1100/1034.Coloring-A-Border/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,32 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
grid [][]int
row, col, color int
expect [][]int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", [][]int{{1, 1}, {1, 2}}, 0, 0, 3, [][]int{{3, 3}, {3, 2}}},
{"TestCase2", [][]int{{1, 2, 2}, {2, 3, 2}}, 0, 1, 3, [][]int{{1, 3, 3}, {2, 3, 3}}},
{"TestCase3", [][]int{{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}, 1, 1, 2, [][]int{{2, 2, 2}, {2, 1, 2}, {2, 2, 2}}},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.grid, c.row, c.col, c.color)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v %v",
c.expect, got, c.grid, c.row, c.col, c.color)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading