1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| #include<bits/stdc++.h> using namespace std; int dx[8]={-1,-1,-1,0,0,1,1,1}; int dy[8]={-1,0,1,-1,1,-1,0,1}; int a[1001][1001]; int n,m,ans; void dfs(int x,int y) { for(int i=0;i<=7;i++) { int xx=x+dx[i]; int yy=y+dy[i]; if(a[xx][yy]==1) { a[xx][yy]=0; dfs(xx,yy); } } return; }
signed main() { cin>>n>>m; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ char c; cin>>c; if(c=='.') a[i][j]=0; else a[i][j]=1; } } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(a[i][j]==1){ ans++; dfs(i,j); } } } cout<<ans<<endl; return 0; }
|