-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathGraphs_flood_fill.cpp
113 lines (110 loc) · 3.53 KB
/
Graphs_flood_fill.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
Flood fill, also called seed fill, is a flooding algorithm that determines and alters the area connected
to a given node in a multi-dimensional array with some matching attribute. It is used in the "bucket" fill
tool of paint programs to fill connected, similarly-colored areas with a different color, and in games such
as Go and Minesweeper for determining which pieces are cleared. Source(https://en.wikipedia.org/wiki/Flood_fill)
Sample Input 15 30
..............................
.............#####............
.............#...#............
.....#########...#######......
....###.....######.....###....
...##....................##...
..##......................#...
..##.....................##...
..###...................##....
....###................###....
......###............###......
........###........###........
..........##########..........
..............................
...........A.P.P.L.E..........
Output :
..............................
.............#####............
.............#...#............
.....#########...#######......
....###.....######.....###....
...##....................##...
..##......................#...
..##.....................##...
..###...................##....
....###................###....
......###............###......
........###........###........
..........##########..........
..............................
...........A.P.P.L.E..........
..............................
.............#####............
.............#...#............
.....#########...#######......
....###rrrrr######rrrrr###....
...##rrrrrrrrrrrrrrrrrrrr##...
..##rrrrrrrrrrrrrrrrrrrrrr#...
..##rrrrrrrrrrrrrrrrrrrrr##...
..###rrrrrrrrrrrrrrrrrrr##....
....###rrrrrrrrrrrrrrrr###....
......###rrrrrrrrrrrr###......
........###rrrrrrrr###........
..........##########..........
..............................
...........A.P.P.L.E..........
..............................
.............#####............
.............#bbb#............
.....#########bbb#######......
....###rrrrr######rrrrr###....
...##rrrrrrrrrrrrrrrrrrrr##...
..##rrrrrrrrrrrrrrrrrrrrrr#...
..##rrrrrrrrrrrrrrrrrrrrr##...
..###rrrrrrrrrrrrrrrrrrr##....
....###rrrrrrrrrrrrrrrr###....
......###rrrrrrrrrrrr###......
........###rrrrrrrr###........
..........##########..........
..............................
...........A.P.P.L.E..........
*/
#include<bits/stdc++.h>
using namespace std;
int R, C;
void print_matrix(char input[][50]){
for(int i = 0; i < R; i++){
for(int j = 0; j < C; j++){
cout << input[i][j];
}
cout << "\n";
}
}
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
void flood_fill(char input[][50], int i, int j, char ch, char color){
// base case : make sure we do not cross boundries of input matrix
if(i < 0 || j < 0 || i >= R || j >= C)
return;
// if we are coming to a cell that is not equal to ch then we dont do anything
if(input[i][j] != ch)
return;
// fill the color
input[i][j] = color;
// dfs flood_fill on all directions
for(int k = 0; k < 4; k++){
flood_fill(input, 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];
}
}
print_matrix(input);
flood_fill(input, 8, 13, '.', 'r');
print_matrix(input);
flood_fill(input, 2, 15, '.', 'b');
print_matrix(input);
return 0;
}