-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCountingRooms.java
More file actions
37 lines (36 loc) · 1.12 KB
/
CountingRooms.java
File metadata and controls
37 lines (36 loc) · 1.12 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
import java.util.*;
import java.io.*;
class CountingRooms {
public static void random(char[][] c, int i, int j, boolean[][] visited) {
int n = c.length;
int m = c[0].length;
if(i < 0 || j < 0 || i > n-1 || j > m-1 || c[i][j] == '#' || visited[i][j]) return;
visited[i][j] = true;
random(c, i-1, j, visited);
random(c, i, j+1, visited);
random(c, i+1, j, visited);
random(c, i, j-1, visited);
}
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s[] = br.readLine().split(" ");
int n = Integer.parseInt(s[0]);
int m = Integer.parseInt(s[1]);
char c[][] = new char[n][m];
for(int i = 0; i < n; i++) {
c[i] = br.readLine().toCharArray();
}
int ans = 0;
boolean[][] visited = new boolean[n][m];
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(!visited[i][j] && c[i][j] == '.') {
ans++;
random(c, i, j, visited);
}
}
}
System.out.println(ans);
}
}