-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountingRooms.cpp
More file actions
52 lines (46 loc) · 1.06 KB
/
CountingRooms.cpp
File metadata and controls
52 lines (46 loc) · 1.06 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
#include <bits/stdc++.h>
using namespace std;
int neighborX[4] = {0, 0, 1, -1};
int neighborY[4] = {1, -1, 0, 0};
int n, m, answer = 0;
int vis[1010][1010];
char grid[1010][1010];
bool isValid (int y, int x) {
if (y < 0) return false;
if (x < 0) return false;
if (y >= n) return false;
if (x >= m) return false;
if (grid[y][x] == '#') return false;
return true;
}
void DFS (int y, int x) {
vis[y][x] = 1;
for (int i = 0 ; i < 4 ; i++) {
int newX = x + neighborX[i];
int newY = y + neighborY[i];
if (isValid(newY, newX)) {
if (!vis[newY][newX]) {
DFS(newY, newX);
}
}
}
}
int main() {
cin >> n >> m;
for (int i = 0 ; i < n ; i++) {
for (int j = 0 ; j < m ; j++) {
cin >> grid[i][j];
vis[i][j] = 0;
}
}
for (int i = 0 ; i < n ; i++) {
for (int j = 0 ; j < m ; j++) {
if (grid[i][j] == '.' && !vis[i][j]) {
DFS(i, j);
answer++;
}
}
}
cout << answer << endl;
return 0;
}