-
Notifications
You must be signed in to change notification settings - Fork 1
/
replace_O_with_X.cpp
58 lines (58 loc) · 1.08 KB
/
replace_O_with_X.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
#include<bits/stdc++.h>
using namespace std;
bool it(char a[21][21],int n,int m,int i,int j,bool vis[21][21])
{
if(i<0 || i>=n || j<0 || j>=m)
return 0;
if(a[i][j]=='X') return 1;
if(vis[i][j]==1) return 1;
if(vis[i][j]==0 && a[i][j]=='O')
vis[i][j]=1;
bool l=it(a,n,m,i,j-1,vis);
bool r=it(a,n,m,i,j+1,vis);
bool u=it(a,n,m,i+1,j,vis);
bool d=it(a,n,m,i-1,j,vis);
return l&&r&&u&&d;
}
int main()
{
//code
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n>>m;
char a[21][21];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>a[i][j];
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(a[i][j]=='O')
{
bool vis[21][21]={};
if(it(a,n,m,i,j,vis))
{
a[i][j]='X';
}
}
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cout<<a[i][j]<<" ";
}
}
cout<<endl;
}
return 0;
}