C + + problem brushing journey

Posted by werushka on Sun, 20 Feb 2022 08:23:08 +0100

Introduction to LeetCode algorithm (day 7)

Breadth first search / depth first search

733. Image rendering


Solution: it's very hard to read the questions. Look at the examples and understand them. The general meaning is, for example, given the starting point pixel p [ 4 , 4 ] p_{[4,4]} The pixel value of p[4,4] position is 1, and given a new color value of 2, because the pixel point p [ 4 , 4 ] p_{[4,4]} The color value 1 of p[4,4] is not equal to the new color value 2, so it is necessary to reset the pixel p [ 4 , 4 ] p_{[4,4]} p[4,4] assigns a new color value of 2, and then starts from the starting point p to find the point equal to the initial pixel value 1 of the starting point p in the upper, lower, left and right directions of p, and assigns it to a new color value of 2. If there are still points equal to the initial pixel value 1 of the starting point in the four directions of the newly found point, continue to repeat the search and coloring operation.

The solution is mainly depth first traversal of DFS and breadth first traversal of BFS. Here are explanation links https://developer.51cto.com/article/614590.html

In short, DFS is a recursive process, which is similar to the preorder traversal of a tree, while BFSl is similar to the sequence traversal of a tree.

lass Solution {
public:
    const int dx[4] = {1, 0, 0, -1};
    const int dy[4] = {0, 1, -1, 0};
    void dfs(vector<vector<int>>& image, int x, int y, int color, int newColor) {
        if (image[x][y] == color) {  //If the pixel value of the initial point is equal to the initial color value, that is, the pixel value of the initial point has not changed, record the initial value
            image[x][y] = newColor;     //Update the pixel value of the initial point to the new color value newcolor
            for (int i = 0; i < 4; i++) {
                int mx = x + dx[i], my = y + dy[i];
                if (mx >= 0 && mx < image.size() && my >= 0 && my < image[0].size()) {
                    dfs(image, mx, my, color, newColor);    //Judge whether the four directions on the right and right recurs in turn, and whether the color value of the is the same as the color value before the initial point is not changed. If it is the same, it will be updated to newColor, and so will the other three directions
                }
            }
        }
    }

    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
        int currColor = image[sr][sc];   
        if (currColor != newColor) {    //If the pixel value of the initial point is not equal to the new color value newColor, a depth search is performed
            dfs(image, sr, sc, currColor, newColor);
        }
        return image;
    }
};


699. Maximum area of the island


Solution: at the beginning of reading the question, I thought it was to return the total number of islands. For the example output result was 6, I couldn't think of the solution. In addition, isn't it two directions to understand horizontally and vertically at the first glance? It's better to write it up, down, left and right. This question is similar to the above question. The same thing to pay attention to is to avoid repeated visits, The previous problem is to assign a new color to the same pixel to avoid repeated search. This problem is to set the traversed point to 0 to represent water to avoid repeated search. In addition, the boundary conditions also need to be considered comprehensively.

class Solution {
public:

    int dfs(vector<vector<int>>& grid, int cur_i, int cur_j){
        if(cur_i < 0 || cur_j < 0 || cur_i == grid.size() || cur_j == grid[0].size() || grid[cur_i][cur_j] != 1){  //When the current point crosses the boundary or is water, the current island area is 0
            return 0;
        }
        grid[cur_i][cur_j] = 0;  //Set the current location to 0 to prevent repeated access
        int di[4] = {0, 0, 1, -1};
        int dj[4] = {1, -1, 0, 0};
        int ans = 1;   //Initialize to 1, because if the current point is not land, it will directly return 0
        for(int i = 0; i < 4; ++i){
            int next_i = cur_i + di[i];
            int next_j = cur_j + dj[i];    
            ans += dfs(grid, next_i, next_j);   //The depth traverses four directions horizontally and vertically until there is no adjacent 1
        }
        return ans;
    }
    int maxAreaOfIsland(vector<vector<int>>& grid) {
       int ans = 0;   
       for(int i = 0; i != grid.size(); ++i){    
           for(int j = 0; j != grid[0].size(); ++j){
               ans = max(ans, dfs(grid, i ,j));     
           }
       }
       return ans;
    }
};

Topics: C++ Algorithm