-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountingRooms.java
64 lines (46 loc) · 1.62 KB
/
CountingRooms.java
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
package PracticeGraphs;
import java.util.Scanner;
public class CountingRooms {
public static void dfs(int row , int col , char[][] buildingMap , boolean vis[][]){
vis[row][col] = true;
int n = buildingMap.length;
int m = buildingMap[0].length;
int delrow[] = {-1, 0 , 1, 0};
int delcol[] = {0 , 1 , 0, -1};
for(int i = 0; i < 4; i++){
int nrow = row + delrow[i];
int ncol = col + delcol[i];
if(nrow >= 0 && nrow < n && ncol >= 0 && ncol < m && buildingMap[nrow][ncol] == '.' && !vis[nrow][ncol]){
dfs(nrow, ncol, buildingMap, vis);
}
}
}
public static void bfs(int row , int col , char[][] buildingMap){
int n = buildingMap.length;
int m = buildingMap[0].length;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
char[][] buildingMap = new char[n][m];
for(int i = 0; i < n; i++){
String row = sc.next();
for(int j = 0; j < m; j++){
buildingMap[i][j] = row.charAt(j);
}
}
boolean vis[][] = new boolean[n][m];
int count = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(buildingMap[i][j] == '.' && vis[i][j] != true){
dfs(i , j , buildingMap , vis);
count++;
bfs(i, j, buildingMap);
}
}
}
System.out.println(count);
}
}