From 5d8ecc32ed0ed771949e1e541304ad38d0550429 Mon Sep 17 00:00:00 2001 From: YDZ Date: Thu, 8 Aug 2019 12:01:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20problem=20733?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0733. Flood Fill/733. Flood Fill.go | 23 ++++++++ .../0733. Flood Fill/733. Flood Fill_test.go | 53 +++++++++++++++++++ Algorithms/0733. Flood Fill/README.md | 50 +++++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 Algorithms/0733. Flood Fill/733. Flood Fill.go create mode 100644 Algorithms/0733. Flood Fill/733. Flood Fill_test.go create mode 100755 Algorithms/0733. Flood Fill/README.md diff --git a/Algorithms/0733. Flood Fill/733. Flood Fill.go b/Algorithms/0733. Flood Fill/733. Flood Fill.go new file mode 100644 index 00000000..6782310b --- /dev/null +++ b/Algorithms/0733. Flood Fill/733. Flood Fill.go @@ -0,0 +1,23 @@ +package leetcode + +func floodFill(image [][]int, sr int, sc int, newColor int) [][]int { + color := image[sr][sc] + if newColor == color { + return image + } + dfs733(image, sr, sc, newColor) + return image +} + +func dfs733(image [][]int, x, y int, newColor int) { + if image[x][y] == newColor { + return + } + oldColor := image[x][y] + image[x][y] = newColor + for i := 0; i < 4; i++ { + if (x+dir[i][0] >= 0 && x+dir[i][0] < len(image)) && (y+dir[i][1] >= 0 && y+dir[i][1] < len(image[0])) && image[x+dir[i][0]][y+dir[i][1]] == oldColor { + dfs733(image, x+dir[i][0], y+dir[i][1], newColor) + } + } +} diff --git a/Algorithms/0733. Flood Fill/733. Flood Fill_test.go b/Algorithms/0733. Flood Fill/733. Flood Fill_test.go new file mode 100644 index 00000000..3f1c706c --- /dev/null +++ b/Algorithms/0733. Flood Fill/733. Flood Fill_test.go @@ -0,0 +1,53 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question733 struct { + para733 + ans733 +} + +// para 是参数 +// one 代表第一个参数 +type para733 struct { + one [][]int + sr int + sc int + c int +} + +// ans 是答案 +// one 代表第一个答案 +type ans733 struct { + one [][]int +} + +func Test_Problem733(t *testing.T) { + + qs := []question733{ + + question733{ + para733{[][]int{ + []int{1, 1, 1}, + []int{1, 1, 0}, + []int{1, 0, 1}, + }, 1, 1, 2}, + ans733{[][]int{ + []int{2, 2, 2}, + []int{2, 2, 0}, + []int{2, 0, 1}, + }}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 733------------------------\n") + + for _, q := range qs { + _, p := q.ans733, q.para733 + fmt.Printf("【input】:%v 【output】:%v\n", p, floodFill(p.one, p.sr, p.sc, p.c)) + } + fmt.Printf("\n\n\n") +} diff --git a/Algorithms/0733. Flood Fill/README.md b/Algorithms/0733. Flood Fill/README.md new file mode 100755 index 00000000..740e7faa --- /dev/null +++ b/Algorithms/0733. Flood Fill/README.md @@ -0,0 +1,50 @@ +# [733. Flood Fill](https://leetcode.com/problems/flood-fill/) + + +## 题目: + +An `image` is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535). + +Given a coordinate `(sr, sc)` representing the starting pixel (row and column) of the flood fill, and a pixel value `newColor`, "flood fill" the image. + +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 as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor. + +At the end, return the modified image. + +**Example 1:** + + Input: + image = [[1,1,1],[1,1,0],[1,0,1]] + sr = 1, sc = 1, newColor = 2 + Output: [[2,2,2],[2,2,0],[2,0,1]] + Explanation: + From the center of the image (with position (sr, sc) = (1, 1)), all pixels connected + by a path of the same color as the starting pixel are colored with the new color. + Note the bottom corner is not colored 2, because it is not 4-directionally connected + to the starting pixel. + +**Note:** + +- The length of `image` and `image[0]` will be in the range `[1, 50]`. +- The given starting pixel will satisfy `0 <= sr < image.length` and `0 <= sc < image[0].length`. +- The value of each color in `image[i][j]` and `newColor` will be an integer in `[0, 65535]`. + + +## 题目大意 + +有一幅以二维整数数组表示的图画,每一个整数表示该图画的像素值大小,数值在 0 到 65535 之间。给你一个坐标 (sr, sc) 表示图像渲染开始的像素值(行 ,列)和一个新的颜色值 newColor,让你重新上色这幅图像。 + +为了完成上色工作,从初始坐标开始,记录初始坐标的上下左右四个方向上像素值与初始坐标相同的相连像素点,接着再记录这四个方向上符合条件的像素点与他们对应四个方向上像素值与初始坐标相同的相连像素点,……,重复该过程。将所有有记录的像素点的颜色值改为新的颜色值。最后返回经过上色渲染后的图像。 + +注意: + +- image 和 image[0] 的长度在范围 [1, 50] 内。 +- 给出的初始点将满足 0 <= sr < image.length 和 0 <= sc < image[0].length。 +- image[i][j] 和 newColor 表示的颜色值在范围 [0, 65535]内。 + + +## 解题思路 + + +- 给出一个二维的图片点阵,每个点阵都有一个数字。给出一个起点坐标,要求从这个起点坐标开始,把所有与这个起点连通的点都染色成 newColor。 +- 这一题是标准的 Flood Fill 算法。可以用 DFS 也可以用 BFS 。