1
\$\begingroup\$

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].

Please review for performance and coding style.

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ArrayQuestions
{
    /// <summary>
    /// https://leetcode.com/problems/flood-fill/
    /// </summary>
    [TestClass]
    public class FloodFillTest
    {
        [TestMethod]
        public void ExampleTest()
        {
            int[][] image = { new[] { 1, 1, 1 }, new[] { 1, 1, 0 }, new[] { 1, 0, 1 } };
            int sr = 1;
            int sc = 1;
            int newColor = 2;
            int[][] expected = { new[] { 2, 2, 2 }, new[] { 2, 2, 0 }, new[] { 2, 0, 1 } };
            FloodFillDFS dfs = new FloodFillDFS();
            dfs.FloodFill(image, sr, sc, newColor);
            for (int i = 0; i < 3; i++)
            {
                CollectionAssert.AreEqual(expected[i],image[i] );
            }

        }
    }

    public class FloodFillDFS
    {
        public int[][] FloodFill(int[][] image, int sr, int sc, int newColor)
        {
            int oldColor = image[sr][sc];
            DFS(image, sr, sc, newColor, oldColor);
            return image;
        }

        //make sure to check first the corner cases
        private void DFS(int[][] image, int sr, int sc, int newColor, int oldColor)
        {
            if (sr < 0 || sc < 0 || sr >= image.Length || sc >= image[0].Length || image[sr][sc] == newColor || image[sr][sc] != oldColor)
            {
                return;
            }

            image[sr][sc] = newColor;
            DFS(image, sr - 1, sc, newColor, oldColor);
            DFS(image, sr + 1, sc, newColor, oldColor);
            DFS(image, sr, sc - 1, newColor, oldColor);
            DFS(image, sr, sc + 1, newColor, oldColor);
        }
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ if you give -1 please explain why, I would like to learn. \$\endgroup\$
    – Gilad
    Commented Apr 10, 2020 at 21:14

1 Answer 1

2
\$\begingroup\$

I would split the if to 3 variables and give each a name : out of image, isVisited and isOriginalColor or something similar.

I don't know what sr means, I would use row and col.

FloodFill return image but dfs change the original image, It can confuse the user of the class.

\$\endgroup\$
1
  • \$\begingroup\$ thanks for the review, FloodFill suppose to change the original image. like when you paint a shape in Paint. \$\endgroup\$
    – Gilad
    Commented Apr 10, 2020 at 20:30

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.