-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFloodfill.cpp
48 lines (47 loc) · 1.31 KB
/
Floodfill.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<bits/stdc++.h>
using namespace std;
vector<vector<int>> final(vector<vector<int>>& image, int sr, int sc, int color,int back_color) {
if(back_color==color)
return image;
image[sr][sc]=color;
if (sr>0 && image[sr-1][sc]==back_color){
image=final(image,sr-1,sc,color,back_color);
}
if (sr<image.size()-1 && image[sr+1][sc]==back_color){
image=final(image,sr+1,sc,color,back_color);
}
if (sc>0 && image[sr][sc-1]==back_color)
image=final(image,sr,sc-1,color,back_color);
if (sc<image[0].size()-1 && image[sr][sc+1]==back_color)
image=final(image,sr,sc+1,color,back_color);
return image;
}
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) {
return final(image,sr,sc,color,image[sr][sc]);
}
int main(){
vector<vector<int>>image;
int rows,col;
cin>>rows>>col;
for(int i=0;i<rows;i++){
vector<int>temp;
for(int j=0;j<col;j++){
int b;
cin>>b;
temp.push_back(b);
}
image.push_back(temp);
}
int sr,sc;
cin>>sr>>sc;
int colour;
cin>>colour;
image=floodFill(image,sr,sc,colour);
for(int i=0;i<rows;i++){
for(int j=0;j<col;j++){
cout<<image[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}