Skip to content

Commit 1733a91

Browse files
committed
refactor Flood Fill
1 parent 9870992 commit 1733a91

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

go/flood_fill.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,16 @@ package main
33

44
func floodFill(image [][]int, sr int, sc int, color int) [][]int {
55
originalColor := image[sr][sc]
6-
76
if originalColor == color {
87
return image
98
}
109
image[sr][sc] = color
11-
12-
directions := [][]int{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}
13-
for _, d := range directions {
14-
nSr, nSc := sr+d[0], sc+d[1]
15-
if 0 <= nSr && nSr < len(image) &&
16-
0 <= nSc && nSc < len(image[0]) &&
17-
image[nSr][nSc] == originalColor {
18-
image = floodFill(image, nSr, nSc, color)
10+
directions := [][]int{{0, 1}, {0, -1}, {1, 0}, {-1, 0}}
11+
for _, direction := range directions {
12+
newSr, newSc := sr+direction[0], sc+direction[1]
13+
if 0 <= newSr && newSr < len(image) && 0 <= newSc && newSc < len(image[0]) && image[newSr][newSc] == originalColor {
14+
floodFill(image, newSr, newSc, color)
1915
}
2016
}
21-
2217
return image
2318
}

0 commit comments

Comments
 (0)