File tree Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include " bits/stdc++.h"
2
+ using namespace std ;
3
+ #define ll long long
4
+ #define mod 1000000007
5
+
6
+ int n, m;
7
+ char grid[1001 ][1001 ];
8
+ bool vis[1001 ][1001 ];
9
+
10
+ void dfs (int i, int j, int n, int m)
11
+ {
12
+ if (i<0 or i>=n or j<0 or j>=m or vis[i][j] or grid[i][j]==' #' )
13
+ return ;
14
+ vis[i][j] = true ;
15
+ dfs (i-1 , j, n, m);
16
+ dfs (i, j+1 , n, m);
17
+ dfs (i+1 , j, n, m);
18
+ dfs (i, j-1 , n, m);
19
+ }
20
+
21
+ void solve ()
22
+ {
23
+ cin >> n >> m;
24
+
25
+ for (int i=0 ; i<n; i++)
26
+ {
27
+ for (int j=0 ; j<m; j++)
28
+ cin >> grid[i][j];
29
+ }
30
+
31
+ int rooms = 0 ;
32
+
33
+ for (int i=0 ; i<n; i++)
34
+ {
35
+ for (int j=0 ; j<m; j++)
36
+ {
37
+ if (grid[i][j]==' .' and !vis[i][j])
38
+ {
39
+ dfs (i, j, n, m);
40
+ rooms++;
41
+ }
42
+ }
43
+ }
44
+
45
+ cout << rooms << " \n " ;
46
+ }
47
+
48
+ int main ()
49
+ {
50
+ ios_base::sync_with_stdio (false );
51
+ cin.tie (NULL );
52
+ cout.tie (NULL );
53
+ solve ();
54
+ }
You can’t perform that action at this time.
0 commit comments