Repository navigation
Expand file tree
/
Copy pathCounting_Rooms.cpp
More file actions
64 lines (51 loc) · 1.42 KB
/
Copy pathCounting_Rooms.cpp
File metadata and controls
64 lines (51 loc) · 1.42 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
58
59
60
61
62
63
64
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
void solve() {
int n, m;
cin >> n >> m;
vector<vector<char>> a(n, vector<char>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> a[i][j];
}
}
int ans = 0;
vector<vector<bool>> ok(n, vector<bool>(m, false));
vector<int> dx = {0, 1, 0, -1};
vector<int> dy = {1, 0, -1, 0};
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] == '.' && !ok[i][j]) {
ans++;
queue<pair<int, int>> q;
q.push({i, j});
ok[i][j] = true;
while (!q.empty()) {
pair<int, int> p = q.front();
q.pop();
for (int k = 0; k < 4; k++) {
int nx = p.first + dx[k];
int ny = p.second + dy[k];
if (nx >= 0 && nx < n && ny >= 0 && ny < m
&& a[nx][ny] == '.' && !ok[nx][ny]) {
ok[nx][ny] = true;
q.push({nx, ny});
}
}
}
}
}
}
cout << ans << endl;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T = 1;
while (T--) {
solve();
}
return 0;
}