1+ #include < bits/stdc++.h>
2+ using namespace std ;
3+ typedef long long ll;
4+ typedef vector<ll> vi;
5+ typedef vector<vi> vvi;
6+ const int mod = 1000000007 ;
7+ const int sze = 100010 ;
8+ int dr[] = {1 , 1 , 0 , -1 , -1 , -1 , 0 , 1 };
9+ int dc[] = {0 , 1 , 1 , 1 , 0 , -1 , -1 , -1 };
10+ char g[110 ][110 ];
11+ char t[110 ][110 ];
12+
13+ int m, n; // no of vertices
14+
15+ int floodFill (int r, int c, char c1, char c2) {
16+ if (r >= n || r < 0 || c >= m || c < 0 || t[r][c] == c2) return 0 ;
17+
18+ int ans = 1 ;
19+ if (t[r][c] == c1) t[r][c] = c2; // visited
20+
21+ for (int i = 0 ; i < 8 ; ++i) {
22+ ans += floodFill (r + dr[i], c + dc[i], c1, c2);
23+ }
24+ // g[r][c] = c1; //backtracking can't be used because 1 cell at be visited in one go
25+ // Once visited it must not be visited again
26+ return ans;
27+
28+ }
29+
30+ int main () {
31+ // freopen("in","r", stdin);
32+ int t1;
33+ cin >> t1;
34+
35+ cin.getline (g[0 ], 100 ); // consumes endl
36+ cin.getline (g[0 ], 100 ); // consumes blank line
37+
38+ while (t1--) {
39+ int i = 0 ; // curline
40+ m = n = 0 ;
41+ while (isalpha (cin.peek ()) && cin.getline (g[i++], 100 )) n++; // fills the matrix
42+ m = strlen (g[0 ]);
43+ int r, c;
44+ char rowCol[100 ];
45+ while (cin.getline (rowCol, 100 ) && strlen (rowCol) != 0 ) {
46+ sscanf (rowCol, " %d%d" , &r, &c);
47+ copy (&g[0 ][0 ], &g[110 ][0 ], &t[0 ][0 ]); // use copy with containers
48+ // memcpy(t, g, sizeof g);
49+ cout << floodFill (--r, --c, ' W' , ' L' ) << " \n " ;
50+ }
51+ if (t1) cout << " \n " ;
52+ }
53+ }
0 commit comments