-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloodFillAlgorithm.cpp
More file actions
46 lines (43 loc) · 873 Bytes
/
floodFillAlgorithm.cpp
File metadata and controls
46 lines (43 loc) · 873 Bytes
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
#include <bits/stdc++.h>
using namespace std;
int a[105][105];
int n, m;
int dfs(int x, int y, int gc, int cc)
{
if (x < 0 || y < 0 || x >= n || y >= m || a[x][y] != cc)
return 0;
a[x][y] = gc;
dfs(x - 1, y, gc, cc);
dfs(x, y - 1, gc, cc);
dfs(x, y + 1, gc, cc);
dfs(x + 1, y, gc, cc);
}
int main()
{
//code
int tc;
cin >> tc;
while (tc--)
{
cin >> n >> m;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> a[i][j];
}
}
int x, y, gc, cc;
cin >> x >> y >> gc;
cc = a[x][y];
dfs(x, y, gc, cc);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cout << a[i][j] << " ";
}
}
cout << "\n";
}
}