Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions 12 April Flood fill Algorithm
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
vector<vector<int>> floodFill(vector<vector<int>>& image,
int sr, int sc, int newColor) {
// Code here
int n=image.size();
int m=image[0].size();

int dr[4]={-1, 0, 1, 0};
int dc[4]={0, 1, 0, -1};

int orgColor=image[sr][sc];
if (orgColor == newColor) return image;
image[sr][sc]=newColor;

queue<pair<int, int>>q;

q.push({sr, sc});

while(!q.empty()){
auto &it=q.front();
int r=it.first;
int c=it.second;
q.pop();

for(int i=0; i<4; i++){
int nr=r+dr[i];
int nc=c+dc[i];
if(nr>=0 && nr<n && nc>=0 && nc<m &&
image[nr][nc]==orgColor){
q.push({nr, nc});
image[nr][nc]=newColor;
}
}
}
return image;
}
Loading