Advertisement
Mirbek

Counting Rooms

Jan 6th, 2022
1,943
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int N = 1003;
  6.  
  7. int n, m;
  8. int vis[N][N];
  9. char c[N][N];
  10.  
  11. void dfs(int x, int y) {
  12.     vis[x][y] = 1;
  13.     if (c[x - 1][y] == '.' && vis[x - 1][y] == 0) {
  14.         dfs(x - 1, y);
  15.     }
  16.     if (c[x + 1][y] == '.' && vis[x + 1][y] == 0) {
  17.         dfs(x + 1, y);
  18.     }
  19.     if (c[x][y - 1] == '.' && vis[x][y - 1] == 0) {
  20.         dfs(x, y - 1);
  21.     }
  22.     if (c[x][y + 1] == '.' && vis[x][y + 1] == 0) {
  23.         dfs(x, y + 1);
  24.     }
  25. }
  26.  
  27. int main(){
  28.     cin >> n >> m;
  29.  
  30.     for (int i = 1; i <= n; i++) {
  31.         for (int j = 1; j <= m; j++) {
  32.             cin >> c[i][j];
  33.         }
  34.     }
  35.  
  36.     int ans = 0;
  37.     for (int i = 1; i <= n; i++) {
  38.         for (int j = 1; j <= m; j++) {
  39.             if (c[i][j] == '.' && vis[i][j] == 0) {
  40.                 dfs(i, j);
  41.                 ans++;
  42.             }
  43.         }
  44.     }
  45.  
  46.     cout << ans << endl;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement