Advertisement
Guest User

Untitled

a guest
Sep 6th, 2021
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int a,b;
  6.  
  7. void dfs(int i, int j , vector<vector<int>>& vis, vector<vector<char>> y) {
  8.     vis[i][j] = 1;
  9.     if(i + 1 < a) {
  10.         if(y[i+1][j] == '.' && !vis[i+1][j]) {
  11.             dfs(i + 1, j, vis, y);
  12.         }
  13.     }
  14.     if(j + 1 < b) {
  15.         if(y[i][j+1] == '.' &&  !vis[i][j + 1]) {
  16.             dfs(i, j + 1, vis, y);
  17.         }  
  18.     }
  19.   if(i - 1 >= 0) {
  20.     if(y[i-1][j] == '.' && !vis[i-1][j]) {
  21.       dfs(i-1, j, vis, y);
  22.     }
  23.   }
  24.   if(j - 1 >= 0) {
  25.     if(y[i][j-1] == '.' && !vis[i][j - 1]) {
  26.       dfs(i, j - 1, vis, y);
  27.     }
  28.   }
  29. }
  30.  
  31.  
  32. int main() {
  33.   ios::sync_with_stdio(false);
  34.   cin.tie(0);
  35.     cin >> a >> b;
  36.     vector<vector<char>> y(a, vector<char> (b));
  37.     for (int i = 0; i<a; i++) {
  38.         for (int j = 0; j<b; j++) {
  39.             cin >> y[i][j];
  40.         }
  41.     }
  42.     vector<vector<int>> vis(1002, vector<int> (1002, 0));
  43.     int ans = 0;
  44.     for (int i = 0; i<a; i++) {
  45.         for (int j = 0; j<b; j++) {
  46.             if(y[i][j] == '.' && !vis[i][j]) {
  47.                 ans++;
  48.                 dfs(i, j, vis, y);
  49.             }
  50.         }
  51.     }  
  52.   cout << ans << '\n';
  53.   return 0;
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement