733. The image rendering

Links: https://leetcode-cn.com/problems/flood-fill/

There is a picture representation of a two-dimensional array of integers, each integer represents a pixel value of the size of the picture, the value between 0 and 65535.

Give you a coordinate (sr, sc) represents the start of image rendering pixel values ​​(row, column) and a new color values ​​newColor, allow you to re-color this image.

In order to complete the work of color, from the initial coordinates, recording four directions initial coordinate values ​​of the initial coordinates of a pixel connected to the same pixel, and then re-recording pixels qualifying them with the four directions corresponding to four the same is connected to the pixel value of the pixel coordinates of the initial point, ......, the process is repeated. The color of all recorded pixel color values ​​to the new values.

Finally, return the image through color rendering.

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]]
Analysis: 
in the middle of the image, (coordinates (sr, sc) = (1,1 )),
in the path of all eligible pixel point colors have been changed to 2.
Note that the pixel does not change to the lower right corner 2,
because it is not a pixel in the vertical and horizontal four directions connected to the initial point.
note:

image, and image [0] of a length in the range [1, 50] within.
The original point analysis satisfies 0 <= sr <image.length and 0 <= sc <image [0 ] .length.
image [i] [j] and the color value newColor shown in the range [0, 65535] within.

It would have been a very simple question DFS, but because of the hour and a half a point card

First the code has not been

class Solution {
public:
    void dfs(vector<vector<int>>& image, int x, int y, int oldColor, int newColor)
    {
        cout<<x<<y<<" ";
        if(image[x][y] == oldColor)
            image[x][y] = newColor;
        else return;
        int dis[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
        for(int i = 0; i < 4; i++)
        {
            int xx = x + dis[i][0];
            int yy = y + dis[i][1];
            cout<<xx<<yy<<" ";
            if(0 <= xx && xx < image.size() && 0 <= yy && yy < image[0].size())
            {
                dfs(image, xx, yy, oldColor, newColor);
                cout<<xx<<yy<<endl;
            }
        }
        return;
    }
    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
        // int len1 = image.size();
        // int len2 = image[0].size();
        int oldColor = image[sr][sc];
        dfs(image, sr, sc, oldColor, newColor);
        return image;
    }
};

Problem code is that, if the image [sr] [sc] is equal to the original color newColor, then there will be an endless loop.

The examples: [[0,0,0], [0,1,1]] 111

That pattern is a 000

               0  1  1

The starting point is (1,1), according to the code (1,1) -> (2, 1) (ineligible)

                                                    -> (1, 2) -> (2, 2) (ineligible)

                                                                -> (1, 3) (not eligible)

                                                                 -> (0, 2) (not eligible)

                                                                 ->(1, 1)

NewColor and then if the initial colors are not equal, then there is no problem, but this time, the updated imag [1] [1] still meets the conditions equal oldColor, which will lead to repeat the above steps have been causing death cycle! ! ! ! ! ! ! ! So when we this particular case, the direct return image, without performing dfs.

class Solution {
public:
    void dfs(vector<vector<int>>& image, int x, int y, int oldColor, int newColor)
    {
        // cout<<x<<y<<" ";
        if(image[x][y] == oldColor)
            image[x][y] = newColor;
        else return;
        int dis[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
        for(int i = 0; i < 4; i++)
        {
            int xx = x + dis[i][0];
            int yy = y + dis[i][1];
            // cout<<xx<<yy<<" ";
            if(0 <= xx && xx < image.size() && 0 <= yy && yy < image[0].size())
            {
                dfs(image, xx, yy, oldColor, newColor);
                // cout<<xx<<yy<<endl;
            }
        }
        return;
    }
    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
        // int len1 = image.size();
        // int len2 = image[0].size();
        int oldColor = image[sr][sc];
        if(oldColor == newColor) return image;
        dfs(image, sr, sc, oldColor, newColor);
        return image;
    }
};

Only need to add the bottom fifth sentence.

Published 58 original articles · won praise 6 · views 7058

Guess you like

Origin blog.csdn.net/weixin_43569916/article/details/103941624