Advertisement
yungyao

neoj 42

Mar 21st, 2021
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. using namespace std;
  2. #include <iostream>
  3. #define runrunrun ios_base::sync_with_stdio(false),cin.tie(0);
  4.  
  5. bool chart[1010][1010];
  6.  
  7. void dfs(int i,int j){
  8.     chart[i][j] = false;
  9.     if (chart[i-1][j]){
  10.         dfs (i-1,j);
  11.     }
  12.     if (chart[i][j+1]){
  13.         dfs (i,j+1);
  14.     }
  15.     if (chart[i][j-1]){
  16.         dfs (i,j-1);
  17.     }
  18.     if (chart[i+1][j]){
  19.         dfs (i+1,j);
  20.     }
  21. }
  22.  
  23. int main(){
  24.     int m,n;
  25.     int t;
  26.     runrunrun
  27.  
  28.     for(cin >> t;t--;){
  29.         cin >> m >> n;
  30.         int chunkC = 0;
  31.         for (int i=0;i<1010;++i){
  32.             for (int j=0;j<1010;++j)
  33.                 chart[i][j] = false;
  34.         }
  35.  
  36.         for (int i=1;i<=m;++i){
  37.             string input;
  38.             cin >> input;
  39.             for (int j=1;j<=n;++j){
  40.                 chart[i][j] = input[j-1] == '.';
  41.             }
  42.         }
  43.  
  44.         for (int i=1;i<=m;++i){
  45.             for (int j=1;j<=n;++j){
  46.                 if (chart[i][j]){
  47.                     dfs (i,j);
  48.                     ++chunkC;
  49.                 }
  50.             }
  51.         }
  52.  
  53.         cout << chunkC << '\n';
  54.     }
  55.  
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement