forked from shivaylamba/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountingRooms.cpp
67 lines (58 loc) · 1.22 KB
/
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
#include <iostream>
#include <vector>
using namespace std;
#define ll long long int
#define OJ \
freopen("input.txt", "r", stdin); \
freopen("output.txt", "w", stdout);
#define FIO \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
char arr[1001][1001];
bool vis[1001][1001] = {false};
int n, m;
bool isValid(int x, int y){
if(x<0 || x>=n || y<0 || y>=m){
return false;
}
if(vis[x][y] == true || arr[x][y] == '#'){
return false;
}
return true;
}
void dfs(int x, int y){
vis[x][y] = true;
if(isValid(x-1, y)){
dfs(x-1, y);
}
if(isValid(x+1, y)){
dfs(x+1, y);
}
if(isValid(x, y-1)){
dfs(x, y-1);
}
if(isValid(x, y+1)){
dfs(x, y+1);
}
}
int main()
{
//OJ;
cin >> n >> m;
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
cin >> arr[i][j];
}
}
int count = 0;
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
if(arr[i][j] == '.' && vis[i][j] == false){
dfs(i,j);
count++;
}
}
}
cout << count << endl;
}