hacktoberfest_2022 icon indicating copy to clipboard operation
hacktoberfest_2022 copied to clipboard

Create FLOOD FILL IN A GRAPH.cpp

Open devanshikapla opened this issue 3 years ago • 1 comments
trafficstars

class Solution { private: void dfs(int row, int col, vector<vector>&ans, vector<vector>& image, int newColor, int delRow[], int delCol[], int iniColor) {

    ans[row][col] = newColor; 
    int n = image.size();
    int m = image[0].size(); 
   
    for(int i = 0;i<4;i++) {
        int nrow = row + delRow[i]; 
        int ncol = col + delCol[i]; 
       
        if(nrow>=0 && nrow<n && ncol>=0 && ncol < m && 
        image[nrow][ncol] == iniColor && ans[nrow][ncol] != newColor) {
            dfs(nrow, ncol, ans, image, newColor, delRow, delCol, iniColor); 
        }
    }
}

public: vector<vector> floodFill(vector<vector>& image, int sr, int sc, int newColor) {

    int iniColor = image[sr][sc]; 
    vector<vector<int>> ans = image; 
    
    int delRow[] = {-1, 0, +1, 0};
    int delCol[] = {0, +1, 0, -1}; 
    dfs(sr, sc, ans, image, newColor, delRow, delCol, iniColor); 
    return ans; 
}

};

devanshikapla avatar Oct 01 '22 06:10 devanshikapla

please follow the id and mssg here i will merge it after 2 days bcs more than 150 pr are pending

markandey007 avatar Oct 09 '22 08:10 markandey007