-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs_flood_fill.cpp
More file actions
66 lines (55 loc) · 1.12 KB
/
Copy pathbfs_flood_fill.cpp
File metadata and controls
66 lines (55 loc) · 1.12 KB
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
#include <bits/stdc++.h>
using namespace std;
const int N = 505;
char g[N][N];
int dr[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dc[] = {0, 1, 1, 1, 0, -1, -1, -1};
int R, C;
int bfsFloodFill(int sr, int sc, char c1, char c2){
queue< pair<int, int> > f;
f.push(make_pair(sr, sc));
int ans = 0;
while(!f.empty()){
int rr = f.front().first;
int cc = f.front().second;
f.pop();
for(int d = 0; d < 8; d++){
int r = rr + dr[d];
int c = cc + dc[d];
if(r < 0 || r >= R || c < 0 || c >= C){
continue;
}
if(g[r][c] != c1){
continue;
}
ans++;
f.push(make_pair(r, c));
g[r][c] = c2;
}
}
return ans;
}
int main()
{
int tt;
scanf("%d", &tt);
while(tt--){
scanf("%d %d", &R, &C);
for(int i = 0; i < R; i++){
for(int j = 0; j < C; j++){
scanf(" %c", &g[i][j]);
}
}
int rr, cc;
scanf("%d %d", &rr, &cc);
int ans = bfsFloodFill(rr, cc, 'W', '.');
printf("ans = %d\n", ans);
for(int i = 0; i < R; i++){
for(int j = 0; j < C; j++){
printf("%c", g[i][j]);
}
printf("\n");
}
}
return 0;
}