-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcounting_rooms.cpp
More file actions
52 lines (43 loc) · 1.05 KB
/
counting_rooms.cpp
File metadata and controls
52 lines (43 loc) · 1.05 KB
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
#include<bits/stdc++.h>
#define ll long long
using namespace std;
//Counting Rooms dfs connected components
int x[4]={0,1,0,-1};
int y[4]={1,0,-1,0};
void dfs(vector<vector<int>> &v,int i,int j,int n,int m,vector<vector<int>> &visited){
visited[i][j]=1;
for(int d=0;d<4;d++){
int x1=i+x[d];
int y1=j+y[d];
if(x1>=0&&y1>=0&&x1<=n&&y1<=m){
if(!(visited[x1][y1]) && v[x1][y1]==1)
dfs(v,x1,y1,n,m,visited);
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n,m;
char a;
int c=0;
cin>>n>>m;
vector<vector<int>> v(n+1,vector<int>(m+1,0));
vector<vector<int>> visited(n+1,vector<int>(m+1,0));
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>a;
if(a=='.')v[i][j]=1;
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(v[i][j]==1 && !(visited[i][j])){
c++;
dfs(v,i,j,n,m,visited);
}
}
}
cout<<c;
return 0;
}