Skip to content

Commit 1035969

Browse files
committed
Merge pull request #217 from 0xff-dev/master
Add solution and test-cases for problem 733
2 parents bab630b + 0209962 commit 1035969

File tree

4 files changed

+57
-34
lines changed

4 files changed

+57
-34
lines changed

leetcode/701-800/0733.Flood-Fill/README.md

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
# [733.Flood Fill][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
An image is represented by an `m x n` integer grid `image` where `image[i][j]` represents the pixel value of the image.
75

8-
**Example 1:**
6+
You are also given three integers `sr`, `sc`, and `newColor`. You should perform a __flood fill__ on the image starting from the pixel `image[sr][sc]`.
97

10-
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
8+
To perform a __flood fill__, consider the starting pixel, plus any pixels connected __4-directionally__ to the starting pixel of the same color as the starting pixel, plus any pixels connected __4-directionally__ to those pixels (also with the same color), and so on. Replace the color of all of the aforementioned pixels with `newColor`.
9+
10+
Return the modified image after performing the flood fill.
1411

15-
## 题意
16-
> ...
1712

18-
## 题解
1913

20-
### 思路1
21-
> ...
22-
Flood Fill
23-
```go
14+
**Example 1:**
15+
16+
![flood1-grid](flood1-grid.jpg)
17+
18+
```
19+
Input: image = [[1,1,1],[1,1,0],[1,0,1]], sr = 1, sc = 1, newColor = 2
20+
Output: [[2,2,2],[2,2,0],[2,0,1]]
21+
Explanation: From the center of the image with position (sr, sc) = (1, 1) (i.e., the red pixel), all pixels connected by a path of the same color as the starting pixel (i.e., the blue pixels) are colored with the new color.
22+
Note the bottom corner is not colored 2, because it is not 4-directionally connected to the starting pixel.
2423
```
2524

25+
__Example 2:__
26+
```
27+
Input: image = [[0,0,0],[0,0,0]], sr = 0, sc = 0, newColor = 2
28+
Output: [[2,2,2],[2,2,2]]
29+
```
2630

2731
## 结语
2832

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
var localDirs = [][2]int{{0, -1}, {0, 1}, {-1, 0}, {1, 0}}
4+
5+
func floodFillDFS(image, visited [][]int, sr, sc, rows, cols int, sourceColor, newColor int) {
6+
if sr < 0 || sr >= rows || sc < 0 || sc >= cols {
7+
return
8+
}
9+
10+
if visited[sr][sc] > 0 || image[sr][sc] != sourceColor {
11+
return
12+
}
13+
14+
image[sr][sc] = newColor
15+
visited[sr][sc] = 1
16+
for _, dir := range localDirs {
17+
floodFillDFS(image, visited, sr+dir[0], sc+dir[1], rows, cols, sourceColor, newColor)
18+
}
19+
}
20+
21+
func Solution(image [][]int, sr, sc, newColor int) [][]int {
22+
rows, cols := len(image), len(image[0])
23+
visited := make([][]int, len(image))
24+
for idx := 0; idx < rows; idx++ {
25+
visited[idx] = make([]int, cols)
26+
}
27+
28+
floodFillDFS(image, visited, sr, sc, rows, cols, image[sr][sc], newColor)
29+
return image
530
}

leetcode/701-800/0733.Flood-Fill/Solution_test.go

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,25 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
inputs [][]int
14+
sr, sc, newColor int
15+
expect [][]int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", [][]int{{1, 1, 1}, {1, 1, 0}, {1, 0, 1}}, 1, 1, 2, [][]int{{2, 2, 2}, {2, 2, 0}, {2, 0, 1}}},
18+
{"TestCase2", [][]int{{1}}, 0, 0, 2, [][]int{{2}}},
19+
{"TestCase3", [][]int{{1, 1, 1}, {1, 0, 1}, {1, 1, 1}}, 1, 1, 2, [][]int{{1, 1, 1}, {1, 2, 1}, {1, 1, 1}}},
20+
{"TestCase4", [][]int{{0, 0, 0}, {0, 0, 0}}, 0, 0, 2, [][]int{{2, 2, 2}, {2, 2, 2}}},
1921
}
2022

2123
// 开始测试
2224
for i, c := range cases {
2325
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
26+
got := Solution(c.inputs, c.sr, c.sc, c.newColor)
2527
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
28+
t.Fatalf("expected: %v, but got: %v, with inputs-image: %v, input-sr: %v, input-sc: %v",
29+
c.expect, got, c.inputs, c.sr, c.sc)
2830
}
2931
})
3032
}
3133
}
32-
33-
// 压力测试
34-
func BenchmarkSolution(b *testing.B) {
35-
}
36-
37-
// 使用案列
38-
func ExampleSolution() {
39-
}
17.2 KB
Loading

0 commit comments

Comments
 (0)