leetcode733. Image rendering/dfs, bfs

Subject: 733. Image rendering

There is a picture represented by a two-dimensional integer array. Each integer represents the pixel value of the picture, and the value is between 0 and 65535.

Give you a coordinate (sr, sc) that represents the pixel value (row, column) at which the image is rendered, and a new color value newColor, allowing you to recolor the image.

In order to complete the coloring work, starting from the initial coordinates, record the connected pixels with the same pixel value as the initial coordinates in the four directions of the initial coordinates, and then record the four pixels that meet the conditions in these four directions and their corresponding four The connected pixels with the same pixel value as the initial coordinate in the direction,..., repeat the process. Change the color value of all recorded pixels to the new color value.

Finally, return to the rendered image after coloring.

Example 1:

输入: 
image = [[1,1,1],[1,1,0],[1,0,1]]
sr = 1, sc = 1, newColor = 2
输出: [[2,2,2],[2,2,0],[2,0,1]]
解析: 
在图像的正中间,(坐标(sr,sc)=(1,1)),
在路径上所有符合条件的像素点的颜色都被更改成2。
注意,右下角的像素没有更改为2,
因为它不是在上下左右四个方向上与初始点相连的像素点。

note:

  • The length of image and image[0] is in the range [1, 50].
  • The given initial point will satisfy 0 <= sr <image.length and 0 <= sc <image[0].length.
  • The color values ​​represented by image[i][j] and newColor are in the range [0, 65535].

Source: LeetCode (LeetCode)
Link: https://leetcode-cn.com/problems/flood-fill The
copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

Basic idea: dfs

class Solution {
    
    
public:
    vector<vector<int>> dir;
    int row, col, pre_color;
    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
    
    
        dir = {
    
    {
    
    -1, 0}, {
    
    0, -1}, {
    
    0, 1}, {
    
    1, 0}};
        row = image.size();
        col = image[0].size();
        pre_color = image[sr][sc];
        image[sr][sc] = newColor;
        if(newColor != pre_color)//这样处理的目的是为了避免出现当颜色相等的时候出现循环递归
            dfs(image, sr, sc, newColor);
        return image;
    }
    void dfs(vector<vector<int>>& image, int x, int y, int color){
    
    
        for(auto d : dir){
    
    
            int cx = x + d[0];
            int cy = y + d[1];
            if(judge(cx, cy) && image[cx][cy] == pre_color){
    
    
                image[cx][cy] = color;
                dfs(image, cx, cy, color);
            }
        }
    }
    bool judge(int x, int y){
    
    
        return x >= 0 && x < row && y >= 0 && y < col;
    }
};

Of course, this question can also be written in bfs, and the same point as the starting position is entered into the queue, and the color of the starting position is updated at the same time, until the queue is empty.

Of course, pay attention to this question. When the color of the starting position is the same as the color to be updated, there is no need to deal with it. If it is not judged here, it will cause an endless loop.

Guess you like

Origin blog.csdn.net/qq_31672701/article/details/108036151
Recommended