-
Notifications
You must be signed in to change notification settings - Fork 0
/
countingrooms.cpp
67 lines (49 loc) · 1.26 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
//
// Created by Tony on 8/20/2021.
//
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define f first
#define s second
#define pb push_back
typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
int R, C;
vector<string> grid;
vector<vector<bool>> used;
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
bool inbounds(pi a){
return a.f >= 0 && a.f < R && a.s >= 0 && a.s < C;
}
void floodfill(pi s) {
for (int i = 0; i < 4; i++) {
pi next = {s.f + dx[i], s.s + dy[i]};
if (inbounds(next) && !used[next.f][next.s] && grid[next.f][next.s] == '.') {
used[next.f][next.s] = true;
floodfill(next);
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
// freopen("","r",stdin);
// freopen("","w",stdout);
cin >> R >> C;
grid.assign(R,"");
used.assign(R,vector<bool>(C,false));
for (string &s : grid) cin >> s;
int counter = 0;
for (int r = 0; r < R; r++) {
for (int c = 0; c < C; c++) {
if (grid[r][c] == '.' && !used[r][c]) {
counter++;
floodfill(pi{r,c});
}
}
}
cout << counter << endl;
}