-
Notifications
You must be signed in to change notification settings - Fork 25
/
flood_fill_dfs.cpp
72 lines (48 loc) · 905 Bytes
/
flood_fill_dfs.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
63
64
65
66
67
68
69
70
71
72
#include<iostream>
using namespace std;
int R;
int 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[] = {-1,0,1,0};
int dy[] = {0,-1,0,1};
//ch is the character to be replaced
//color is the character to be added
void floodFill(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;
}
// Figure Boundary Condition
if(mat[i][j]!=ch){
return;
}
//Recursive Call
mat[i][j] = color;
printMat(mat);
cout<<endl;
for(int k=0;k<4;k++){
floodFill(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];
}
}
printMat(input);
floodFill(input,8,13,'.','r');
//floodFill(input,0,0,'.','!');
printMat(input);
return 0;
}