-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflood fill problem.cpp
54 lines (49 loc) · 1.03 KB
/
flood fill problem.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
#include <bits/stdc++.h>
using namespace std;
int r , c;
int dx[] = { 0 , 1 , -1 , 0 };
int dy[] = { 1 ,0 , 0 , -1 };
void floodfill(char mat[][50] , int i , int j, char ch , char color)
{
///base case
if(i < 0 || j < 0 || i >= r || j>=c)
{
return;
}
///boundary base case
if(mat[i][j] != ch)
{
return;
}
mat[i][j] = color;
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];
}
}
// for(int i = 0 ; i<r ;i++){
// for(int j = 0 ; j < c ; j++)
// {
// cout<<input[i][j];
// }
// cout<<endl;
// }
floodfill(input, 3 , 3,'.','t');
for(int i = 0 ; i<r ;i++){
for(int j = 0 ; j < c ; j++)
{
cout<<input[i][j];
}
cout<<endl;
}
}