Advertisement
Guest User

Untitled

a guest
Nov 10th, 2021
478
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define all(x) x.begin(),x.end()
  4. using ll = long long int ;
  5. const long mod = 1e9+7;
  6. const ll inf = 1e+14;
  7. #define mk make_pair
  8. #define pb push_back
  9. bool vis[1001][1001];
  10. char arr[1001][1001];
  11. int n,m;
  12. bool valid(int x,int y){
  13. if(x>n or x<1 or y<1 or y>m)return false;
  14. else if (vis[x][y]==true or arr[x][y]=='#')return false;
  15. else return true;
  16. }
  17. void dfs(int x,int y){
  18. vis[x][y]=true;
  19. if(valid(x-1,y))dfs(x-1, y);
  20. else if (valid(x,y+1))dfs(x , y+1);
  21. else if(valid(x,y-1))dfs(x , y-1);
  22. else if(valid(x+1,y))dfs(x+1, y);
  23.  
  24. }
  25. void solve(){
  26. memset(vis,false,sizeof(vis));
  27. cin>>n>>m;
  28. for(int i=1;i<=n;i++){
  29. for(int j=1;j<=m;j++)cin>>arr[i][j];
  30. }
  31. int cnt=0;
  32. for(int i =1;i<=n;i++){
  33. for(int j=1;j<=m;j++){
  34. if(arr[i][j]=='.' and vis[i][j]==false){
  35. cnt++;
  36. dfs(i,j);
  37. }
  38. }
  39. }
  40. cout<<cnt;
  41.  
  42. }int main(){
  43. #ifndef ONLINE_JUDGE
  44. freopen("in.txt", "r", stdin);
  45. freopen("out.txt", "w", stdout);
  46. #endif
  47. ios_base::sync_with_stdio(0);
  48. cin.tie(0);
  49. cout.tie(0);//
  50. int t =1;// cin>>t;
  51. // int i =1;
  52. while(t--){
  53. solve();
  54. cout<<endl;
  55.  
  56. }
  57.  
  58.  
  59.  
  60. return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement