forked from VinayBelwal/Hactober-22-Programs-and-Projects-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cses_countingRooms.cpp
88 lines (79 loc) · 1.28 KB
/
cses_countingRooms.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
83
84
85
86
87
88
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
const int mod=1000000007;
int dx[]={-1,1,0,0,};
int dy[]={0,0,-1,1};
int dist[10001][10001];
int visited[10001][10001];
char arr[10001][10001];
int n,m;
bool isvalid(int x,int y)
{
if(x<1 || x>n || y<1 || y>m)
{
return false;
}
if(visited[x][y]==1 || arr[x][y]=='#')
{
return false;
}
return true;
}
void dfs(int srcx,int srcy)
{
visited[srcx][srcy]=1;
for(int i=0;i<4;i++)
{
if(isvalid(srcx+dx[i],srcy+dy[i]))
{
dfs(srcx+dx[i],srcy+dy[i]);
}
}
}
void solve()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>arr[i][j];
}
}
int cnt=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(arr[i][j]=='.' && !visited[i][j])
{
cnt++;
dfs(i,j);
}
}
}
cout<<cnt<<endl;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
visited[i][j]=0;
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int t=1;
// cin>>t;
while (t--)
{
solve();
}
return 0;
}