Take it, can you be addicted to brushing the questions? six

Offer arrives, dig friends to pick up! I am participating in the 2022 Spring Recruitment Check-In Event, click to view the event details .

Addiction Program

Guided reading

Plans that have been implemented (can be found on my blog):

insert image description here

Since I started to record my own notes, I have gone from the daily fat school series , to the 21-day dynamic programming , to the 28-day special training of the big factory , and the series of not conquering dfs and not competing . If you are interested, you can go and see. Judging from the final result, I received some like-minded friends to brush the questions together. If you also want to join us, you can privately message me to join us, or you can leave a message below to check in. Now our team is not very big, but there are also a few successful students, some of whom have entered a good company (of course, it can’t be said that it is all based on brushing ). Some of them got the competition certificate . Next, I will continue to persevere. We need to cultivate this "addiction". So we have another 28-day problem-solving addiction training program. When these 28 days are over, let's summarize together.

PS: The algorithm is not very language-specific and everyone can come.

topic

There is an image image represented by a two-dimensional array of mxn integers, where image[i][j] represents the pixel value of the image.

You are also given three integers sr, sc and newColor. You should paint the image starting at pixel image[sr][sc].

In order to complete the coloring work, starting from the initial pixel, record the connected pixels whose pixel values ​​are the same as the initial coordinates in the four directions of the initial coordinates, up, down, left, and right, and then record the eligible pixels in these four directions and their corresponding four The connected pixels in the direction whose pixel value is the same as the initial coordinate, ..., repeat the process. Change the color value of all recorded pixels to newColor .

Finally, return the rendered image after shading.

Example 1:

insert image description here

输入: 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,因为它不是在上下左右四个方向上与初始点相连的像素点。
复制代码
示例 2:

输入: image = [[0,0,0],[0,0,0]], sr = 0, sc = 0, newColor = 2
输出: [[2,2,2],[2,2,2]]
复制代码

Test address : address

Ideas We start from a given starting point and perform a depth-first search. Each time a square is searched, if it is the same color as the square at the initial position, the color of the square is updated to prevent repeated searches; if it is not the same, backtracking is performed.

Note: Because the color of the initial position will be modified, we need to save the color of the initial position for future update operations.

Solution one:

class Solution {
    int[] x={0,0,-1,1};//上下左右
    int[] y={1,-1,0,0};
    public int[][] dfs(int[][]image,int sr,int sc,int newColor,int flag){
        if((image[sr][sc]!=flag)||(newColor==flag))return image;
        image[sr][sc]=newColor;
        for(int i=0;i<4;i++){
            int row=x[i]+sr;
            int col=y[i]+sc;
            if(row<=image.length-1&&row>=0&&col>=0&&col<=image[0].length-1){//判断是否溢出
                dfs(image,row,col,newColor,flag);
                 if(image[row][col]==flag){
                   image[row][col]=newColor;
                }
            }
        }
        return image;
    }
    public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
        int flag=image[sr][sc];
        return dfs(image,sr,sc,newColor,flag);

    }
}
复制代码

Solution two:

class Solution {
    int[] dx = {1, 0, 0, -1};
    int[] dy = {0, 1, -1, 0};

    public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
        int currColor = image[sr][sc];
        if (currColor != newColor) {
            dfs(image, sr, sc, currColor, newColor);
        }
        return image;
    }

    public void dfs(int[][] image, int x, int y, int color, int newColor) {
        if (image[x][y] == color) {
            image[x][y] = newColor;
            for (int i = 0; i < 4; i++) {
                int mx = x + dx[i], my = y + dy[i];
                if (mx >= 0 && mx < image.length && my >= 0 && my < image[0].length) {
                    dfs(image, mx, my, color, newColor);
                }
            }
        }
    }
}


复制代码

debugging

Here is the debugging code: You can debug in eclipse or idea


public class Solution {

	int[] x = { 0, 0, -1, 1 };// 上下左右
	int[] y = { 1, -1, 0, 0 };

	public int[][] dfs(int[][] image, int sr, int sc, int newColor, int flag) {
		if ((image[sr][sc] != flag)||(newColor==flag))
			return image;
		image[sr][sc] = newColor;
		for (int i = 0; i < 4; i++) {
			int row = x[i] + sr;
			int col = y[i] + sc;
			if (row <= image.length - 1 && row >= 0 && col >= 0 && col <= image[0].length - 1) {
				dfs(image, row, col, newColor, flag);
				if (image[row][col] == flag) {
					image[row][col] = newColor;
				}
			}
		}
		return image;
	}

	public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
		int flag = image[sr][sc];
		return dfs(image, sr, sc, newColor, flag);

	}

	public static void main(String[] args) {
		Solution s=new Solution();
		int[][] image= {{0,0,0},{0,1,1}};
		image=s.floodFill( image, 1, 1, 1);
		for(int[] i:image) {
			for(int j:i) {
				System.out.print(j+" ");
			}
			System.out.println();
		}
	}
}
复制代码

important things

If you are learning python or Java or even C and encounter problems, you can leave a message to me, because in the early stage of learning, novices will always take a lot of detours. At this time, if there is no one to help, it is easy to give up. There are many such examples around. Many people changed their majors and changed their direction after learning, not only because of their own problems but also because they did not study correctly. Therefore, as a person who has come here, I hope to leave a message for me if you have any questions. It is not a matter of helping but just typing a few lines of words.

Guess you like

Origin juejin.im/post/7078502797997506591