-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAreas.cpp
75 lines (75 loc) · 1.6 KB
/
Areas.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
#include<bits/stdc++.h>
using namespace std;
//A kind of obstacle tackling problem!!!
void dfs(int i,int j,int &cnt,vector<vector<int>> &v,int n,int m)
{
v[i][j]=2;
if(i+1<n) //corner case check then apply dfs
{
if(v[i+1][j]==1)
{
cnt++;
dfs(i+1,j,cnt,v,n,m);
}
}
if(i-1>=0) //corner case check and then apply dfs
{
if(v[i-1][j]==1)
{
cnt++;
dfs(i-1,j,cnt,v,n,m);
}
}
if(j-1>=0)
{
if(v[i][j-1]==1)
{
cnt++;
dfs(i,j-1,cnt,v,n,m);
}
}
if(j+1<m)
{
if(v[i][j+1]==1)
{
cnt++;
dfs(i,j+1,cnt,v,n,m);
}
}
}
int main()
{
int t;cin>>t;
while(t--)
{
int n,m;cin>>n>>m;
vector<vector<int>> v(n,vector<int> (m));
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
char s;cin>>s;
if(s=='#')v[i][j]=0;
else v[i][j]=1;
}
}
vector<int> ans;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
int cnt=1;
if(v[i][j]==1)
{
dfs(i,j,cnt,v,n,m);
ans.push_back(cnt);
}
}
}
cout<<ans.size()<<endl;
for(int i=0;i<ans.size();i++){
cout<<ans[i]<<" ";
}
cout<<endl;
}
}