Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<queue>
- using namespace std;
- main(){
- int I,J,i,j,k=0,n,m; queue<int>q;
- cin>>n>>m;string a[n];
- for(i=0;i<n;++i)cin>>a[i];
- for(I=0;I<n;++I)for(J=0;J<m;++J)
- if(a[I][J]=='#'){k++;
- a[I][J]='.';q.push(I);q.push(J);
- while(!q.empty()){
- i=q.front();q.pop();j=q.front();q.pop();
- if(i && a[i-1][j]=='#'){
- a[i-1][j]='.';q.push(i-1);q.push(j);}
- if(i<n-1 && a[i+1][j]=='#'){
- a[i+1][j]='.';q.push(i+1);q.push(j);}
- if(j && a[i][j-1]=='#'){
- a[i][j-1]='.';q.push(i);q.push(j-1);}
- if(j<m-1 and a[i][j+1]=='#'){
- a[i][j+1]='.';q.push(i);q.push(j+1);}}}
- cout<<k;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement