-
Notifications
You must be signed in to change notification settings - Fork 0
/
floodfill_algo.cpp
52 lines (46 loc) · 1.38 KB
/
floodfill_algo.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
49
50
51
52
#include<bits/stdc++.h>
using namespace std;
void print(vector<vector<char>> mat){
for(int i=0;i<mat.size();i++){
for(int j=0;j<mat[0].size();j++){
cout<<mat[i][j]<<" ";
}
cout<<endl;
}
}
vector<int> dx{-1,0,1,0};
vector<int> dy{0,-1,0,1};
void flood_fill(vector<vector<char>> &mat, int i, int j,char ch, char color, int R, int C){
if(i<0 or j<0 or i>=R or j>=C){
return;
}
if(mat[i][j] != ch){
return;
}
mat[i][j] = color;
for(int k=0;k<dx.size();k++){
flood_fill(mat,i+dx[k],j+dy[k],ch,color,R,C);
}
}
int main(){
vector<vector<char>> mat{
{'.','.','.','.','.','.','.','.'},
{'.','#','#','#','#','#','#','.'},
{'.','#','.','.','.','.','#','.'},
{'.','#','.','#','#','.','#','.'},
{'.','#','.','#','#','.','#','.'},
{'.','#','.','.','.','.','#','.'},
{'.','#','.','.','#','#','#','.'},
{'.','#','.','#','.','.','.','.'},
{'.','#','.','#','.','.','.','.'},
{'.','#','.','#','.','.','.','.'},
{'.','#','#','#','.','.','.','.'},
{'.','.','.','.','.','.','.','.'}
};
print(mat);
// (2,2) coord to start with
// changing color like '.' to '*' or any other char
flood_fill(mat,2,2,'.','*',mat.size(),mat[0].size());
print(mat);
return 0;
}