-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountingRooms.cpp
More file actions
55 lines (45 loc) · 1.09 KB
/
CountingRooms.cpp
File metadata and controls
55 lines (45 loc) · 1.09 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
//Counting Rooms - https://cses.fi/problemset/task/1192
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const ll INF = 1e18;
const vector<pair<int, int>> DIRECTIONS = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
void solve() {
int n, m;
cin >> n >> m;
vector<string> grid(n);
for (int i = 0; i < n; i++) {
cin >> grid[i];
}
auto dfs = [&](auto dfs, int x, int y) -> void {
grid[x][y] = '#';
for (auto [dx, dy]: DIRECTIONS) {
int nx = x + dx;
int ny = y + dy;
if (nx >= 0 && nx < n && ny >= 0 && ny < m && grid[nx][ny] == '.') {
dfs(dfs, nx, ny);
}
}
};
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] == '.') {
ans++;
dfs(dfs, i, j);
}
}
}
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
// cin >> T;
while (T--) {
solve();
}
return 0;
}