forked from Jonathan-Uy/CSES-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCounting Rooms.cpp
More file actions
36 lines (32 loc) · 750 Bytes
/
Counting Rooms.cpp
File metadata and controls
36 lines (32 loc) · 750 Bytes
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
#include <bits/stdc++.h>
using namespace std;
char c;
int N, M, cnt;
int h[] = {1, -1, 0, 0}, v[] = {0, 0, 1, -1};
bool vis[1000][1000];
void dfs(int x, int y){
vis[x][y] = true;
for(int i = 0; i < 4; i++){
int dx = x+h[i], dy = y+v[i];
if(0 <= dx && dx < N && 0 <= dy && dy < M && !vis[dx][dy])
dfs(dx, dy);
}
}
int main(){
scanf("%d %d", &N, &M);
for(int i = 0; i < N; i++){
for(int j = 0; j < M; j++){
scanf(" %c", &c);
vis[i][j] = (c == '#');
}
}
for(int i = 0; i < N; i++){
for(int j = 0; j < M; j++){
if(!vis[i][j]){
dfs(i, j);
cnt++;
}
}
}
printf("%d\n", cnt);
}