-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathx total shapes.cpp
64 lines (60 loc) · 934 Bytes
/
x total shapes.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
#include<iostream>
#include<vector>
#include<bits/stdc++.h>
#define lli long long int
#define mod 1000000007
#define pb push_back
#define mk make_pair
#define fastio ios_base::sync_with_stdio(false); cout.tie(NULL);
using namespace std;
int vis[50][50],n,m;
char a[50][50];
void DFS(int x,int y)
{
if(vis[x][y]==0 && a[x][y]=='X')
{
// cout<<x<<" "<<y<<"\n";
vis[x][y]=1;
a[x][y]='1';
if(x-1>=0)
DFS(x-1,y);
if(x+1<n)
DFS(x+1,y);
if(y-1>=0)
DFS(x,y-1);
if(y+1<m)
DFS(x,y+1);
}
}
int main()
{
fastio;
int t;
cin>>t;
while(t--)
{
string s;
int i,j,cc=0;
cin>>n>>m;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
vis[i][j]=0;
for(i=0;i<n;i++)
{
cin>>s;
for(j=0;j<m;j++)
{
if(s[j]=='O')
a[i][j]='O';
else
a[i][j]='X';
}
}
for(i=0;i<n;i++)
for(j=0;j<m;j++)
if(vis[i][j]==0 && a[i][j]=='X')
DFS(i,j) , cc++;
cout<<cc<<"\n";
}
return 0;
}