-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathFlood Fill Algorithm.cpp
62 lines (52 loc) · 956 Bytes
/
Flood Fill Algorithm.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
53
54
55
56
57
58
59
60
61
62
#include<iostream>
using namespace std;
int r,c;
void printMat(char input[][50]){
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
cout<<input[i][j];
}
cout<<endl;
}
}
// w,n,e,s
int dx[4]={-1,0,1,0};
int dy[4]={0,-1,0,1};
//ch is the character to be replaced.
//colour is the character to be added/
void flood_fill(char mat[][50],int i, int j, char ch, char color)
{
//BASE CASE- matrix Bounds
if(i<0 || j<0 || i>=r || j>=c)
{
return;
}
//BOUNDARY CONDITION
if(mat[i][j]!=ch)
{
return ;
}
//Recursive Call
mat[i][j]=color;
for(int k=0;k<4;k++)
{
flood_fill(mat,i+dx[k],j+dy[k],ch,color);
}
}
int main()
{
cin>>r>>c;
char input[15][50];
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cin>>input[i][j];
}
}
//flood_fill(input,5,10,'.','r');
//printMat(input);
flood_fill(input, 5 , 0 , '.', 'r');
printMat(input);
return 0;
}