-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountingRooms.cpp
More file actions
57 lines (39 loc) · 1.19 KB
/
CountingRooms.cpp
File metadata and controls
57 lines (39 loc) · 1.19 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
52
53
54
55
56
57
#include <bits/stdc++.h>
using namespace std;
int n,m;
void do_it(vector<char>array[],bool**check,int i,int j){
check[i][j] = true;
if((i-1 >= 0 && !check[i-1][j]) && array[i-1][j] == '.')do_it(array,check,i-1,j);
if((j-1 >= 0 && !check[i][j-1])&& array[i][j-1] == '.')do_it(array,check,i,j-1);
if((i+1 < n && !check[i+1][j])&& array[i+1][j] == '.')do_it(array,check,i+1,j);
if((j+1 < m && !check[i][j+1])&& array[i][j+1] == '.')do_it(array,check,i,j+1);
}
int rooms(vector<char>array[],bool**check){
int ans = 0;
for(int i = 0 ; i < n ; i ++){
for(int j = 0 ; j < m ; j ++){
if(check[i][j] || array[i][j] == '#')continue;
ans++;
do_it(array,check,i,j);
}
}
return ans;
}
int main(){
cin >> n >> m;
vector<char>array[n];
bool**check;
check = (bool**)malloc(n*sizeof(bool*));
for(int i = 0 ; i < n ; i ++){
check[i] = (bool*)malloc(m*sizeof(bool));
for(int j = 0 ; j < m ; j ++){
char x;
cin >> x;
array[i].push_back(x);
check[i][j] = false;
}
}
int ans = rooms(array,check);
cout << ans;
return 0;
}