-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path142.cpp
83 lines (72 loc) · 1.96 KB
/
142.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
#include<bits/stdc++.h>
using namespace std;
void nhap(char a[100][100],int m,int n){
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
cin>>a[i][j];
}
}
}
void floodFillUtil(char mat[100][100], int x, int y, char prevV, char newV,int m,int n) {
// Base cases
if (x < 0 || x >= m || y < 0 || y >= n)
return;
if (mat[x][y] != prevV)
return;
// Replace the color at (x, y)
mat[x][y] = newV;
// Recur for north, east, south and west
floodFillUtil(mat, x+1, y, prevV, newV,m,n);
floodFillUtil(mat, x-1, y, prevV, newV,m,n);
floodFillUtil(mat, x, y+1, prevV, newV,m,n);
floodFillUtil(mat, x, y-1, prevV, newV,m,n);
}
// Returns size of maximum size subsquare matrix
// surrounded by 'X'
int replaceSurrounded(char mat[100][100],int m,int n) {
// Step 1: Replace all 'O' with '-'
for (int i=0; i<m; i++)
for (int j=0; j<n; j++)
if (mat[i][j] == 'O')
mat[i][j] = '-';
// Call floodFill for all '-' lying on edges
for (int i=0; i<m; i++) // Left side
if (mat[i][0] == '-')
floodFillUtil(mat, i, 0, '-', 'O',m,n);
for (int i=0; i<m; i++) // Right side
if (mat[i][n-1] == '-')
floodFillUtil(mat, i, n-1, '-', 'O',m,n);
for (int i=0; i<n; i++) // Top side
if (mat[0][i] == '-')
floodFillUtil(mat, 0, i, '-', 'O',m,n);
for (int i=0; i<n; i++) // Bottom side
if (mat[m-1][i] == '-')
floodFillUtil(mat, m-1, i, '-', 'O',m,n);
// Step 3: Replace all '-' with 'X'
for (int i=0; i<m; i++)
for (int j=0; j<n; j++)
if (mat[i][j] == '-')
mat[i][j] = 'X';
}
void in(char a[100][100],int m,int n){
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
cout<<a[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
}
int main(){
int t;
cin>>t;
while(t--){
int m,n;
char a[100][100];
cin>>m>>n;
nhap(a,m,n);
replaceSurrounded(a,m,n);
in(a,m,n);
}
return 0;
}